@vnejs/plugins.vars 0.1.5 → 0.1.7
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/const/events.js +1 -0
- package/modules/vars.js +10 -2
- package/package.json +1 -1
package/const/events.js
CHANGED
package/modules/vars.js
CHANGED
|
@@ -3,9 +3,12 @@ import { Module } from "@vnejs/module";
|
|
|
3
3
|
export class Vars extends Module {
|
|
4
4
|
name = "vars";
|
|
5
5
|
|
|
6
|
+
defaultVarValues = {};
|
|
7
|
+
|
|
6
8
|
subscribe = () => {
|
|
7
9
|
this.on(this.EVENTS.VARS.GET, this.onVarGet);
|
|
8
10
|
this.on(this.EVENTS.VARS.SET, this.onVarSet);
|
|
11
|
+
this.on(this.EVENTS.VARS.DEFAULT_REG, this.onVarDefaultReg);
|
|
9
12
|
|
|
10
13
|
this.on(this.EVENTS.STATE.SET, this.onStateSet);
|
|
11
14
|
this.on(this.EVENTS.STATE.CLEAR, this.setDefaultState);
|
|
@@ -16,10 +19,15 @@ export class Vars extends Module {
|
|
|
16
19
|
|
|
17
20
|
return isGlobal ? this.emit(this.EVENTS.VARS.GLOBAL_SET, { name: varName, value }) : this.setLocalVar(varName, value);
|
|
18
21
|
};
|
|
19
|
-
onVarGet = ({ name = "", type = "", isGlobal = false } = {}) => {
|
|
22
|
+
onVarGet = async ({ name = "", type = "", isGlobal = false, state = null } = {}) => {
|
|
20
23
|
const varName = this.getInnerVarName(name, type);
|
|
21
24
|
|
|
22
|
-
|
|
25
|
+
const value = isGlobal ? await this.emitOne(this.EVENTS.VARS.GLOBAL_GET, { name: varName }) : state ? state.vars[varName] : this.state[varName];
|
|
26
|
+
|
|
27
|
+
return value ?? this.defaultVarValues[type];
|
|
28
|
+
};
|
|
29
|
+
onVarDefaultReg = ({ type = "", value } = {}) => {
|
|
30
|
+
this.defaultVarValues[type] = value;
|
|
23
31
|
};
|
|
24
32
|
|
|
25
33
|
onStateSet = async ({ [this.name]: state } = {}) => this.setState(await this.emitOne(this.EVENTS.VENDORS.CLONE, state));
|