@vnejs/plugins.vars.counters 0.2.2 → 0.2.4

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/dist/index.js CHANGED
@@ -3,4 +3,6 @@ import { regPlugin } from "@vnejs/shared";
3
3
  import { CONSTANTS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/contracts.vars.counters";
4
4
  import { VarsCounters } from "./modules/counters.js";
5
5
  import { VarsCountersExec } from "./modules/exec.js";
6
- regPlugin(PLUGIN_NAME, { constants: CONSTANTS, events: SUBSCRIBE_EVENTS }, [VarsCounters, VarsCountersExec]);
6
+ import { VarsCountersHandlers } from "./modules/handlers.js";
7
+ import { VarsCountersScenario } from "./modules/scenario.js";
8
+ regPlugin(PLUGIN_NAME, { constants: CONSTANTS, events: SUBSCRIBE_EVENTS }, [VarsCounters, VarsCountersHandlers, VarsCountersExec, VarsCountersScenario]);
@@ -1,20 +1,10 @@
1
1
  import { ModuleCore } from "@vnejs/module.core";
2
+ import type { CountersGetPayload, CountersSetPayload } from "@vnejs/contracts.vars.counters";
2
3
  import type { CountersPluginConstants, CountersPluginEvents, CountersPluginParams, CountersPluginSettings } from "../types.js";
3
- import type { CountersChangePayload, CountersComparePayload, CountersExecCheckPayload, CountersExecInfoPayload, CountersExecInfoResult, CountersGetPayload, CountersSetPayload } from "../utils/counters.js";
4
- type ChangeHandler = (oldValue?: number, value?: number) => number;
5
- type CompareHandler = (oldValue?: number, value?: number) => boolean;
6
4
  export declare class VarsCounters extends ModuleCore<CountersPluginEvents, CountersPluginConstants, CountersPluginSettings, CountersPluginParams> {
7
5
  name: string;
8
- varsChangeHandlers: Record<string, ChangeHandler>;
9
- varsCompareHandlers: Record<string, CompareHandler>;
10
6
  subscribe: () => void;
11
- init: () => Promise<unknown[]> | undefined;
12
- onCounterSet: ({ name, isGlobal, value }?: CountersSetPayload) => Promise<unknown[]> | undefined;
13
- onCounterGet: ({ name, isGlobal, state }?: CountersGetPayload) => Promise<unknown> | undefined;
14
- onCounterChange: ({ value, execValue, action }?: CountersChangePayload) => number;
15
- onCounterCompare: ({ value, execValue, action }?: CountersComparePayload) => boolean;
16
- onCounterExecInfo: ({ line, keywords, state, withValue }?: CountersExecInfoPayload) => Promise<CountersExecInfoResult>;
17
- onCounterExecCheck: ({ line, keywords, state }?: CountersExecCheckPayload) => Promise<unknown>;
18
- onCounterExecGet: ({ line, keywords, state }?: CountersExecCheckPayload) => Promise<number>;
7
+ init: () => Promise<import("@vnejs/shared").EmitResults<"vne:vars:default_reg">> | undefined;
8
+ onCounterSet: ({ name, isGlobal, value }?: CountersSetPayload) => Promise<import("@vnejs/shared").EmitResults<"vne:vars:set">> | undefined;
9
+ onCounterGet: ({ name, isGlobal, state }?: CountersGetPayload) => Promise<number | undefined>;
19
10
  }
20
- export {};
@@ -1,51 +1,14 @@
1
1
  import { ModuleCore } from "@vnejs/module.core";
2
- import { tokenizeExecLine } from "@vnejs/helpers";
3
2
  export class VarsCounters extends ModuleCore {
4
3
  name = "vars.counters";
5
- varsChangeHandlers = {
6
- [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.SET]: (oldValue = 0, value = 0) => value,
7
- [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.INC]: (oldValue = 0) => oldValue + 1,
8
- [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.DEC]: (oldValue = 0) => oldValue - 1,
9
- [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.ADD]: (oldValue = 0, value = 0) => oldValue + value,
10
- [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.SUB]: (oldValue = 0, value = 0) => oldValue - value,
11
- };
12
- varsCompareHandlers = {
13
- [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.MORE]: (oldValue = 0, value = 0) => oldValue > value,
14
- [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.MORE_EQ]: (oldValue = 0, value = 0) => oldValue >= value,
15
- [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.EQ]: (oldValue = 0, value = 0) => oldValue === value,
16
- [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.EQ_NOT]: (oldValue = 0, value = 0) => oldValue !== value,
17
- [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.LESS]: (oldValue = 0, value = 0) => oldValue < value,
18
- [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.LESS_EQ]: (oldValue = 0, value = 0) => oldValue <= value,
19
- };
20
4
  subscribe = () => {
21
5
  this.on(this.EVENTS.VARS_COUNTERS.SET, this.onCounterSet);
22
6
  this.on(this.EVENTS.VARS_COUNTERS.GET, this.onCounterGet);
23
- this.on(this.EVENTS.VARS_COUNTERS.CHANGE, this.onCounterChange);
24
- this.on(this.EVENTS.VARS_COUNTERS.COMPARE, this.onCounterCompare);
25
- this.on(this.EVENTS.VARS_COUNTERS.EXEC_INFO, this.onCounterExecInfo);
26
- this.on(this.EVENTS.VARS_COUNTERS.EXEC_CHECK, this.onCounterExecCheck);
27
- this.on(this.EVENTS.VARS_COUNTERS.EXEC_GET, this.onCounterExecGet);
28
7
  };
29
8
  init = () => this.emit(this.EVENTS.VARS.DEFAULT_REG, { type: this.CONST.VARS_COUNTERS.TYPE, value: 0 });
30
9
  onCounterSet = ({ name = "", isGlobal = false, value = 0 } = {}) => this.emit(this.EVENTS.VARS.SET, { name, type: this.CONST.VARS_COUNTERS.TYPE, value, isGlobal });
31
- onCounterGet = ({ name = "", isGlobal = false, state = null } = {}) => this.emitOne(this.EVENTS.VARS.GET, { name, type: this.CONST.VARS_COUNTERS.TYPE, isGlobal, state });
32
- onCounterChange = ({ value = 0, execValue = 0, action = "" } = {}) => this.varsChangeHandlers[action]?.(value, execValue) ?? value;
33
- onCounterCompare = ({ value = 0, execValue = 0, action = "" } = {}) => this.varsCompareHandlers[action]?.(value, execValue) ?? false;
34
- onCounterExecInfo = async ({ line = "", keywords = [], state = null, withValue = true } = {}) => {
35
- const tokens = tokenizeExecLine(line);
36
- const name = String(tokens[0] ?? "");
37
- const action = String(tokens[1] ?? "");
38
- const execValue = Number(tokens[2] ?? 0);
39
- const isGlobal = keywords.includes(this.CONST.VARS.LINE_KEYWORDS.GLOBAL);
40
- const result = { name, action, isGlobal, execValue };
41
- return withValue ? { ...result, value: Number(await this.emitOne(this.EVENTS.VARS_COUNTERS.GET, { name, isGlobal, state })) } : result;
42
- };
43
- onCounterExecCheck = async ({ line = "", keywords = [], state = null } = {}) => {
44
- const { action = "", execValue = 0, value = 0, } = (await this.emitOne(this.EVENTS.VARS_COUNTERS.EXEC_INFO, { line, keywords, state }));
45
- return this.emitOne(this.EVENTS.VARS_COUNTERS.COMPARE, { value, execValue, action });
46
- };
47
- onCounterExecGet = async ({ line = "", keywords = [], state = null } = {}) => {
48
- const { value = 0 } = (await this.emitOne(this.EVENTS.VARS_COUNTERS.EXEC_INFO, { line, keywords, state }));
49
- return value;
10
+ onCounterGet = async ({ name = "", isGlobal = false, state = null } = {}) => {
11
+ const value = await this.emitOne(this.EVENTS.VARS.GET, { name, type: this.CONST.VARS_COUNTERS.TYPE, isGlobal, state });
12
+ return typeof value === "number" ? value : value == null ? undefined : Number(value);
50
13
  };
51
14
  }
@@ -1,9 +1,10 @@
1
1
  import { ModuleCore } from "@vnejs/module.core";
2
- import type { LineExecHandlerArg } from "@vnejs/module.core";
2
+ import type { CountersExecCheckPayload, CountersExecInfoPayload, CountersExecInfoResult } from "@vnejs/contracts.vars.counters";
3
3
  import type { CountersPluginConstants, CountersPluginEvents, CountersPluginParams, CountersPluginSettings } from "../types.js";
4
4
  export declare class VarsCountersExec extends ModuleCore<CountersPluginEvents, CountersPluginConstants, CountersPluginSettings, CountersPluginParams> {
5
5
  name: string;
6
- init: () => Promise<[unknown[] | undefined, unknown[] | undefined]>;
7
- onLineExecModule: ({ line, keywords }?: LineExecHandlerArg) => Promise<void>;
8
- onLineExecBlock: ({ line, keywords }?: LineExecHandlerArg) => Promise<void>;
6
+ subscribe: () => void;
7
+ onCounterExecInfo: ({ line, keywords, state, withValue }?: CountersExecInfoPayload) => Promise<CountersExecInfoResult>;
8
+ onCounterExecCheck: ({ line, keywords, state }?: CountersExecCheckPayload) => Promise<boolean | undefined>;
9
+ onCounterExecGet: ({ line, keywords, state }?: CountersExecCheckPayload) => Promise<number>;
9
10
  }
@@ -1,21 +1,27 @@
1
1
  import { ModuleCore } from "@vnejs/module.core";
2
+ import { tokenizeExecLine } from "@vnejs/helpers";
2
3
  export class VarsCountersExec extends ModuleCore {
3
4
  name = "vars.counters.exec";
4
- init = () => Promise.all([
5
- this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { block: this.CONST.VARS_COUNTERS.EXEC_NAME, handler: this.onLineExecBlock }),
6
- this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { module: this.CONST.VARS_COUNTERS.EXEC_NAME, handler: this.onLineExecModule }),
7
- ]);
8
- onLineExecModule = async ({ line = "", keywords = [] } = {}) => {
9
- const execInfo = (await this.emitOne(this.EVENTS.VARS_COUNTERS.EXEC_INFO, { line, keywords }));
10
- const { name = "", action = "", execValue = 0, value = 0, isGlobal = false } = execInfo;
11
- const newValue = Number(await this.emitOne(this.EVENTS.VARS_COUNTERS.CHANGE, { value, execValue, action }));
12
- await this.emit(this.EVENTS.VARS_COUNTERS.SET, { name, isGlobal, value: newValue });
13
- this.emitNext();
5
+ subscribe = () => {
6
+ this.on(this.EVENTS.VARS_COUNTERS.EXEC_INFO, this.onCounterExecInfo);
7
+ this.on(this.EVENTS.VARS_COUNTERS.EXEC_CHECK, this.onCounterExecCheck);
8
+ this.on(this.EVENTS.VARS_COUNTERS.EXEC_GET, this.onCounterExecGet);
14
9
  };
15
- onLineExecBlock = async ({ line = "", keywords = [] } = {}) => {
16
- const { action = "", execValue = 0, value = 0 } = (await this.emitOne(this.EVENTS.VARS_COUNTERS.EXEC_INFO, { line, keywords }));
17
- const shouldGoInside = Boolean(await this.emitOne(this.EVENTS.VARS_COUNTERS.COMPARE, { value, execValue, action }));
18
- await this.emit(this.EVENTS.VARS.SCENARIO, { shouldGoInside });
19
- this.emitNext();
10
+ onCounterExecInfo = async ({ line = "", keywords = [], state = null, withValue = true } = {}) => {
11
+ const tokens = tokenizeExecLine(line);
12
+ const name = String(tokens[0] ?? "");
13
+ const action = String(tokens[1] ?? "");
14
+ const execValue = Number(tokens[2] ?? 0);
15
+ const isGlobal = keywords.includes(this.CONST.VARS.LINE_KEYWORDS.GLOBAL);
16
+ const result = { name, action, isGlobal, execValue };
17
+ return withValue ? { ...result, value: await this.emitOne(this.EVENTS.VARS_COUNTERS.GET, { name, isGlobal, state }) } : result;
18
+ };
19
+ onCounterExecCheck = async ({ line = "", keywords = [], state = null } = {}) => {
20
+ const { action = "", execValue = 0, value = 0 } = (await this.emitOne(this.EVENTS.VARS_COUNTERS.EXEC_INFO, { line, keywords, state })) ?? {};
21
+ return this.emitOne(this.EVENTS.VARS_COUNTERS.COMPARE, { value, execValue, action });
22
+ };
23
+ onCounterExecGet = async ({ line = "", keywords = [], state = null } = {}) => {
24
+ const { value = 0 } = (await this.emitOne(this.EVENTS.VARS_COUNTERS.EXEC_INFO, { line, keywords, state })) ?? {};
25
+ return value;
20
26
  };
21
27
  }
@@ -0,0 +1,14 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import type { CountersChangePayload, CountersComparePayload } from "@vnejs/contracts.vars.counters";
3
+ import type { CountersPluginConstants, CountersPluginEvents, CountersPluginParams, CountersPluginSettings } from "../types.js";
4
+ type ChangeHandler = (oldValue?: number, value?: number) => number;
5
+ type CompareHandler = (oldValue?: number, value?: number) => boolean;
6
+ export declare class VarsCountersHandlers extends ModuleCore<CountersPluginEvents, CountersPluginConstants, CountersPluginSettings, CountersPluginParams> {
7
+ name: string;
8
+ varsChangeHandlers: Record<string, ChangeHandler>;
9
+ varsCompareHandlers: Record<string, CompareHandler>;
10
+ subscribe: () => void;
11
+ onCounterChange: ({ value, execValue, action }?: CountersChangePayload) => number;
12
+ onCounterCompare: ({ value, execValue, action }?: CountersComparePayload) => boolean;
13
+ }
14
+ export {};
@@ -0,0 +1,25 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ export class VarsCountersHandlers extends ModuleCore {
3
+ name = "vars.counters.handlers";
4
+ varsChangeHandlers = {
5
+ [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.SET]: (oldValue = 0, value = 0) => value,
6
+ [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.INC]: (oldValue = 0) => oldValue + 1,
7
+ [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.DEC]: (oldValue = 0) => oldValue - 1,
8
+ [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.ADD]: (oldValue = 0, value = 0) => oldValue + value,
9
+ [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.SUB]: (oldValue = 0, value = 0) => oldValue - value,
10
+ };
11
+ varsCompareHandlers = {
12
+ [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.MORE]: (oldValue = 0, value = 0) => oldValue > value,
13
+ [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.MORE_EQ]: (oldValue = 0, value = 0) => oldValue >= value,
14
+ [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.EQ]: (oldValue = 0, value = 0) => oldValue === value,
15
+ [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.EQ_NOT]: (oldValue = 0, value = 0) => oldValue !== value,
16
+ [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.LESS]: (oldValue = 0, value = 0) => oldValue < value,
17
+ [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.LESS_EQ]: (oldValue = 0, value = 0) => oldValue <= value,
18
+ };
19
+ subscribe = () => {
20
+ this.on(this.EVENTS.VARS_COUNTERS.CHANGE, this.onCounterChange);
21
+ this.on(this.EVENTS.VARS_COUNTERS.COMPARE, this.onCounterCompare);
22
+ };
23
+ onCounterChange = ({ value = 0, execValue = 0, action = "" } = {}) => this.varsChangeHandlers[action]?.(value, execValue) ?? value;
24
+ onCounterCompare = ({ value = 0, execValue = 0, action = "" } = {}) => this.varsCompareHandlers[action]?.(value, execValue) ?? false;
25
+ }
@@ -0,0 +1,9 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import type { LineExecHandlerArg } from "@vnejs/module.core";
3
+ import type { CountersPluginConstants, CountersPluginEvents, CountersPluginParams, CountersPluginSettings } from "../types.js";
4
+ export declare class VarsCountersScenario extends ModuleCore<CountersPluginEvents, CountersPluginConstants, CountersPluginSettings, CountersPluginParams> {
5
+ name: string;
6
+ init: () => Promise<[import("@vnejs/shared").EmitResults<"vne:scenario:line_exec_reg"> | undefined, import("@vnejs/shared").EmitResults<"vne:scenario:line_exec_reg"> | undefined]>;
7
+ onLineExecModule: ({ line, keywords }?: LineExecHandlerArg) => Promise<void>;
8
+ onLineExecBlock: ({ line, keywords }?: LineExecHandlerArg) => Promise<void>;
9
+ }
@@ -0,0 +1,20 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ export class VarsCountersScenario extends ModuleCore {
3
+ name = "vars.counters.scenario";
4
+ init = () => Promise.all([
5
+ this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { block: this.CONST.VARS_COUNTERS.EXEC_NAME, handler: this.onLineExecBlock }),
6
+ this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { module: this.CONST.VARS_COUNTERS.EXEC_NAME, handler: this.onLineExecModule }),
7
+ ]);
8
+ onLineExecModule = async ({ line = "", keywords = [] } = {}) => {
9
+ const { name = "", action = "", execValue = 0, value = 0, isGlobal = false, } = (await this.emitOne(this.EVENTS.VARS_COUNTERS.EXEC_INFO, { line, keywords })) ?? {};
10
+ const newValue = (await this.emitOne(this.EVENTS.VARS_COUNTERS.CHANGE, { value, execValue, action })) ?? 0;
11
+ await this.emit(this.EVENTS.VARS_COUNTERS.SET, { name, isGlobal, value: newValue });
12
+ this.emitNext();
13
+ };
14
+ onLineExecBlock = async ({ line = "", keywords = [] } = {}) => {
15
+ const { action = "", execValue = 0, value = 0 } = (await this.emitOne(this.EVENTS.VARS_COUNTERS.EXEC_INFO, { line, keywords })) ?? {};
16
+ const shouldGoInside = (await this.emitOne(this.EVENTS.VARS_COUNTERS.COMPARE, { value, execValue, action })) ?? false;
17
+ await this.emit(this.EVENTS.VARS.SCENARIO, { shouldGoInside });
18
+ this.emitNext();
19
+ };
20
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vnejs/plugins.vars.counters",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -5,5 +5,7 @@ import { CONSTANTS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/contracts.vars.
5
5
 
6
6
  import { VarsCounters } from "./modules/counters.js";
7
7
  import { VarsCountersExec } from "./modules/exec.js";
8
+ import { VarsCountersHandlers } from "./modules/handlers.js";
9
+ import { VarsCountersScenario } from "./modules/scenario.js";
8
10
 
9
- regPlugin(PLUGIN_NAME, { constants: CONSTANTS, events: SUBSCRIBE_EVENTS }, [VarsCounters, VarsCountersExec]);
11
+ regPlugin(PLUGIN_NAME, { constants: CONSTANTS, events: SUBSCRIBE_EVENTS }, [VarsCounters, VarsCountersHandlers, VarsCountersExec, VarsCountersScenario]);
@@ -1,49 +1,15 @@
1
1
  import { ModuleCore } from "@vnejs/module.core";
2
- import { tokenizeExecLine } from "@vnejs/helpers";
2
+ import type { CountersGetPayload, CountersSetPayload } from "@vnejs/contracts.vars.counters";
3
+ import type { VarsSetPayload } from "@vnejs/contracts.vars";
3
4
 
4
5
  import type { CountersPluginConstants, CountersPluginEvents, CountersPluginParams, CountersPluginSettings } from "../types.js";
5
- import type {
6
- CountersChangePayload,
7
- CountersComparePayload,
8
- CountersExecCheckPayload,
9
- CountersExecInfoPayload,
10
- CountersExecInfoResult,
11
- CountersGetPayload,
12
- CountersSetPayload,
13
- VarsSetPayload,
14
- } from "../utils/counters.js";
15
-
16
- type ChangeHandler = (oldValue?: number, value?: number) => number;
17
- type CompareHandler = (oldValue?: number, value?: number) => boolean;
18
6
 
19
7
  export class VarsCounters extends ModuleCore<CountersPluginEvents, CountersPluginConstants, CountersPluginSettings, CountersPluginParams> {
20
8
  name = "vars.counters";
21
9
 
22
- varsChangeHandlers: Record<string, ChangeHandler> = {
23
- [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.SET]: (oldValue = 0, value = 0) => value,
24
- [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.INC]: (oldValue = 0) => oldValue + 1,
25
- [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.DEC]: (oldValue = 0) => oldValue - 1,
26
- [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.ADD]: (oldValue = 0, value = 0) => oldValue + value,
27
- [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.SUB]: (oldValue = 0, value = 0) => oldValue - value,
28
- };
29
-
30
- varsCompareHandlers: Record<string, CompareHandler> = {
31
- [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.MORE]: (oldValue = 0, value = 0) => oldValue > value,
32
- [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.MORE_EQ]: (oldValue = 0, value = 0) => oldValue >= value,
33
- [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.EQ]: (oldValue = 0, value = 0) => oldValue === value,
34
- [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.EQ_NOT]: (oldValue = 0, value = 0) => oldValue !== value,
35
- [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.LESS]: (oldValue = 0, value = 0) => oldValue < value,
36
- [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.LESS_EQ]: (oldValue = 0, value = 0) => oldValue <= value,
37
- };
38
-
39
10
  subscribe = () => {
40
11
  this.on(this.EVENTS.VARS_COUNTERS.SET, this.onCounterSet);
41
12
  this.on(this.EVENTS.VARS_COUNTERS.GET, this.onCounterGet);
42
- this.on(this.EVENTS.VARS_COUNTERS.CHANGE, this.onCounterChange);
43
- this.on(this.EVENTS.VARS_COUNTERS.COMPARE, this.onCounterCompare);
44
- this.on(this.EVENTS.VARS_COUNTERS.EXEC_INFO, this.onCounterExecInfo);
45
- this.on(this.EVENTS.VARS_COUNTERS.EXEC_CHECK, this.onCounterExecCheck);
46
- this.on(this.EVENTS.VARS_COUNTERS.EXEC_GET, this.onCounterExecGet);
47
13
  };
48
14
 
49
15
  init = () => this.emit(this.EVENTS.VARS.DEFAULT_REG, { type: this.CONST.VARS_COUNTERS.TYPE, value: 0 });
@@ -51,37 +17,9 @@ export class VarsCounters extends ModuleCore<CountersPluginEvents, CountersPlugi
51
17
  onCounterSet = ({ name = "", isGlobal = false, value = 0 }: CountersSetPayload = {}) =>
52
18
  this.emit(this.EVENTS.VARS.SET, { name, type: this.CONST.VARS_COUNTERS.TYPE, value, isGlobal } satisfies VarsSetPayload);
53
19
 
54
- onCounterGet = ({ name = "", isGlobal = false, state = null }: CountersGetPayload = {}) =>
55
- this.emitOne(this.EVENTS.VARS.GET, { name, type: this.CONST.VARS_COUNTERS.TYPE, isGlobal, state });
56
-
57
- onCounterChange = ({ value = 0, execValue = 0, action = "" }: CountersChangePayload = {}) => this.varsChangeHandlers[action]?.(value, execValue) ?? value;
58
-
59
- onCounterCompare = ({ value = 0, execValue = 0, action = "" }: CountersComparePayload = {}) => this.varsCompareHandlers[action]?.(value, execValue) ?? false;
60
-
61
- onCounterExecInfo = async ({ line = "", keywords = [], state = null, withValue = true }: CountersExecInfoPayload = {}) => {
62
- const tokens = tokenizeExecLine(line);
63
- const name = String(tokens[0] ?? "");
64
- const action = String(tokens[1] ?? "");
65
- const execValue = Number(tokens[2] ?? 0);
66
- const isGlobal = keywords.includes(this.CONST.VARS.LINE_KEYWORDS.GLOBAL);
67
- const result: CountersExecInfoResult = { name, action, isGlobal, execValue };
68
-
69
- return withValue ? { ...result, value: Number(await this.emitOne(this.EVENTS.VARS_COUNTERS.GET, { name, isGlobal, state })) } : result;
70
- };
71
-
72
- onCounterExecCheck = async ({ line = "", keywords = [], state = null }: CountersExecCheckPayload = {}) => {
73
- const {
74
- action = "",
75
- execValue = 0,
76
- value = 0,
77
- } = (await this.emitOne(this.EVENTS.VARS_COUNTERS.EXEC_INFO, { line, keywords, state })) as CountersExecInfoResult;
78
-
79
- return this.emitOne(this.EVENTS.VARS_COUNTERS.COMPARE, { value, execValue, action });
80
- };
81
-
82
- onCounterExecGet = async ({ line = "", keywords = [], state = null }: CountersExecCheckPayload = {}) => {
83
- const { value = 0 } = (await this.emitOne(this.EVENTS.VARS_COUNTERS.EXEC_INFO, { line, keywords, state })) as CountersExecInfoResult;
20
+ onCounterGet = async ({ name = "", isGlobal = false, state = null }: CountersGetPayload = {}) => {
21
+ const value = await this.emitOne(this.EVENTS.VARS.GET, { name, type: this.CONST.VARS_COUNTERS.TYPE, isGlobal, state });
84
22
 
85
- return value;
23
+ return typeof value === "number" ? value : value == null ? undefined : Number(value);
86
24
  };
87
25
  }
@@ -1,37 +1,38 @@
1
1
  import { ModuleCore } from "@vnejs/module.core";
2
-
3
- import type { LineExecHandlerArg } from "@vnejs/module.core";
2
+ import { tokenizeExecLine } from "@vnejs/helpers";
3
+ import type { CountersExecCheckPayload, CountersExecInfoPayload, CountersExecInfoResult } from "@vnejs/contracts.vars.counters";
4
4
 
5
5
  import type { CountersPluginConstants, CountersPluginEvents, CountersPluginParams, CountersPluginSettings } from "../types.js";
6
- import type { CountersExecInfoResult, CountersSetPayload } from "../utils/counters.js";
7
6
 
8
7
  export class VarsCountersExec extends ModuleCore<CountersPluginEvents, CountersPluginConstants, CountersPluginSettings, CountersPluginParams> {
9
8
  name = "vars.counters.exec";
10
9
 
11
- init = () =>
12
- Promise.all([
13
- this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { block: this.CONST.VARS_COUNTERS.EXEC_NAME, handler: this.onLineExecBlock }),
14
- this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { module: this.CONST.VARS_COUNTERS.EXEC_NAME, handler: this.onLineExecModule }),
15
- ]);
16
-
17
- onLineExecModule = async ({ line = "", keywords = [] }: LineExecHandlerArg = {}) => {
18
- const execInfo = (await this.emitOne(this.EVENTS.VARS_COUNTERS.EXEC_INFO, { line, keywords })) as CountersExecInfoResult;
19
- const { name = "", action = "", execValue = 0, value = 0, isGlobal = false } = execInfo;
20
-
21
- const newValue = Number(await this.emitOne(this.EVENTS.VARS_COUNTERS.CHANGE, { value, execValue, action }));
10
+ subscribe = () => {
11
+ this.on(this.EVENTS.VARS_COUNTERS.EXEC_INFO, this.onCounterExecInfo);
12
+ this.on(this.EVENTS.VARS_COUNTERS.EXEC_CHECK, this.onCounterExecCheck);
13
+ this.on(this.EVENTS.VARS_COUNTERS.EXEC_GET, this.onCounterExecGet);
14
+ };
22
15
 
23
- await this.emit(this.EVENTS.VARS_COUNTERS.SET, { name, isGlobal, value: newValue } satisfies CountersSetPayload);
16
+ onCounterExecInfo = async ({ line = "", keywords = [], state = null, withValue = true }: CountersExecInfoPayload = {}) => {
17
+ const tokens = tokenizeExecLine(line);
18
+ const name = String(tokens[0] ?? "");
19
+ const action = String(tokens[1] ?? "");
20
+ const execValue = Number(tokens[2] ?? 0);
21
+ const isGlobal = keywords.includes(this.CONST.VARS.LINE_KEYWORDS.GLOBAL);
22
+ const result: CountersExecInfoResult = { name, action, isGlobal, execValue };
24
23
 
25
- this.emitNext();
24
+ return withValue ? { ...result, value: await this.emitOne(this.EVENTS.VARS_COUNTERS.GET, { name, isGlobal, state }) } : result;
26
25
  };
27
26
 
28
- onLineExecBlock = async ({ line = "", keywords = [] }: LineExecHandlerArg = {}) => {
29
- const { action = "", execValue = 0, value = 0 } = (await this.emitOne(this.EVENTS.VARS_COUNTERS.EXEC_INFO, { line, keywords })) as CountersExecInfoResult;
27
+ onCounterExecCheck = async ({ line = "", keywords = [], state = null }: CountersExecCheckPayload = {}) => {
28
+ const { action = "", execValue = 0, value = 0 } = (await this.emitOne(this.EVENTS.VARS_COUNTERS.EXEC_INFO, { line, keywords, state })) ?? {};
30
29
 
31
- const shouldGoInside = Boolean(await this.emitOne(this.EVENTS.VARS_COUNTERS.COMPARE, { value, execValue, action }));
30
+ return this.emitOne(this.EVENTS.VARS_COUNTERS.COMPARE, { value, execValue, action });
31
+ };
32
32
 
33
- await this.emit(this.EVENTS.VARS.SCENARIO, { shouldGoInside });
33
+ onCounterExecGet = async ({ line = "", keywords = [], state = null }: CountersExecCheckPayload = {}) => {
34
+ const { value = 0 } = (await this.emitOne(this.EVENTS.VARS_COUNTERS.EXEC_INFO, { line, keywords, state })) ?? {};
34
35
 
35
- this.emitNext();
36
+ return value;
36
37
  };
37
38
  }
@@ -0,0 +1,37 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import type { CountersChangePayload, CountersComparePayload } from "@vnejs/contracts.vars.counters";
3
+
4
+ import type { CountersPluginConstants, CountersPluginEvents, CountersPluginParams, CountersPluginSettings } from "../types.js";
5
+
6
+ type ChangeHandler = (oldValue?: number, value?: number) => number;
7
+ type CompareHandler = (oldValue?: number, value?: number) => boolean;
8
+
9
+ export class VarsCountersHandlers extends ModuleCore<CountersPluginEvents, CountersPluginConstants, CountersPluginSettings, CountersPluginParams> {
10
+ name = "vars.counters.handlers";
11
+
12
+ varsChangeHandlers: Record<string, ChangeHandler> = {
13
+ [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.SET]: (oldValue = 0, value = 0) => value,
14
+ [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.INC]: (oldValue = 0) => oldValue + 1,
15
+ [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.DEC]: (oldValue = 0) => oldValue - 1,
16
+ [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.ADD]: (oldValue = 0, value = 0) => oldValue + value,
17
+ [this.CONST.VARS_COUNTERS.CHANGE_ACTIONS.SUB]: (oldValue = 0, value = 0) => oldValue - value,
18
+ };
19
+
20
+ varsCompareHandlers: Record<string, CompareHandler> = {
21
+ [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.MORE]: (oldValue = 0, value = 0) => oldValue > value,
22
+ [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.MORE_EQ]: (oldValue = 0, value = 0) => oldValue >= value,
23
+ [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.EQ]: (oldValue = 0, value = 0) => oldValue === value,
24
+ [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.EQ_NOT]: (oldValue = 0, value = 0) => oldValue !== value,
25
+ [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.LESS]: (oldValue = 0, value = 0) => oldValue < value,
26
+ [this.CONST.VARS_COUNTERS.COMPARE_ACTIONS.LESS_EQ]: (oldValue = 0, value = 0) => oldValue <= value,
27
+ };
28
+
29
+ subscribe = () => {
30
+ this.on(this.EVENTS.VARS_COUNTERS.CHANGE, this.onCounterChange);
31
+ this.on(this.EVENTS.VARS_COUNTERS.COMPARE, this.onCounterCompare);
32
+ };
33
+
34
+ onCounterChange = ({ value = 0, execValue = 0, action = "" }: CountersChangePayload = {}) => this.varsChangeHandlers[action]?.(value, execValue) ?? value;
35
+
36
+ onCounterCompare = ({ value = 0, execValue = 0, action = "" }: CountersComparePayload = {}) => this.varsCompareHandlers[action]?.(value, execValue) ?? false;
37
+ }
@@ -0,0 +1,42 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+
3
+ import type { LineExecHandlerArg } from "@vnejs/module.core";
4
+ import type { CountersSetPayload } from "@vnejs/contracts.vars.counters";
5
+
6
+ import type { CountersPluginConstants, CountersPluginEvents, CountersPluginParams, CountersPluginSettings } from "../types.js";
7
+
8
+ export class VarsCountersScenario extends ModuleCore<CountersPluginEvents, CountersPluginConstants, CountersPluginSettings, CountersPluginParams> {
9
+ name = "vars.counters.scenario";
10
+
11
+ init = () =>
12
+ Promise.all([
13
+ this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { block: this.CONST.VARS_COUNTERS.EXEC_NAME, handler: this.onLineExecBlock }),
14
+ this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { module: this.CONST.VARS_COUNTERS.EXEC_NAME, handler: this.onLineExecModule }),
15
+ ]);
16
+
17
+ onLineExecModule = async ({ line = "", keywords = [] }: LineExecHandlerArg = {}) => {
18
+ const {
19
+ name = "",
20
+ action = "",
21
+ execValue = 0,
22
+ value = 0,
23
+ isGlobal = false,
24
+ } = (await this.emitOne(this.EVENTS.VARS_COUNTERS.EXEC_INFO, { line, keywords })) ?? {};
25
+
26
+ const newValue = (await this.emitOne(this.EVENTS.VARS_COUNTERS.CHANGE, { value, execValue, action })) ?? 0;
27
+
28
+ await this.emit(this.EVENTS.VARS_COUNTERS.SET, { name, isGlobal, value: newValue } satisfies CountersSetPayload);
29
+
30
+ this.emitNext();
31
+ };
32
+
33
+ onLineExecBlock = async ({ line = "", keywords = [] }: LineExecHandlerArg = {}) => {
34
+ const { action = "", execValue = 0, value = 0 } = (await this.emitOne(this.EVENTS.VARS_COUNTERS.EXEC_INFO, { line, keywords })) ?? {};
35
+
36
+ const shouldGoInside = (await this.emitOne(this.EVENTS.VARS_COUNTERS.COMPARE, { value, execValue, action })) ?? false;
37
+
38
+ await this.emit(this.EVENTS.VARS.SCENARIO, { shouldGoInside });
39
+
40
+ this.emitNext();
41
+ };
42
+ }
@@ -11,30 +11,6 @@ describe("VarsCounters", () => {
11
11
  registerCountersPluginVne();
12
12
  });
