@vnejs/plugins.vars.counters 0.2.1 → 0.2.3

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
@@ -1,6 +1,8 @@
1
1
  import "@vnejs/contracts.vars.counters";
2
2
  import { regPlugin } from "@vnejs/shared";
3
3
  import { CONSTANTS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/contracts.vars.counters";
4
- import { Counters } from "./modules/counters.js";
5
- import { CountersExec } from "./modules/exec.js";
6
- regPlugin(PLUGIN_NAME, { constants: CONSTANTS, events: SUBSCRIBE_EVENTS }, [Counters, CountersExec]);
4
+ import { VarsCounters } from "./modules/counters.js";
5
+ import { VarsCountersExec } from "./modules/exec.js";
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
2
  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
- export declare class Counters extends ModuleCore<CountersPluginEvents, CountersPluginConstants, CountersPluginSettings, CountersPluginParams> {
3
+ import type { CountersGetPayload, CountersSetPayload } from "../utils/counters.js";
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
7
  init: () => Promise<unknown[]> | undefined;
12
8
  onCounterSet: ({ name, isGlobal, value }?: CountersSetPayload) => Promise<unknown[]> | undefined;
13
9
  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>;
19
10
  }
20
- export {};
@@ -1,51 +1,11 @@
1
1
  import { ModuleCore } from "@vnejs/module.core";
2
- import { tokenizeExecLine } from "@vnejs/helpers";
3
- export class Counters extends ModuleCore {
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
10
  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;
50
- };
51
11
  }
@@ -1,9 +1,10 @@
1
1
  import { ModuleCore } from "@vnejs/module.core";
2
- import type { LineExecHandlerArg } from "@vnejs/module.core";
3
2
  import type { CountersPluginConstants, CountersPluginEvents, CountersPluginParams, CountersPluginSettings } from "../types.js";
4
- export declare class CountersExec extends ModuleCore<CountersPluginEvents, CountersPluginConstants, CountersPluginSettings, CountersPluginParams> {
3
+ import type { CountersExecCheckPayload, CountersExecInfoPayload, CountersExecInfoResult } from "../utils/counters.js";
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<unknown>;
9
+ onCounterExecGet: ({ line, keywords, state }?: CountersExecCheckPayload) => Promise<number>;
9
10
  }
@@ -1,21 +1,27 @@
1
1
  import { ModuleCore } from "@vnejs/module.core";
2
- export class CountersExec extends ModuleCore {
2
+ import { tokenizeExecLine } from "@vnejs/helpers";
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: Number(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 { CountersPluginConstants, CountersPluginEvents, CountersPluginParams, CountersPluginSettings } from "../types.js";
3
+ import type { CountersChangePayload, CountersComparePayload } from "../utils/counters.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<[unknown[] | undefined, unknown[] | undefined]>;
7
+ onLineExecModule: ({ line, keywords }?: LineExecHandlerArg) => Promise<void>;
8
+ onLineExecBlock: ({ line, keywords }?: LineExecHandlerArg) => Promise<void>;
9
+ }
@@ -0,0 +1,21 @@
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 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();
14
+ };
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();
20
+ };
21
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vnejs/plugins.vars.counters",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -3,7 +3,9 @@ import "@vnejs/contracts.vars.counters";
3
3
  import { regPlugin } from "@vnejs/shared";
4
4
  import { CONSTANTS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/contracts.vars.counters";
5
5
 
6
- import { Counters } from "./modules/counters.js";
7
- import { CountersExec } from "./modules/exec.js";
6
+ import { VarsCounters } from "./modules/counters.js";
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 }, [Counters, CountersExec]);
11
+ regPlugin(PLUGIN_NAME, { constants: CONSTANTS, events: SUBSCRIBE_EVENTS }, [VarsCounters, VarsCountersHandlers, VarsCountersExec, VarsCountersScenario]);
@@ -1,49 +1,14 @@
1
1
  import { ModuleCore } from "@vnejs/module.core";
2
- import { tokenizeExecLine } from "@vnejs/helpers";
3
2
 
4
3
  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";
4
+ import type { CountersGetPayload, CountersSetPayload, VarsSetPayload } from "../utils/counters.js";
15
5
 
16
- type ChangeHandler = (oldValue?: number, value?: number) => number;
17
- type CompareHandler = (oldValue?: number, value?: number) => boolean;
18
-
19
- export class Counters extends ModuleCore<CountersPluginEvents, CountersPluginConstants, CountersPluginSettings, CountersPluginParams> {
6
+ export class VarsCounters extends ModuleCore<CountersPluginEvents, CountersPluginConstants, CountersPluginSettings, CountersPluginParams> {
20
7
  name = "vars.counters";
21
8
 
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
9
  subscribe = () => {
40
10
  this.on(this.EVENTS.VARS_COUNTERS.SET, this.onCounterSet);
41
11
  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
12
  };
48
13
 
49
14
  init = () => this.emit(this.EVENTS.VARS.DEFAULT_REG, { type: this.CONST.VARS_COUNTERS.TYPE, value: 0 });
@@ -53,35 +18,4 @@ export class Counters extends ModuleCore<CountersPluginEvents, CountersPluginCon
53
18
 
54
19
  onCounterGet = ({ name = "", isGlobal = false, state = null }: CountersGetPayload = {}) =>
55
20
  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;
84
-
85
- return value;
86
- };
87
21
  }
