@vnejs/core 0.2.11 → 0.2.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +8 -9
- package/src/index.ts +69 -0
- package/src/types.ts +20 -0
- package/src/utils.ts +10 -0
- package/tsconfig.json +18 -0
package/package.json
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vnejs/core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.12",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"files": [
|
|
8
|
-
"dist"
|
|
8
|
+
"dist",
|
|
9
|
+
"src",
|
|
10
|
+
"tsconfig.json"
|
|
9
11
|
],
|
|
10
12
|
"scripts": {
|
|
11
13
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
12
|
-
"build": "
|
|
13
|
-
"publish:major": "
|
|
14
|
-
"publish:minor": "
|
|
15
|
-
"publish:patch": "
|
|
14
|
+
"build": "npx @vnejs/monorepo package",
|
|
15
|
+
"publish:major": "npx @vnejs/monorepo publish major --access public",
|
|
16
|
+
"publish:minor": "npx @vnejs/monorepo publish minor --access public",
|
|
17
|
+
"publish:patch": "npx @vnejs/monorepo publish patch --access public"
|
|
16
18
|
},
|
|
17
19
|
"author": "",
|
|
18
20
|
"license": "ISC",
|
|
@@ -33,8 +35,5 @@
|
|
|
33
35
|
"@vnejs/module.components": "~0.0.1",
|
|
34
36
|
"@vnejs/observer": "~0.0.1",
|
|
35
37
|
"@vnejs/helpers": "~0.1.0"
|
|
36
|
-
},
|
|
37
|
-
"devDependencies": {
|
|
38
|
-
"typescript": "^6.0.3"
|
|
39
38
|
}
|
|
40
39
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Observer } from "@vnejs/observer";
|
|
2
|
+
import { Module, type ModuleGlobalStateRegistry, type ModuleInject } from "@vnejs/module";
|
|
3
|
+
import { VNE, VNE_MODULES } from "@vnejs/shared";
|
|
4
|
+
import type { VneModuleClass } from "@vnejs/shared";
|
|
5
|
+
import { toJson } from "@vnejs/helpers";
|
|
6
|
+
|
|
7
|
+
import type { CoreVnePluginConstantsMap, CoreVnePluginEventsMap, ModuleMedia, ModuleShared, VneCoreModules } from "./types.js";
|
|
8
|
+
import { mergeFetchResult } from "./utils.js";
|
|
9
|
+
|
|
10
|
+
export type { ModuleMedia, ModuleShared, VneCoreModules } from "./types.js";
|
|
11
|
+
|
|
12
|
+
export class VneCore {
|
|
13
|
+
observer = new Observer();
|
|
14
|
+
modules: VneCoreModules = {};
|
|
15
|
+
state: ModuleGlobalStateRegistry = {};
|
|
16
|
+
shared: ModuleShared = {};
|
|
17
|
+
|
|
18
|
+
constructor() {
|
|
19
|
+
VNE_MODULES.forEach(this.addModule);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
init = async () => {
|
|
23
|
+
const CONST = VNE.CONST as unknown as CoreVnePluginConstantsMap;
|
|
24
|
+
const ts = Math.round(new Date().getTime() / 1000 / 60 / 60);
|
|
25
|
+
|
|
26
|
+
const mods = (await fetch(`${CONST.SYSTEM.HOST}/mods.json?ts=${ts}`).then(toJson)) as string[];
|
|
27
|
+
const fetchResult = await Promise.all(mods.map((mod) => fetch(`${CONST.SYSTEM.HOST}/media.${mod}.json?ts=${ts}`).then(toJson) as Promise<ModuleMedia>));
|
|
28
|
+
const media = fetchResult.reduce(mergeFetchResult, {} as ModuleMedia);
|
|
29
|
+
const injectArg: ModuleInject = { observer: this.observer, state: this.state, shared: this.shared, media };
|
|
30
|
+
|
|
31
|
+
Object.values(this.modules).forEach((module) => {
|
|
32
|
+
module?.inject(injectArg);
|
|
33
|
+
module?.subscribe();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
window.uninitedModules = [];
|
|
37
|
+
|
|
38
|
+
console.log("init modules start");
|
|
39
|
+
await Promise.all(
|
|
40
|
+
Object.values(this.modules).map(async (module) => {
|
|
41
|
+
if (!module) return;
|
|
42
|
+
window.uninitedModules.push(module.name);
|
|
43
|
+
console.log("init module", module.name, "start");
|
|
44
|
+
module.isReady = false;
|
|
45
|
+
if (module.init) await module.init();
|
|
46
|
+
module.isReady = true;
|
|
47
|
+
console.log("init module", module.name, "finish");
|
|
48
|
+
window.uninitedModules = window.uninitedModules.filter((k) => k !== module.name);
|
|
49
|
+
}),
|
|
50
|
+
);
|
|
51
|
+
console.log("init modules finish");
|
|
52
|
+
|
|
53
|
+
const started = VNE.EVENTS.SYSTEM?.STARTED;
|
|
54
|
+
if (started) await this.observer.emit(started);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
reset = async () => {
|
|
58
|
+
const EVENTS = VNE.EVENTS as unknown as CoreVnePluginEventsMap;
|
|
59
|
+
|
|
60
|
+
await this.observer.emit(EVENTS.STORAGE.CLEAR);
|
|
61
|
+
await this.observer.emit(EVENTS.SYSTEM.RELOAD);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
addModule = (ModuleClass: VneModuleClass) => {
|
|
65
|
+
const module = new ModuleClass() as Module;
|
|
66
|
+
|
|
67
|
+
this.modules[module.name] = module;
|
|
68
|
+
};
|
|
69
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Module, ModuleMedia, ModuleShared } from "@vnejs/module";
|
|
2
|
+
|
|
3
|
+
export type VneCoreModules = Record<string, Module | undefined>;
|
|
4
|
+
|
|
5
|
+
export type { ModuleMedia, ModuleShared };
|
|
6
|
+
|
|
7
|
+
declare global {
|
|
8
|
+
interface Window {
|
|
9
|
+
uninitedModules: string[];
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface CoreVnePluginEventsMap {
|
|
14
|
+
STORAGE: { CLEAR: string };
|
|
15
|
+
SYSTEM: { RELOAD: string; STARTED: string; HOST: string };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface CoreVnePluginConstantsMap {
|
|
19
|
+
SYSTEM: { HOST: string };
|
|
20
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ModuleMedia } from "./types.js";
|
|
2
|
+
|
|
3
|
+
export const mergeFetchResult = (acc: ModuleMedia, media: ModuleMedia) => {
|
|
4
|
+
Object.keys(media).forEach((key) => {
|
|
5
|
+
if (!acc[key]) acc[key] = {};
|
|
6
|
+
Object.assign(acc[key], media[key]);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
return acc;
|
|
10
|
+
};
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "Bundler",
|
|
6
|
+
"lib": ["ES2022", "DOM"],
|
|
7
|
+
"strict": true,
|
|
8
|
+
"skipLibCheck": true,
|
|
9
|
+
"declaration": true,
|
|
10
|
+
"noEmitOnError": true,
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"forceConsistentCasingInFileNames": true,
|
|
13
|
+
"rootDir": "src",
|
|
14
|
+
"outDir": "dist"
|
|
15
|
+
},
|
|
16
|
+
"include": ["src/**/*.ts"],
|
|
17
|
+
"exclude": ["dist", "node_modules"]
|
|
18
|
+
}
|