@vnejs/plugins.vars 0.1.5 → 0.1.6

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 CHANGED
@@ -3,6 +3,7 @@ export const SUBSCRIBE_EVENTS = {
3
3
  SET: "vne:vars:set",
4
4
  INJECT: "vne:vars:inject",
5
5
  SCENARIO: "vne:vars:scenario",
6
+ DEFAULT_REG: "vne:vars:default_reg",
6
7
 
7
8
  GLOBAL_SET: "vne:vars:global_set",
8
9
  GLOBAL_GET: "vne:vars:global_get",
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 } = {}) => {
20
23
  const varName = this.getInnerVarName(name, type);
21
24
 
22
- return isGlobal ? this.emitOne(this.EVENTS.VARS.GLOBAL_GET, { name: varName }) : this.state[varName];
25
+ const value = isGlobal ? await this.emitOne(this.EVENTS.VARS.GLOBAL_GET, { name: 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));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vnejs/plugins.vars",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",