@vnejs/core 0.0.1

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.
Files changed (2) hide show
  1. package/index.js +83 -0
  2. package/package.json +23 -0
package/index.js ADDED
@@ -0,0 +1,83 @@
1
+ import { Capacitor } from "@capacitor/core";
2
+
3
+ import { Observer } from "@vnejs/observer";
4
+ import { VNE_CONST, VNE_EVENTS, VNE_MODULES } from "@vnejs/shared";
5
+
6
+ export class VneCore {
7
+ observer = new Observer();
8
+ modules = {};
9
+ state = {};
10
+ shared = {};
11
+
12
+ constructor() {
13
+ VNE_MODULES.forEach(this.addModule);
14
+ }
15
+
16
+ init = async (options) => {
17
+ const [ts, version] = [Math.round(new Date().getTime() / 1000 / 60 / 60), options?.global?.version || 0];
18
+ const mods = await fetch(`/mods.json?ts=${ts}&version=${version}`).then((i) => i.json());
19
+ const media = (
20
+ await Promise.all(
21
+ mods.map((mod) => fetch(`/media.${mod}.json?ts=${ts}&version=${version}`).then((i) => i.json()))
22
+ )
23
+ ).reduce((acc, media) => {
24
+ Object.keys(media).forEach((key) => {
25
+ if (!acc[key]) acc[key] = {};
26
+ Object.assign(acc[key], media[key]);
27
+ });
28
+
29
+ return acc;
30
+ }, {});
31
+ const moduleArgs = { platform: this.getPlatform(), subPlatform: this.getSubPlatform() };
32
+ const [on, emit, emitOne] = [this.observer.subscribe, this.observer.notify, this.observer.notifyOne];
33
+ const injectArgs = { on, emit, emitOne, state: this.state, shared: this.shared, media, options };
34
+
35
+ Object.values(this.modules).forEach((module) => module && module.inject(injectArgs));
36
+ Object.values(this.modules).forEach((module) => module && module.subscribe(moduleArgs));
37
+
38
+ window.uninitedModules = [];
39
+
40
+ console.log("init modules start");
41
+ await Promise.all(
42
+ Object.values(this.modules).map(async (module) => {
43
+ if (!module) return;
44
+ window.uninitedModules.push(module.name);
45
+ console.log("init module", module.name, "start");
46
+ module.isReady = false;
47
+ if (module.init) await module.init(moduleArgs);
48
+ module.isReady = true;
49
+ console.log("init module", module.name, "finish");
50
+ window.uninitedModules = window.uninitedModules.filter((k) => k !== module.name);
51
+ })
52
+ );
53
+ console.log("init modules finish");
54
+
55
+ emit(VNE_EVENTS.SYSTEM.STARTED);
56
+ };
57
+
58
+ reset = async () => {
59
+ await this.observer.notify(VNE_EVENTS.STORAGE.CLEAR);
60
+ await this.observer.notify(VNE_EVENTS.SYSTEM.RELOAD);
61
+ };
62
+
63
+ addModule = (Module) => {
64
+ const module = new Module();
65
+
66
+ this.modules[module.name] = module;
67
+ };
68
+
69
+ getPlatform = () => {
70
+ if (this.isMobile()) return VNE_CONST.PLATFORMS.MOBILE;
71
+ if (this.isDesktop()) return VNE_CONST.PLATFORMS.ELECTRON;
72
+ return VNE_CONST.PLATFORMS.WEB;
73
+ };
74
+
75
+ getSubPlatform = () => {
76
+ if (this.isMobile()) return VNE_CONST.PLATFORMS.ANDROID;
77
+ if (this.isDesktop()) return VNE_CONST.PLATFORMS.WINDOWS;
78
+ return VNE_CONST.PLATFORMS.WEB;
79
+ };
80
+
81
+ isMobile = () => Boolean(Capacitor.isNativePlatform());
82
+ isDesktop = () => navigator.userAgent.includes("Electron");
83
+ }
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@vnejs/core",
3
+ "version": "0.0.1",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1",
7
+ "publish:major": "npm version major && npm publish --access public",
8
+ "publish:minor": "npm version minor && npm publish --access public",
9
+ "publish:patch": "npm version patch && npm publish --access public"
10
+ },
11
+ "author": "",
12
+ "license": "ISC",
13
+ "description": "",
14
+ "dependencies": {
15
+ "@capacitor/core": "5.0.2",
16
+ "@vnejs/shared": "0.0.1",
17
+ "@vnejs/semaphore": "0.0.1",
18
+ "@vnejs/build": "0.0.12",
19
+ "@vnejs/desktop": "0.0.27",
20
+ "@vnejs/module": "0.0.1",
21
+ "@vnejs/observer": "0.0.1"
22
+ }
23
+ }