@vnejs/module 0.0.12 → 0.0.14

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/index.d.ts CHANGED
@@ -1,72 +1 @@
1
- import { CONST, EVENTS, PARAMS, SETTINGS } from "@vnejs/shared";
2
-
3
- declare type GlobalState = {};
4
-
5
- declare type SHARED = {};
6
-
7
- type OnFunc = (event: string, func: (arg: any) => Promise<any>) => void;
8
- type EmitFunc = (event: string, arg: any) => Promise<any[]>;
9
- type EmitOneFunc = (event: string, arg: any) => Promise<any>;
10
-
11
- type InjectArg = {
12
- on: OnFunc;
13
- emit: EmitFunc;
14
- emitOne: EmitOneFunc;
15
-
16
- state: GlobalState;
17
-
18
- shared: SHARED;
19
- media: Record<string, Record<string, string>>;
20
- options: any;
21
- };
22
-
23
- declare type StateType = {};
24
-
25
- export class Module {
26
- isReady: Boolean;
27
-
28
- CONST: CONST;
29
- EVENTS: EVENTS;
30
- PARAMS: PARAMS;
31
- SETTINGS: SETTINGS;
32
-
33
- on: OnFunc;
34
- emit: EmitFunc;
35
- emitOne: EmitOneFunc;
36
- emitReal: EmitOneFunc;
37
-
38
- globalState: GlobalState;
39
- state: StateType;
40
-
41
- shared: SHARED;
42
- media: Record<string, Record<string, string>>;
43
- options: any;
44
-
45
- inject: (arg: InjectArg) => void; // abstract
46
-
47
- subscribe: () => void;
48
- init: () => Promise<void>; // implement
49
-
50
- setState: (state: StateType) => void; // abstract
51
- updateState: (state: Partial<StateType>) => void; // abstract
52
- setDefaultState: () => void;
53
- setStateFromGlobalState: (state: GlobalState) => void;
54
- getState: () => StateType;
55
- getDefaultState: () => StateType; // implement
56
-
57
- waitCheck: (checkFunc: () => Boolean) => Promise<void>;
58
- waitRerenderCheck: (checkFunc: () => Boolean) => Promise<void>;
59
- waitIsReady: () => Promise<void>;
60
- waitTimeout: (time: number) => Promise<void>;
61
- waitImmediate: () => Promise<void>;
62
- waitRender: () => Promise<void>;
63
- waitRerender: () => Promise<void>;
64
-
65
- getIsReady: () => Boolean;
66
- }
67
-
68
- declare module "@vnejs/shared" {
69
- export const VNE_MODULES: Module[];
70
-
71
- export const regModule: (m: Module) => void;
72
- }
1
+ export * from "./modules/module";
package/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./modules/module";
2
+
@@ -0,0 +1,44 @@
1
+ import type { VnePluginConstants, VnePluginEvents, VnePluginParams, VnePluginSettings, VneNamespace } from "@vnejs/shared";
2
+ export type ModuleInject = {
3
+ on?: (...args: unknown[]) => unknown;
4
+ emit?: (...args: unknown[]) => unknown;
5
+ emitOne?: (...args: unknown[]) => unknown;
6
+ emitReal?: (...args: unknown[]) => unknown;
7
+ state?: Record<string, unknown>;
8
+ shared?: unknown;
9
+ media?: unknown;
10
+ };
11
+ export declare class Module {
12
+ isReady: boolean;
13
+ isOnlyLocalState: boolean;
14
+ EVENTS: Record<VneNamespace, VnePluginEvents>;
15
+ CONST: Record<VneNamespace, VnePluginConstants>;
16
+ SETTINGS: Record<VneNamespace, VnePluginSettings>;
17
+ PARAMS: Record<VneNamespace, VnePluginParams>;
18
+ on?: ModuleInject["on"];
19
+ emit?: ModuleInject["emit"];
20
+ emitOne?: ModuleInject["emitOne"];
21
+ emitReal?: ModuleInject["emitReal"];
22
+ shared?: unknown;
23
+ media?: unknown;
24
+ name: string;
25
+ globalState: Record<string, unknown>;
26
+ state: unknown;
27
+ inject: ({ on, emit, emitOne, emitReal, state, shared, media }?: ModuleInject) => void;
28
+ subscribe: () => void;
29
+ init: () => Promise<void>;
30
+ setState: (state?: unknown) => void;
31
+ updateState: (state?: Record<string, unknown>) => void;
32
+ setDefaultState: () => void;
33
+ setStateFromGlobalState: ({ [this.name]: state }: Record<string, unknown>) => void;
34
+ getState: () => unknown;
35
+ getDefaultState: () => {};
36
+ waitCheck: (checkFunc: () => boolean) => Promise<void>;
37
+ waitRerenderCheck: (checkFunc: () => boolean) => Promise<void>;
38
+ waitIsReady: () => Promise<void>;
39
+ waitTimeout: (time: number) => Promise<void>;
40
+ waitImmediate: () => Promise<void>;
41
+ waitRender: () => Promise<number>;
42
+ waitRerender: () => Promise<void>;
43
+ getIsReady: () => boolean;
44
+ }
package/modules/module.js CHANGED
@@ -1,57 +1,59 @@
1
1
  import { VNE } from "@vnejs/shared";
