@vnejs/module 0.0.13 → 0.0.15

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.
@@ -0,0 +1,39 @@
1
+ import type { Observer } from "@vnejs/observer";
2
+ import type { VnePluginConstantsMapRegistry, VnePluginEventsMapRegistry, VnePluginParamsMapRegistry, VnePluginSettingsMapRegistry } from "@vnejs/shared";
3
+ export type { ModuleGlobalState, ModuleGlobalStateRegistry, ModuleInject, ModuleMedia, ModuleShared } from "./types.js";
4
+ import type { ModuleGlobalStateRegistry, ModuleInject, ModuleMedia, ModuleShared } from "./types.js";
5
+ export declare class Module {
6
+ isReady: boolean;
7
+ isOnlyLocalState: boolean;
8
+ EVENTS: VnePluginEventsMapRegistry;
9
+ CONST: VnePluginConstantsMapRegistry;
10
+ SETTINGS: VnePluginSettingsMapRegistry;
11
+ PARAMS: VnePluginParamsMapRegistry;
12
+ observer?: Observer;
13
+ on: (...args: Parameters<Observer["subscribe"]>) => undefined;
14
+ emit: (...args: Parameters<Observer["emit"]>) => Promise<unknown[]> | undefined;
15
+ emitOne: (...args: Parameters<Observer["emitOne"]>) => Promise<unknown> | undefined;
16
+ emitReal: (...args: Parameters<Observer["emitReal"]>) => Promise<unknown> | undefined;
17
+ shared?: ModuleShared;
18
+ media?: ModuleMedia;
19
+ name: string;
20
+ globalState: ModuleGlobalStateRegistry;
21
+ state: unknown;
22
+ inject: ({ observer, state, shared, media }?: ModuleInject) => void;
23
+ subscribe: () => void;
24
+ init: () => Promise<void>;
25
+ setState: (state?: unknown) => void;
26
+ updateState: (state?: Record<string, unknown>) => void;
27
+ setDefaultState: () => void;
28
+ setStateFromGlobalState: ({ [this.name]: state }: ModuleGlobalStateRegistry) => void;
29
+ getState: () => unknown;
30
+ getDefaultState: () => {};
31
+ waitCheck: (checkFunc: () => boolean) => Promise<void>;
32
+ waitRerenderCheck: (checkFunc: () => boolean) => Promise<void>;
33
+ waitIsReady: () => Promise<void>;
34
+ waitTimeout: (time: number) => Promise<void>;
35
+ waitImmediate: () => Promise<void>;
36
+ waitRender: () => Promise<number>;
37
+ waitRerender: () => Promise<void>;
38
+ getIsReady: () => boolean;
39
+ }
package/dist/index.js ADDED
@@ -0,0 +1,62 @@
1
+ import { VNE } from "@vnejs/shared";
2
+ export class Module {
3
+ isReady = false;
4
+ isOnlyLocalState = false;
5
+ EVENTS = VNE.EVENTS;
6
+ CONST = VNE.CONST;
7
+ SETTINGS = VNE.SETTINGS;
8
+ PARAMS = VNE.PARAMS;
9
+ observer;
10
+ on = (...args) => this.observer?.subscribe(...args);
11
+ emit = (...args) => this.observer?.emit(...args);
12
+ emitOne = (...args) => this.observer?.emitOne(...args);
13
+ emitReal = (...args) => this.observer?.emitReal(...args);
14
+ shared;
15
+ media;
16
+ name;
17
+ globalState;
18
+ state;
19
+ inject = ({ observer, state, shared, media } = {}) => {
20
+ this.observer = observer;
21
+ this.globalState = state ?? {};
22
+ this.setState(this.getDefaultState() || {});
23
+ this.shared = shared;
24
+ this.media = media;
25
+ }; // abstract
26
+ subscribe = () => { }; // implement
27
+ init = async () => { }; // implement
28
+ setState = (state = {}) => {
29
+ if (this.isOnlyLocalState) {
30
+ this.state = state;
31
+ }
32
+ else {
33
+ this.globalState[this.name] = state;
34
+ this.state = this.globalState[this.name];
35
+ }
36
+ }; // abstract
37
+ updateState = (state = {}) => this.setState({ ...this.state, ...state });
38
+ setDefaultState = () => this.setState(this.getDefaultState());
39
+ setStateFromGlobalState = ({ [this.name]: state }) => this.setState(state);
40
+ getState = () => this.state;
41
+ getDefaultState = () => ({}); // implement
42
+ waitCheck = async (checkFunc) => {
43
+ while (!checkFunc())
44
+ await this.waitTimeout(0);
45
+ };
46
+ waitRerenderCheck = async (checkFunc) => {
47
+ while (!checkFunc())
48
+ await this.waitRerender();
49
+ };
50
+ waitIsReady = () => this.waitCheck(this.getIsReady);
51
+ waitTimeout = (time) => new Promise((resolve) => setTimeout(resolve, time));
52
+ waitImmediate = () => new Promise((resolve) => {
53
+ const maybeSetImmediate = globalThis.setImmediate;
54
+ if (typeof maybeSetImmediate === "function")
55
+ maybeSetImmediate(resolve);
56
+ else
57
+ setTimeout(resolve, 0);
58
+ });
59
+ waitRender = () => new Promise((resolve) => requestAnimationFrame(resolve));
60
+ waitRerender = () => new Promise((resolve) => requestAnimationFrame(() => setTimeout(resolve, 0)));
61
+ getIsReady = () => this.isReady;
62
+ }
@@ -0,0 +1,29 @@
1
+ import type { Observer } from "@vnejs/observer";
2
+ /**
3
+ * Extend in consumer packages via module augmentation:
4
+ *
5
+ * @example
6
+ * declare module "@vnejs/module" {
7
+ * interface ModuleShared {
8
+ * settings: Record<string, unknown>;
9
+ * lang: string;
10
+ * }
11
+ * interface ModuleGlobalState {
12
+ * text: { tokens: string[] };
13
+ * }
14
+ * }
15
+ */
16
+ export interface ModuleShared {
17
+ }
18
+ /** Global module state registry; defaults to string-keyed records per module name. */
19
+ export interface ModuleGlobalState {
20
+ }
21
+ export type ModuleGlobalStateRegistry = ModuleGlobalState & Record<string, unknown>;
22
+ /** Media bucket id → path/key → url or asset path. */
23
+ export type ModuleMedia = Record<string, Record<string, string>>;
24
+ export type ModuleInject = {
25
+ observer?: Observer;
26
+ state?: ModuleGlobalStateRegistry;
27
+ shared?: ModuleShared;
28
+ media?: ModuleMedia;
29
+ };
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,17 +1,26 @@
1
1
  {
2
2
  "name": "@vnejs/module",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "description": "",
5
- "main": "index.js",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
6
10
  "scripts": {
7
11
  "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"
12
+ "build": "rm -rf dist && tsc -p tsconfig.json",
13
+ "publish:major": "npm run build && npm version major && npm publish --access public --legacy-peer-deps",
14
+ "publish:minor": "npm run build && npm version minor && npm publish --access public --legacy-peer-deps",
15
+ "publish:patch": "npm run build && npm version patch && npm publish --access public --legacy-peer-deps"
11
16
  },
12
17
  "author": "",
13
18
  "license": "ISC",
14
19
  "peerDependencies": {
15
- "@vnejs/shared": "~0.0.8"
20
+ "@vnejs/observer": "~0.0.1",
21
+ "@vnejs/shared": "~0.0.9"
22
+ },
23
+ "devDependencies": {
24
+ "typescript": "^6.0.3"
16
25
  }
17
26
  }