13
13
 
14
- it("CHANGE applies increment action", async () => {
15
- const { observer } = createCountersTestModule(VarsCounters);
16
-
17
- const [result] = await observer.emit(SUBSCRIBE_EVENTS.CHANGE, {
18
- value: 5,
19
- execValue: 0,
20
- action: CONSTANTS.CHANGE_ACTIONS.INC,
21
- });
22
-
23
- expect(result).toBe(6);
24
- });
25
-
26
- it("COMPARE uses configured comparator", async () => {
27
- const { observer } = createCountersTestModule(VarsCounters);
28
-
29
- const [result] = await observer.emit(SUBSCRIBE_EVENTS.COMPARE, {
30
- value: 10,
31
- execValue: 5,
32
- action: CONSTANTS.COMPARE_ACTIONS.MORE,
33
- });
34
-
35
- expect(result).toBe(true);
36
- });
37
-
38
14
  it("SET delegates to VARS.SET with counter type", async () => {
39
15
  const { observer } = createCountersTestModule(VarsCounters);
40
16
  const set = spyEvent(observer, VARS_EVENTS.SET);
@@ -0,0 +1,43 @@
1
+ import { beforeEach, describe, expect, it } from "vitest";
2
+
3
+ import { stubEvent } from "@vnejs/test-utils";
4
+
5
+ import { VarsCountersExec } from "../modules/exec.js";
6
+ import { CONSTANTS, createCountersTestModule, registerCountersPluginVne, SUBSCRIBE_EVENTS } from "./setup.js";
7
+
8
+ describe("VarsCountersExec", () => {
9
+ beforeEach(() => {
10
+ registerCountersPluginVne();
11
+ });
12
+
13
+ it("EXEC_INFO parses line and fetches value", async () => {
14
+ const { observer } = createCountersTestModule(VarsCountersExec);
15
+
16
+ stubEvent(observer, SUBSCRIBE_EVENTS.GET, () => 7);
17
+
18
+ const [info] = await observer.emit(SUBSCRIBE_EVENTS.EXEC_INFO, { line: "gold add 3", keywords: [] });
19
+
20
+ expect(info).toEqual({ name: "gold", action: "add", isGlobal: false, execValue: 3, value: 7 });
21
+ });
22
+
23
+ it("EXEC_CHECK compares via COMPARE", async () => {
24
+ const { observer } = createCountersTestModule(VarsCountersExec);
25
+
26
+ stubEvent(observer, SUBSCRIBE_EVENTS.GET, () => 10);
27
+ stubEvent(observer, SUBSCRIBE_EVENTS.COMPARE, () => true);
28
+
29
+ const [result] = await observer.emit(SUBSCRIBE_EVENTS.EXEC_CHECK, { line: `gold ${CONSTANTS.COMPARE_ACTIONS.MORE} 5`, keywords: [] });
30
+
31
+ expect(result).toBe(true);
32
+ });
33
+
34
+ it("EXEC_GET returns stored value", async () => {
35
+ const { observer } = createCountersTestModule(VarsCountersExec);
36
+
37
+ stubEvent(observer, SUBSCRIBE_EVENTS.GET, () => 12);
38
+
39
+ const [result] = await observer.emit(SUBSCRIBE_EVENTS.EXEC_GET, { line: "gold", keywords: [] });
40
+
41
+ expect(result).toBe(12);
42
+ });
43
+ });
@@ -0,0 +1,34 @@
1
+ import { beforeEach, describe, expect, it } from "vitest";
2
+
3
+ import { VarsCountersHandlers } from "../modules/handlers.js";
4
+ import { CONSTANTS, createCountersTestModule, registerCountersPluginVne, SUBSCRIBE_EVENTS } from "./setup.js";
5
+
6
+ describe("VarsCountersHandlers", () => {
7
+ beforeEach(() => {
8
+ registerCountersPluginVne();
9
+ });
10
+
11
+ it("CHANGE applies increment action", async () => {
12
+ const { observer } = createCountersTestModule(VarsCountersHandlers);
13
+
14
+ const [result] = await observer.emit(SUBSCRIBE_EVENTS.CHANGE, {
15
+ value: 5,
16
+ execValue: 0,
17
+ action: CONSTANTS.CHANGE_ACTIONS.INC,
18
+ });
19
+
20
+ expect(result).toBe(6);
21
+ });
22
+
23
+ it("COMPARE uses configured comparator", async () => {
24
+ const { observer } = createCountersTestModule(VarsCountersHandlers);
25
+
26
+ const [result] = await observer.emit(SUBSCRIBE_EVENTS.COMPARE, {
27
+ value: 10,
28
+ execValue: 5,
29
+ action: CONSTANTS.COMPARE_ACTIONS.MORE,
30
+ });
31
+
32
+ expect(result).toBe(true);
33
+ });
34
+ });
@@ -1,46 +0,0 @@
1
- import type { ModuleGlobalStateRegistry } from "@vnejs/module.core";
2
- import type { VarValue, VarsGetPayload } from "@vnejs/contracts.vars";
3
- export type CountersSetPayload = {
4
- name?: string;
5
- isGlobal?: boolean;
6
- value?: number;
7
- };
8
- export type CountersGetPayload = {
9
- name?: string;
10
- isGlobal?: boolean;
11
- state?: VarsGetPayload["state"];
12
- };
13
- export type CountersChangePayload = {
14
- value?: number;
15
- execValue?: number;
16
- action?: string;
17
- };
18
- export type CountersComparePayload = {
19
- value?: number;
20
- execValue?: number;
21
- action?: string;
22
- };
23
- export type CountersExecInfoPayload = {
24
- line?: string;
25
- keywords?: string[];
26
- state?: ModuleGlobalStateRegistry | null;
27
- withValue?: boolean;
28
- };
29
- export type CountersExecInfoResult = {
30
- name: string;
31
- action: string;
32
- isGlobal: boolean;
33
- execValue: number;
34
- value?: number;
35
- };
36
- export type CountersExecCheckPayload = {
37
- line?: string;
38
- keywords?: string[];
39
- state?: ModuleGlobalStateRegistry | null;
40
- };
41
- export type VarsSetPayload = {
42
- name?: string;
43
- type?: string;
44
- value?: VarValue;
45
- isGlobal?: boolean;
46
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1,54 +0,0 @@
1
- import type { ModuleGlobalStateRegistry } from "@vnejs/module.core";
2
- import type { VarValue, VarsGetPayload } from "@vnejs/contracts.vars";
3
-
4
- export type CountersSetPayload = {
5
- name?: string;
6
- isGlobal?: boolean;
7
- value?: number;
8
- };
9
-
10
- export type CountersGetPayload = {
11
- name?: string;
12
- isGlobal?: boolean;
13
- state?: VarsGetPayload["state"];
14
- };
15
-
16
- export type CountersChangePayload = {
17
- value?: number;
18
- execValue?: number;
19
- action?: string;
20
- };
21
-
22
- export type CountersComparePayload = {
23
- value?: number;
24
- execValue?: number;
25
- action?: string;
26
- };
27
-
28
- export type CountersExecInfoPayload = {
29
- line?: string;
30
- keywords?: string[];
31
- state?: ModuleGlobalStateRegistry | null;
32
- withValue?: boolean;
33
- };
34
-
35
- export type CountersExecInfoResult = {
36
- name: string;
37
- action: string;
38
- isGlobal: boolean;
39
- execValue: number;
40
- value?: number;
41
- };
42
-
43
- export type CountersExecCheckPayload = {
44
- line?: string;
45
- keywords?: string[];
46
- state?: ModuleGlobalStateRegistry | null;
47
- };
48
-
49
- export type VarsSetPayload = {
50
- name?: string;
51
- type?: string;
52
- value?: VarValue;
53
- isGlobal?: boolean;
54
- };