2
-
3
2
  export class Module {
4
- isReady = false;
5
-
6
- EVENTS = VNE.EVENTS;
7
- CONST = VNE.CONST;
8
- SETTINGS = VNE.SETTINGS;
9
- PARAMS = VNE.PARAMS;
10
-
11
- inject = ({ on, emit, emitOne, emitReal, state, shared, media } = {}) => {
12
- // observer
13
- this.on = on;
14
- this.emit = emit;
15
- this.emitOne = emitOne;
16
- this.emitReal = emitReal;
17
-
18
- // state
19
- this.globalState = state;
20
- this.state = this.globalState[this.name] = this.getDefaultState() || {};
21
-
22
- this.shared = shared;
23
- this.media = media;
24
- }; // abstract
25
- subscribe = () => {}; // implement
26
-
27
- init = async () => {}; // implement
28
-
29
- setState = (state = {}) => {
30
- this.globalState[this.name] = state;
31
- this.state = this.globalState[this.name];
32
- }; // abstract
33
-
34
- updateState = (state = {}) => this.setState({ ...this.state, ...state });
35
-
36
- setDefaultState = () => this.setState(this.getDefaultState());
37
-
38
- setStateFromGlobalState = ({ [this.name]: state }) => this.setState(state);
39
-
40
- getState = () => this.state;
41
-
42
- getDefaultState = () => ({}); // implement
43
-
44
- waitCheck = async (checkFunc) => {
45
- while (!checkFunc()) await this.waitTimeout(0);
46
- };
47
- waitRerenderCheck = async (checkFunc) => {
48
- while (!checkFunc()) await this.waitRerender();
49
- };
50
- waitIsReady = () => this.waitCheck(this.getIsReady);
51
- waitTimeout = (time) => new Promise((resolve) => setTimeout(resolve, time));
52
- waitImmediate = () => new Promise(setImmediate);
53
- waitRender = () => new Promise(requestAnimationFrame);
54
- waitRerender = () => new Promise((resolve) => requestAnimationFrame(() => setTimeout(resolve, 0)));
55
-
56
- getIsReady = () => this.isReady;
3
+ constructor() {
4
+ this.isReady = false;
5
+ this.isOnlyLocalState = false;
6
+ this.EVENTS = VNE.EVENTS;
7
+ this.CONST = VNE.CONST;
8
+ this.SETTINGS = VNE.SETTINGS;
9
+ this.PARAMS = VNE.PARAMS;
10
+ this.inject = ({ on, emit, emitOne, emitReal, state, shared, media } = {}) => {
11
+ // observer
12
+ this.on = on;
13
+ this.emit = emit;
14
+ this.emitOne = emitOne;
15
+ this.emitReal = emitReal;
16
+ // state
17
+ this.globalState = state ?? {};
18
+ this.setState(this.getDefaultState() || {});
19
+ this.shared = shared;
20
+ this.media = media;
21
+ }; // abstract
22
+ this.subscribe = () => { }; // implement
23
+ this.init = async () => { }; // implement
24
+ this.setState = (state = {}) => {
25
+ if (this.isOnlyLocalState) {
26
+ this.state = state;
27
+ }
28
+ else {
29
+ this.globalState[this.name] = state;
30
+ this.state = this.globalState[this.name];
31
+ }
32
+ }; // abstract
33
+ this.updateState = (state = {}) => this.setState({ ...this.state, ...state });
34
+ this.setDefaultState = () => this.setState(this.getDefaultState());
35
+ this.setStateFromGlobalState = ({ [this.name]: state }) => this.setState(state);
36
+ this.getState = () => this.state;
37
+ this.getDefaultState = () => ({}); // implement
38
+ this.waitCheck = async (checkFunc) => {
39
+ while (!checkFunc())
40
+ await this.waitTimeout(0);
41
+ };
42
+ this.waitRerenderCheck = async (checkFunc) => {
43
+ while (!checkFunc())
44
+ await this.waitRerender();
45
+ };
46
+ this.waitIsReady = () => this.waitCheck(this.getIsReady);
47
+ this.waitTimeout = (time) => new Promise((resolve) => setTimeout(resolve, time));
48
+ this.waitImmediate = () => new Promise((resolve) => {
49
+ const maybeSetImmediate = globalThis.setImmediate;
50
+ if (typeof maybeSetImmediate === "function")
51
+ maybeSetImmediate(resolve);
52
+ else
53
+ setTimeout(resolve, 0);
54
+ });
55
+ this.waitRender = () => new Promise((resolve) => requestAnimationFrame(resolve));
56
+ this.waitRerender = () => new Promise((resolve) => requestAnimationFrame(() => setTimeout(resolve, 0)));
57
+ this.getIsReady = () => this.isReady;
58
+ }
57
59
  }