@@ -1,37 +1,42 @@
1
1
  import { ModuleCore } from "@vnejs/module.core";
2
-
3
- import type { LineExecHandlerArg } from "@vnejs/module.core";
2
+ import { tokenizeExecLine } from "@vnejs/helpers";
4
3
 
5
4
  import type { CountersPluginConstants, CountersPluginEvents, CountersPluginParams, CountersPluginSettings } from "../types.js";
6
- import type { CountersExecInfoResult, CountersSetPayload } from "../utils/counters.js";
5
+ import type { CountersExecCheckPayload, CountersExecInfoPayload, CountersExecInfoResult } from "../utils/counters.js";
7
6
 
8
- export class CountersExec extends ModuleCore<CountersPluginEvents, CountersPluginConstants, CountersPluginSettings, CountersPluginParams> {
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: Number(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 {
29
+ action = "",
30
+ execValue = 0,
31
+ value = 0,
32
+ } = (await this.emitOne(this.EVENTS.VARS_COUNTERS.EXEC_INFO, { line, keywords, state })) as CountersExecInfoResult;
30
33
 
31
- const shouldGoInside = Boolean(await this.emitOne(this.EVENTS.VARS_COUNTERS.COMPARE, { value, execValue, action }));
34
+ return this.emitOne(this.EVENTS.VARS_COUNTERS.COMPARE, { value, execValue, action });
35
+ };
32
36
 
33
- await this.emit(this.EVENTS.VARS.SCENARIO, { shouldGoInside });
37
+ onCounterExecGet = async ({ line = "", keywords = [], state = null }: CountersExecCheckPayload = {}) => {
38
+ const { value = 0 } = (await this.emitOne(this.EVENTS.VARS_COUNTERS.EXEC_INFO, { line, keywords, state })) as CountersExecInfoResult;
34
39
 
35
- this.emitNext();
40
+ return value;
36
41
  };
37
42
  }
@@ -0,0 +1,37 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+
3
+ import type { CountersPluginConstants, CountersPluginEvents, CountersPluginParams, CountersPluginSettings } from "../types.js";
4
+ import type { CountersChangePayload, CountersComparePayload } from "../utils/counters.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,37 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+
3
+ import type { LineExecHandlerArg } from "@vnejs/module.core";
4
+
5
+ import type { CountersPluginConstants, CountersPluginEvents, CountersPluginParams, CountersPluginSettings } from "../types.js";
6
+ import type { CountersExecInfoResult, CountersSetPayload } from "../utils/counters.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 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 }));
22
+
23
+ await this.emit(this.EVENTS.VARS_COUNTERS.SET, { name, isGlobal, value: newValue } satisfies CountersSetPayload);
24
+
25
+ this.emitNext();
26
+ };
27
+
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;
30
+
31
+ const shouldGoInside = Boolean(await this.emitOne(this.EVENTS.VARS_COUNTERS.COMPARE, { value, execValue, action }));
32
+
33
+ await this.emit(this.EVENTS.VARS.SCENARIO, { shouldGoInside });
34
+
35
+ this.emitNext();
36
+ };
37
+ }
@@ -3,40 +3,16 @@ import { beforeEach, describe, expect, it } from "vitest";
3
3
  import { SUBSCRIBE_EVENTS as VARS_EVENTS } from "@vnejs/contracts.vars";
4
4
  import { spyEvent } from "@vnejs/test-utils";
5
5
 
6
- import { Counters } from "../modules/counters.js";
6
+ import { VarsCounters } from "../modules/counters.js";
7
7
  import { CONSTANTS, createCountersTestModule, registerCountersPluginVne, SUBSCRIBE_EVENTS } from "./setup.js";
8
8
 
9
- describe("Counters", () => {
9
+ describe("VarsCounters", () => {
10
10
  beforeEach(() => {
11
11
  registerCountersPluginVne();
12
12
  });
13
13
 
14
- it("CHANGE applies increment action", async () => {
15
- const { observer } = createCountersTestModule(Counters);
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(Counters);
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
- const { observer } = createCountersTestModule(Counters);
15
+ const { observer } = createCountersTestModule(VarsCounters);
40
16
  const set = spyEvent(observer, VARS_EVENTS.SET);
41
17
 
42
18
  await observer.emit(SUBSCRIBE_EVENTS.SET, { name: "gold", value: 3 });
@@ -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
+ });