package/index.d.ts DELETED
@@ -1,72 +0,0 @@
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
- }
package/index.js DELETED
@@ -1 +0,0 @@
1
- export * from "./modules/module";
package/modules/module.js DELETED
@@ -1,62 +0,0 @@
1
- import { VNE } from "@vnejs/shared";
2
-
3
- export class Module {
4
- isReady = false;
5
- isOnlyLocalState = false;
6
-
7
- EVENTS = VNE.EVENTS;
8
- CONST = VNE.CONST;
9
- SETTINGS = VNE.SETTINGS;
10
- PARAMS = VNE.PARAMS;
11
-
12
- inject = ({ on, emit, emitOne, emitReal, state, shared, media } = {}) => {
13
- // observer
14
- this.on = on;
15
- this.emit = emit;
16
- this.emitOne = emitOne;
17
- this.emitReal = emitReal;
18
-
19
- // state
20
- this.globalState = state;
21
- this.setState(this.getDefaultState() || {});
22
-
23
- this.shared = shared;
24
- this.media = media;
25
- }; // abstract
26
- subscribe = () => {}; // implement
27
-
28
- init = async () => {}; // implement
29
-
30
- setState = (state = {}) => {
31
- if (this.isOnlyLocalState) {
32
- this.state = state;
33
- } else {
34
- this.globalState[this.name] = state;
35
- this.state = this.globalState[this.name];
36
- }
37
- }; // abstract
38
-
39
- updateState = (state = {}) => this.setState({ ...this.state, ...state });
40
-
41
- setDefaultState = () => this.setState(this.getDefaultState());
42
-
43
- setStateFromGlobalState = ({ [this.name]: state }) => this.setState(state);
44
-
45
- getState = () => this.state;
46
-
47
- getDefaultState = () => ({}); // implement
48
-
49
- waitCheck = async (checkFunc) => {
50
- while (!checkFunc()) await this.waitTimeout(0);
51
- };
52
- waitRerenderCheck = async (checkFunc) => {
53
- while (!checkFunc()) await this.waitRerender();
54
- };
55
- waitIsReady = () => this.waitCheck(this.getIsReady);
56
- waitTimeout = (time) => new Promise((resolve) => setTimeout(resolve, time));
57
- waitImmediate = () => new Promise(setImmediate);
58
- waitRender = () => new Promise(requestAnimationFrame);
59
- waitRerender = () => new Promise((resolve) => requestAnimationFrame(() => setTimeout(resolve, 0)));
60
-
61
- getIsReady = () => this.isReady;
62
- }
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
- }