@@ -0,0 +1,98 @@
1
+ import { VNE } from "@vnejs/shared";
2
+ import type { VnePluginConstants, VnePluginEvents, VnePluginParams, VnePluginSettings, VneNamespace } from "@vnejs/shared";
3
+
4
+ export type ModuleInject = {
5
+ on?: (...args: unknown[]) => unknown;
6
+ emit?: (...args: unknown[]) => unknown;
7
+ emitOne?: (...args: unknown[]) => unknown;
8
+ emitReal?: (...args: unknown[]) => unknown;
9
+ state?: Record<string, unknown>;
10
+ shared?: unknown;
11
+ media?: unknown;
12
+ };
13
+
14
+ export class Module {
15
+ isReady = false;
16
+ isOnlyLocalState = false;
17
+
18
+ EVENTS: Record<VneNamespace, VnePluginEvents> = VNE.EVENTS;
19
+ CONST: Record<VneNamespace, VnePluginConstants> = VNE.CONST;
20
+ SETTINGS: Record<VneNamespace, VnePluginSettings> = VNE.SETTINGS;
21
+ PARAMS: Record<VneNamespace, VnePluginParams> = VNE.PARAMS;
22
+
23
+ // runtime-injected
24
+ on?: ModuleInject["on"];
25
+ emit?: ModuleInject["emit"];
26
+ emitOne?: ModuleInject["emitOne"];
27
+ emitReal?: ModuleInject["emitReal"];
28
+
29
+ shared?: unknown;
30
+ media?: unknown;
31
+
32
+ name!: string;
33
+ globalState!: Record<string, unknown>;
34
+ state!: unknown;
35
+
36
+ inject = ({ on, emit, emitOne, emitReal, state, shared, media }: ModuleInject = {}) => {
37
+ // observer
38
+ this.on = on;
39
+ this.emit = emit;
40
+ this.emitOne = emitOne;
41
+ this.emitReal = emitReal;
42
+
43
+ // state
44
+ this.globalState = state ?? {};
45
+ this.setState(this.getDefaultState() || {});
46
+
47
+ this.shared = shared;
48
+ this.media = media;
49
+ }; // abstract
50
+
51
+ subscribe = () => {}; // implement
52
+
53
+ init = async () => {}; // implement
54
+
55
+ setState = (state: unknown = {}) => {
56
+ if (this.isOnlyLocalState) {
57
+ this.state = state;
58
+ } else {
59
+ this.globalState[this.name] = state;
60
+ this.state = this.globalState[this.name];
61
+ }
62
+ }; // abstract
63
+
64
+ updateState = (state: Record<string, unknown> = {}) => this.setState({ ...(this.state as Record<string, unknown>), ...state });
65
+
66
+ setDefaultState = () => this.setState(this.getDefaultState());
67
+
68
+ setStateFromGlobalState = ({ [this.name]: state }: Record<string, unknown>) => this.setState(state);
69
+
70
+ getState = () => this.state;
71
+
72
+ getDefaultState = () => ({}); // implement
73
+
74
+ waitCheck = async (checkFunc: () => boolean) => {
75
+ while (!checkFunc()) await this.waitTimeout(0);
76
+ };
77
+
78
+ waitRerenderCheck = async (checkFunc: () => boolean) => {
79
+ while (!checkFunc()) await this.waitRerender();
80
+ };
81
+
82
+ waitIsReady = () => this.waitCheck(this.getIsReady);
83
+
84
+ waitTimeout = (time: number) => new Promise<void>((resolve) => setTimeout(resolve, time));
85
+
86
+ waitImmediate = () =>
87
+ new Promise<void>((resolve) => {
88
+ const maybeSetImmediate = (globalThis as unknown as { setImmediate?: (cb: () => void) => unknown }).setImmediate;
89
+ if (typeof maybeSetImmediate === "function") maybeSetImmediate(resolve);
90
+ else setTimeout(resolve, 0);
91
+ });
92
+
93
+ waitRender = () => new Promise<number>((resolve) => requestAnimationFrame(resolve));
94
+
95
+ waitRerender = () => new Promise<void>((resolve) => requestAnimationFrame(() => setTimeout(resolve, 0)));
96
+
97
+ getIsReady = () => this.isReady;
98
+ }
package/package.json CHANGED
@@ -1,17 +1,19 @@
1
1
  {
2
2
  "name": "@vnejs/module",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
4
4
  "description": "",
5
5
  "main": "index.js",
6
+ "types": "index.d.ts",
6
7
  "scripts": {
7
8
  "test": "echo \"Error: no test specified\" && exit 1",
8
- "publish:major": "npm version major && npm publish --access public",
9
- "publish:minor": "npm version minor && npm publish --access public",
10
- "publish:patch": "npm version patch && npm publish --access public"
9
+ "build": "tsc -p tsconfig.json",
10
+ "publish:major": "npm version major && npm publish --access public --legacy-peer-deps",
11
+ "publish:minor": "npm version minor && npm publish --access public --legacy-peer-deps",
12
+ "publish:patch": "npm version patch && npm publish --access public --legacy-peer-deps"
11
13
  },
12
14
  "author": "",
13
15
  "license": "ISC",
14
16
  "peerDependencies": {
15
- "@vnejs/shared": "~0.0.8"
17
+ "@vnejs/shared": "~0.0.9"
16
18
  }
17
19
  }
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "ES2020",
5
+ "moduleResolution": "Bundler",
6
+ "strict": true,
7
+ "skipLibCheck": true,
8
+ "declaration": true,
9
+ "noEmitOnError": true,
10
+ "esModuleInterop": true,
11
+ "forceConsistentCasingInFileNames": true
12
+ },
13
+ "include": ["index.ts", "modules/**/*.ts"]
14
+ }
15
+
package/shared.d.ts DELETED
@@ -1,7 +0,0 @@
1
- import { Module } from ".";
2
-
3
- declare module "@vnejs/shared" {
4
- export const VNE_MODULES: Module[];
5
-
6
- export const regModule: (m: Module) => void;
7
- }