@vnejs/module 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 +62 -0
  2. package/package.json +14 -0
package/index.js ADDED
@@ -0,0 +1,62 @@
1
+ import { VNE_CONST, VNE_EVENTS, VNE_SETTINGS, VNE_MODULE_EXTENSIONS } from "@vnejs/shared";
2
+
3
+ export class Module {
4
+ isReady = false;
5
+
6
+ EVENTS = VNE_EVENTS;
7
+ CONST = VNE_CONST;
8
+ SETTINGS = VNE_SETTINGS;
9
+
10
+ constructor() {
11
+ Object.keys(VNE_MODULE_EXTENSIONS).forEach((funcName) => {
12
+ this[funcName] = VNE_MODULE_EXTENSIONS[funcName].bind(this, this);
13
+ });
14
+ }
15
+
16
+ inject = ({ on, emit, emitOne, state, shared, media, options } = {}) => {
17
+ // observer
18
+ this.on = on;
19
+ this.emit = emit;
20
+ this.emitOne = emitOne;
21
+
22
+ // state
23
+ this.globalState = state;
24
+ this.state = this.globalState[this.name] = this.getDefaultState() || {};
25
+
26
+ this.shared = shared;
27
+ this.media = media;
28
+ this.options = options;
29
+ }; // abstract
30
+ subscribe = () => {}; // implement
31
+
32
+ init = async () => {}; // implement
33
+
34
+ setState = (state = {}) => {
35
+ this.globalState[this.name] = state;
36
+ this.state = this.globalState[this.name];
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/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "@vnejs/module",
3
+ "version": "0.0.1",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "author": "",
10
+ "license": "ISC",
11
+ "peerDependencies": {
12
+ "@vnejs/shared": "0.0.1"
13
+ }
14
+ }