@vnejs/plugins.scenario.fastforward 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,15 @@
1
1
  import "@vnejs/contracts.scenario.fastforward";
2
2
  import { regPlugin } from "@vnejs/shared";
3
3
  import { CONSTANTS, PARAMS, PLUGIN_NAME, SETTINGS_KEYS, SUBSCRIBE_EVENTS } from "@vnejs/contracts.scenario.fastforward";
4
+ import { FastforwardEmit } from "./modules/emit.js";
4
5
  import { Fastforward } from "./modules/fastforward.js";
6
+ import { FastforwardScenario } from "./modules/scenario.js";
7
+ import { FastforwardStorage } from "./modules/storage.js";
5
8
  import { FastforwardVisits } from "./modules/visits.js";
6
- regPlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, settings: SETTINGS_KEYS, params: PARAMS, constants: CONSTANTS }, [Fastforward, FastforwardVisits]);
9
+ regPlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, settings: SETTINGS_KEYS, params: PARAMS, constants: CONSTANTS }, [
10
+ Fastforward,
11
+ FastforwardEmit,
12
+ FastforwardScenario,
13
+ FastforwardVisits,
14
+ FastforwardStorage,
15
+ ]);
@@ -0,0 +1,8 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import type { FastforwardPluginConstants, FastforwardPluginEvents, FastforwardPluginParams, FastforwardPluginSettings } from "../types.js";
3
+ export declare class FastforwardEmit extends ModuleCore<FastforwardPluginEvents, FastforwardPluginConstants, FastforwardPluginSettings, FastforwardPluginParams> {
4
+ name: string;
5
+ subscribe: () => void;
6
+ onFastforwardEmit: () => Promise<void>;
7
+ onFastforwardEmitCheck: () => number;
8
+ }
@@ -0,0 +1,18 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ export class FastforwardEmit extends ModuleCore {
3
+ name = "fastforward.emit";
4
+ subscribe = () => {
5
+ this.on(this.EVENTS.FASTFORWARD.EMIT, this.onFastforwardEmit);
6
+ this.on(this.EVENTS.FASTFORWARD.EMIT_CHECK, this.onFastforwardEmitCheck);
7
+ };
8
+ onFastforwardEmit = async () => {
9
+ if (!this.shared.settings[this.SETTINGS.FASTFORWARD.ENABLED])
10
+ return;
11
+ const checkResults = ((await this.emit(this.EVENTS.FASTFORWARD.CHECK)) ?? []);
12
+ if (!checkResults.every((r) => r.isAllowed))
13
+ await this.emit(this.EVENTS.FASTFORWARD.STOP);
14
+ await this.emit(this.EVENTS.INTERACT.EMIT);
15
+ await this.emit(this.EVENTS.FASTFORWARD.EMIT_CHECK);
16
+ };
17
+ onFastforwardEmitCheck = () => setTimeout(() => this.shared.isFastforwardOn && this.emit(this.EVENTS.FASTFORWARD.EMIT), this.shared.settings[this.SETTINGS.FASTFORWARD.DELAY]);
18
+ }
@@ -12,7 +12,5 @@ export declare class Fastforward extends ModuleCore<FastforwardPluginEvents, Fas
12
12
  };
13
13
  onStart: () => void;
14
14
  onStop: () => void;
15
- onFastforwardEmit: () => Promise<void>;
16
- onFastforwardEmitCheck: () => number;
17
15
  setValue: (value: boolean) => void;
18
16
  }
@@ -7,33 +7,23 @@ export class Fastforward extends ModuleCore {
7
7
  this.on(this.EVENTS.FASTFORWARD.START, this.onStart);
8
8
  this.on(this.EVENTS.FASTFORWARD.CHECK, this.onCheck);
9
9
  this.on(this.EVENTS.FASTFORWARD.TOGGLE, this.onToggle);
10
- this.on(this.EVENTS.FASTFORWARD.EMIT, this.onFastforwardEmit);
11
- this.on(this.EVENTS.FASTFORWARD.EMIT_CHECK, this.onFastforwardEmitCheck);
12
- this.on(this.EVENTS.SYSTEM.STARTED, this.emitStop);
13
10
  this.on(this.EVENTS.STATE.CLEAR, this.emitStop);
14
11
  };
15
- init = () => Promise.all([
16
- this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.FASTFORWARD.DELAY, value: this.PARAMS.FASTFORWARD.DEFAULT_DELAY }),
17
- this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.FASTFORWARD.ENABLED, value: this.PARAMS.FASTFORWARD.DEFAULT_ENABLED }),
18
- ]);
12
+ init = () => {
13
+ this.setValue(false);
14
+ return Promise.all([
15
+ this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.FASTFORWARD.DELAY, value: this.PARAMS.FASTFORWARD.DEFAULT_DELAY }),
16
+ this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.FASTFORWARD.ENABLED, value: this.PARAMS.FASTFORWARD.DEFAULT_ENABLED }),
17
+ ]);
18
+ };
19
19
  onToggle = () => this.setValue(!this.shared.isFastforwardOn);
20
20
  onCheck = () => ({ isAllowed: this.shared.settings[this.SETTINGS.FASTFORWARD.ENABLED], source: this.name });
21
21
  onStart = () => this.setValue(true);
22
22
  onStop = () => this.setValue(false);
23
- onFastforwardEmit = async () => {
24
- if (!this.shared.settings[this.SETTINGS.FASTFORWARD.ENABLED])
25
- return;
26
- const checkResults = ((await this.emit(this.EVENTS.FASTFORWARD.CHECK)) ?? []);
27
- if (!checkResults.every((r) => r.isAllowed))
28
- this.setValue(false);
29
- await this.emit(this.EVENTS.INTERACT.EMIT);
30
- await this.emit(this.EVENTS.FASTFORWARD.EMIT_CHECK);
31
- };
32
- onFastforwardEmitCheck = () => setTimeout(() => this.shared.isFastforwardOn && this.emit(this.EVENTS.FASTFORWARD.EMIT), this.shared.settings[this.SETTINGS.FASTFORWARD.DELAY]);
33
23
  setValue = (value) => {
34
24
  this.shared.isFastforwardOn = value;
35
- this.shared.mediaNoTimeoutSources.set(this.name, value);
36
- this.shared.viewForceAnimationSources.set(this.name, value);
25
+ this.shared.mediaNoTimeoutSources?.set(this.name, value);
26
+ this.shared.viewForceAnimationSources?.set(this.name, value);
37
27
  this.emit(this.EVENTS.FASTFORWARD.CHANGED, { value });
38
28
  if (value)
39
29
  this.emit(this.EVENTS.FASTFORWARD.EMIT);
@@ -0,0 +1,9 @@
1
+ import "@vnejs/contracts.core.scenario";
2
+ import { ModuleCore } from "@vnejs/module.core";
3
+ import type { ScenarioExecPayload } from "@vnejs/contracts.core.scenario";
4
+ import type { FastforwardPluginConstants, FastforwardPluginEvents, FastforwardPluginParams, FastforwardPluginSettings } from "../types.js";
5
+ export declare class FastforwardScenario extends ModuleCore<FastforwardPluginEvents, FastforwardPluginConstants, FastforwardPluginSettings, FastforwardPluginParams> {
6
+ name: string;
7
+ subscribe: () => void;
8
+ onExec: (payload?: ScenarioExecPayload) => Promise<void>;
9
+ }
@@ -0,0 +1,14 @@
1
+ import "@vnejs/contracts.core.scenario";
2
+ import { hashSha256 } from "@vnejs/helpers.hash-sha256";
3
+ import { ModuleCore } from "@vnejs/module.core";
4
+ export class FastforwardScenario extends ModuleCore {
5
+ name = "fastforward.scenario";
6
+ subscribe = () => {
7
+ this.on(this.EVENTS.SCENARIO.EXEC, this.onExec);
8
+ };
9
+ onExec = async (payload = {}) => {
10
+ const key = await hashSha256(this.globalState["fastforward.visits"]);
11
+ const group = this.globalState["fastforward.visits"].label || this.globalState.scenario.label;
12
+ await this.emit(this.EVENTS.FASTFORWARD.VISIT_ADD, { ...payload, key, group });
13
+ };
14
+ }
@@ -0,0 +1,10 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import type { FastforwardVisitAddPayload } from "@vnejs/contracts.scenario.fastforward";
3
+ import type { FastforwardPluginConstants, FastforwardPluginEvents, FastforwardPluginParams, FastforwardPluginSettings } from "../types.js";
4
+ export declare class FastforwardStorage extends ModuleCore<FastforwardPluginEvents, FastforwardPluginConstants, FastforwardPluginSettings, FastforwardPluginParams> {
5
+ name: string;
6
+ subscribe: () => void;
7
+ init: () => Promise<void>;
8
+ onVisitAdd: ({ key, group }?: FastforwardVisitAddPayload) => Promise<unknown[]> | undefined;
9
+ onClear: () => Promise<unknown[]> | undefined;
10
+ }
@@ -0,0 +1,22 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ const STORAGE_VALUE = 1;
3
+ const toVisitKey = (key) => {
4
+ const sep = key.lastIndexOf("/");
5
+ return sep === -1 ? key : key.slice(sep + 1);
6
+ };
7
+ export class FastforwardStorage extends ModuleCore {
8
+ name = "fastforward.storage";
9
+ subscribe = () => {
10
+ this.on(this.EVENTS.FASTFORWARD.VISIT_ADD, this.onVisitAdd);
11
+ this.on(this.EVENTS.FASTFORWARD.CLEAR, this.onClear);
12
+ };
13
+ init = async () => {
14
+ const stored = (await this.emitOne(this.EVENTS.STORAGE.GET_ALL, { module: this.name }));
15
+ this.shared.fastforwardStorage = new Set(Object.keys(stored).map(toVisitKey));
16
+ };
17
+ onVisitAdd = ({ key = "", group = "" } = {}) => this.emit(this.EVENTS.STORAGE.SET, { module: this.name, group, key, value: STORAGE_VALUE });
18
+ onClear = () => {
19
+ this.shared.fastforwardStorage.clear();
20
+ return this.emit(this.EVENTS.STORAGE.RM_ALL, { module: this.name });
21
+ };
22
+ }
@@ -1,19 +1,14 @@
1
- import "@vnejs/contracts.core.scenario";
2
1
  import { ModuleCore } from "@vnejs/module.core";
3
- import type { ScenarioExecPayload } from "@vnejs/contracts.core.scenario";
4
- import type { ModuleGlobalStateFastforwardVisits } from "@vnejs/contracts.scenario.fastforward";
2
+ import type { FastforwardVisitAddPayload, ModuleGlobalStateFastforwardVisits } from "@vnejs/contracts.scenario.fastforward";
5
3
  import type { FastforwardPluginConstants, FastforwardPluginEvents, FastforwardPluginParams, FastforwardPluginSettings } from "../types.js";
6
4
  export declare class FastforwardVisits extends ModuleCore<FastforwardPluginEvents, FastforwardPluginConstants, FastforwardPluginSettings, FastforwardPluginParams, ModuleGlobalStateFastforwardVisits> {
7
5
  name: string;
8
- visits: Set<string>;
9
6
  subscribe: () => void;
10
- init: () => Promise<void>;
11
- onExec: ({ module, block, line, args, keywords }?: ScenarioExecPayload) => Promise<void>;
7
+ init: () => Promise<unknown[]> | undefined;
12
8
  onVisitCheck: () => Promise<{
13
9
  isAllowed: boolean;
14
10
  source: string;
15
11
  }>;
16
- onClear: () => Promise<unknown[]> | undefined;
17
- getHash: () => Promise<unknown> | undefined;
12
+ onVisitAdd: ({ key, module, block, line, args, keywords }?: FastforwardVisitAddPayload) => void;
18
13
  getDefaultState: () => ModuleGlobalStateFastforwardVisits;
19
14
  }
@@ -1,40 +1,21 @@
1
- import "@vnejs/contracts.core.scenario";
1
+ import { hashSha256 } from "@vnejs/helpers.hash-sha256";
2
2
  import { ModuleCore } from "@vnejs/module.core";
3
- const STORAGE_VALUE = 1;
4
- const toVisitKey = (key) => {
5
- const sep = key.lastIndexOf("/");
6
- return sep === -1 ? key : key.slice(sep + 1);
7
- };
8
3
  export class FastforwardVisits extends ModuleCore {
9
4
  name = "fastforward.visits";
10
- visits = new Set();
11
5
  subscribe = () => {
12
- this.on(this.EVENTS.SCENARIO.EXEC, this.onExec);
13
6
  this.on(this.EVENTS.FASTFORWARD.CHECK, this.onVisitCheck);
14
- this.on(this.EVENTS.FASTFORWARD.CLEAR, this.onClear);
15
- this.on(this.EVENTS.STATE.CLEAR, this.setDefaultState);
7
+ this.on(this.EVENTS.FASTFORWARD.VISIT_ADD, this.onVisitAdd);
16
8
  this.on(this.EVENTS.STATE.SET, this.onSetStateClone);
9
+ this.on(this.EVENTS.STATE.CLEAR, this.setDefaultState);
17
10
  };
18
- init = async () => {
19
- await this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.FASTFORWARD.CHECK_VISITED, value: this.PARAMS.FASTFORWARD.DEFAULT_CHECK_VISITED });
20
- const stored = (await this.emitOne(this.EVENTS.STORAGE.GET_ALL, { module: this.name }));
21
- this.visits = new Set(Object.keys(stored).map(toVisitKey));
22
- };
23
- onExec = async ({ module, block, line, args = {}, keywords = [] } = {}) => {
24
- const key = (await this.emitOne(this.EVENTS.VENDORS.HASH, this.state));
25
- const group = this.state.label || this.globalState.scenario.label;
26
- this.visits.add(key);
27
- this.emit(this.EVENTS.STORAGE.SET, { module: this.name, group, key, value: STORAGE_VALUE });
28
- this.setState({ module, block, line, args, keywords, label: this.globalState.scenario.label });
29
- };
11
+ init = () => this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.FASTFORWARD.CHECK_VISITED, value: this.PARAMS.FASTFORWARD.DEFAULT_CHECK_VISITED });
30
12
  onVisitCheck = async () => {
31
- const isAllowed = !this.shared.settings[this.SETTINGS.FASTFORWARD.CHECK_VISITED] || this.visits.has((await this.getHash()));
13
+ const isAllowed = !this.shared.settings[this.SETTINGS.FASTFORWARD.CHECK_VISITED] || this.shared.fastforwardStorage.has((await hashSha256(this.state)));
32
14
  return { isAllowed, source: this.name };
33
15
  };
34
- onClear = () => {
35
- this.visits.clear();
36
- return this.emit(this.EVENTS.STORAGE.RM_ALL, { module: this.name });
16
+ onVisitAdd = ({ key = "", module, block, line, args = {}, keywords = [] } = {}) => {
17
+ this.shared.fastforwardStorage.add(key);
18
+ this.setState({ module, block, line, args, keywords, label: this.globalState.scenario.label });
37
19
  };
38
- getHash = () => this.emitOne(this.EVENTS.VENDORS.HASH, this.state);
39
20
  getDefaultState = () => ({ label: "", args: {}, keywords: [] });
40
21
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vnejs/plugins.scenario.fastforward",
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",
@@ -30,7 +30,8 @@
30
30
  "author": "",
31
31
  "license": "ISC",
32
32
  "dependencies": {
33
- "@vnejs/contracts.scenario.fastforward": "~0.2.0"
33
+ "@vnejs/contracts.scenario.fastforward": "~0.2.0",
34
+ "@vnejs/helpers.hash-sha256": "~0.2.0"
34
35
  },
35
36
  "peerDependencies": {
36
37
  "@vnejs/module.core": "~0.2.0",
package/src/index.ts CHANGED
@@ -1,10 +1,18 @@
1
-
2
1
  import "@vnejs/contracts.scenario.fastforward";
3
2
 
4
3
  import { regPlugin } from "@vnejs/shared";
5
4
  import { CONSTANTS, PARAMS, PLUGIN_NAME, SETTINGS_KEYS, SUBSCRIBE_EVENTS } from "@vnejs/contracts.scenario.fastforward";
6
5
 
6
+ import { FastforwardEmit } from "./modules/emit.js";
7
7
  import { Fastforward } from "./modules/fastforward.js";
8
+ import { FastforwardScenario } from "./modules/scenario.js";
9
+ import { FastforwardStorage } from "./modules/storage.js";
8
10
  import { FastforwardVisits } from "./modules/visits.js";
9
11
 
10
- regPlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, settings: SETTINGS_KEYS, params: PARAMS, constants: CONSTANTS }, [Fastforward, FastforwardVisits]);
12
+ regPlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, settings: SETTINGS_KEYS, params: PARAMS, constants: CONSTANTS }, [
13
+ Fastforward,
14
+ FastforwardEmit,
15
+ FastforwardScenario,
16
+ FastforwardVisits,
17
+ FastforwardStorage,
18
+ ]);
@@ -0,0 +1,29 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+
3
+ import type { FastforwardCheckResult } from "@vnejs/contracts.scenario.fastforward";
4
+
5
+ import type { FastforwardPluginConstants, FastforwardPluginEvents, FastforwardPluginParams, FastforwardPluginSettings } from "../types.js";
6
+
7
+ export class FastforwardEmit extends ModuleCore<FastforwardPluginEvents, FastforwardPluginConstants, FastforwardPluginSettings, FastforwardPluginParams> {
8
+ name = "fastforward.emit";
9
+
10
+ subscribe = () => {
11
+ this.on(this.EVENTS.FASTFORWARD.EMIT, this.onFastforwardEmit);
12
+ this.on(this.EVENTS.FASTFORWARD.EMIT_CHECK, this.onFastforwardEmitCheck);
13
+ };
14
+
15
+ onFastforwardEmit = async () => {
16
+ if (!this.shared.settings[this.SETTINGS.FASTFORWARD.ENABLED]) return;
17
+
18
+ const checkResults = ((await this.emit(this.EVENTS.FASTFORWARD.CHECK)) ?? []) as FastforwardCheckResult[];
19
+
20
+ if (!checkResults.every((r) => r.isAllowed)) await this.emit(this.EVENTS.FASTFORWARD.STOP);
21
+
22
+ await this.emit(this.EVENTS.INTERACT.EMIT);
23
+
24
+ await this.emit(this.EVENTS.FASTFORWARD.EMIT_CHECK);
25
+ };
26
+
27
+ onFastforwardEmitCheck = () =>
28
+ setTimeout(() => this.shared.isFastforwardOn && this.emit(this.EVENTS.FASTFORWARD.EMIT), this.shared.settings[this.SETTINGS.FASTFORWARD.DELAY]);
29
+ }
@@ -15,44 +15,28 @@ export class Fastforward extends ModuleCore<FastforwardPluginEvents, Fastforward
15
15
  this.on(this.EVENTS.FASTFORWARD.CHECK, this.onCheck);
16
16
  this.on(this.EVENTS.FASTFORWARD.TOGGLE, this.onToggle);
17
17
 
18
- this.on(this.EVENTS.FASTFORWARD.EMIT, this.onFastforwardEmit);
19
- this.on(this.EVENTS.FASTFORWARD.EMIT_CHECK, this.onFastforwardEmitCheck);
20
-
21
- this.on(this.EVENTS.SYSTEM.STARTED, this.emitStop);
22
18
  this.on(this.EVENTS.STATE.CLEAR, this.emitStop);
23
19
  };
24
20
 
25
- init = () =>
26
- Promise.all([
21
+ init = () => {
22
+ this.setValue(false);
23
+
24
+ return Promise.all([
27
25
  this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.FASTFORWARD.DELAY, value: this.PARAMS.FASTFORWARD.DEFAULT_DELAY }),
28
26
  this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.FASTFORWARD.ENABLED, value: this.PARAMS.FASTFORWARD.DEFAULT_ENABLED }),
29
27
  ]);
28
+ };
30
29
 
31
30
  onToggle = () => this.setValue(!this.shared.isFastforwardOn);
32
31
  onCheck = () => ({ isAllowed: this.shared.settings[this.SETTINGS.FASTFORWARD.ENABLED], source: this.name }) satisfies FastforwardCheckResult;
33
32
  onStart = () => this.setValue(true);
34
33
  onStop = () => this.setValue(false);
35
34
 
36
- onFastforwardEmit = async () => {
37
- if (!this.shared.settings[this.SETTINGS.FASTFORWARD.ENABLED]) return;
38
-
39
- const checkResults = ((await this.emit(this.EVENTS.FASTFORWARD.CHECK)) ?? []) as FastforwardCheckResult[];
40
-
41
- if (!checkResults.every((r) => r.isAllowed)) this.setValue(false);
42
-
43
- await this.emit(this.EVENTS.INTERACT.EMIT);
44
-
45
- await this.emit(this.EVENTS.FASTFORWARD.EMIT_CHECK);
46
- };
47
-
48
- onFastforwardEmitCheck = () =>
49
- setTimeout(() => this.shared.isFastforwardOn && this.emit(this.EVENTS.FASTFORWARD.EMIT), this.shared.settings[this.SETTINGS.FASTFORWARD.DELAY]);
50
-
51
35
  setValue = (value: boolean) => {
52
36
  this.shared.isFastforwardOn = value;
53
37
 
54
- this.shared.mediaNoTimeoutSources.set(this.name, value);
55
- this.shared.viewForceAnimationSources.set(this.name, value);
38
+ this.shared.mediaNoTimeoutSources?.set(this.name, value);
39
+ this.shared.viewForceAnimationSources?.set(this.name, value);
56
40
 
57
41
  this.emit(this.EVENTS.FASTFORWARD.CHANGED, { value } satisfies FastforwardChangedPayload);
58
42
 
@@ -0,0 +1,23 @@
1
+ import "@vnejs/contracts.core.scenario";
2
+
3
+ import { hashSha256 } from "@vnejs/helpers.hash-sha256";
4
+ import { ModuleCore } from "@vnejs/module.core";
5
+ import type { ScenarioExecPayload } from "@vnejs/contracts.core.scenario";
6
+ import type { FastforwardVisitAddPayload } from "@vnejs/contracts.scenario.fastforward";
7
+
8
+ import type { FastforwardPluginConstants, FastforwardPluginEvents, FastforwardPluginParams, FastforwardPluginSettings } from "../types.js";
9
+
10
+ export class FastforwardScenario extends ModuleCore<FastforwardPluginEvents, FastforwardPluginConstants, FastforwardPluginSettings, FastforwardPluginParams> {
11
+ name = "fastforward.scenario";
12
+
13
+ subscribe = () => {
14
+ this.on(this.EVENTS.SCENARIO.EXEC, this.onExec);
15
+ };
16
+
17
+ onExec = async (payload: ScenarioExecPayload = {}) => {
18
+ const key = await hashSha256(this.globalState["fastforward.visits"]);
19
+ const group = this.globalState["fastforward.visits"].label || this.globalState.scenario.label;
20
+
21
+ await this.emit(this.EVENTS.FASTFORWARD.VISIT_ADD, { ...payload, key, group } satisfies FastforwardVisitAddPayload);
22
+ };
23
+ }
@@ -0,0 +1,36 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import type { FastforwardVisitAddPayload } from "@vnejs/contracts.scenario.fastforward";
3
+
4
+ import type { FastforwardPluginConstants, FastforwardPluginEvents, FastforwardPluginParams, FastforwardPluginSettings } from "../types.js";
5
+
6
+ const STORAGE_VALUE = 1;
7
+
8
+ const toVisitKey = (key: string) => {
9
+ const sep = key.lastIndexOf("/");
10
+
11
+ return sep === -1 ? key : key.slice(sep + 1);
12
+ };
13
+
14
+ export class FastforwardStorage extends ModuleCore<FastforwardPluginEvents, FastforwardPluginConstants, FastforwardPluginSettings, FastforwardPluginParams> {
15
+ name = "fastforward.storage";
16
+
17
+ subscribe = () => {
18
+ this.on(this.EVENTS.FASTFORWARD.VISIT_ADD, this.onVisitAdd);
19
+ this.on(this.EVENTS.FASTFORWARD.CLEAR, this.onClear);
20
+ };
21
+
22
+ init = async () => {
23
+ const stored = (await this.emitOne(this.EVENTS.STORAGE.GET_ALL, { module: this.name })) as Record<string, number>;
24
+
25
+ this.shared.fastforwardStorage = new Set(Object.keys(stored).map(toVisitKey));
26
+ };
27
+
28
+ onVisitAdd = ({ key = "", group = "" }: FastforwardVisitAddPayload = {}) =>
29
+ this.emit(this.EVENTS.STORAGE.SET, { module: this.name, group, key, value: STORAGE_VALUE });
30
+
31
+ onClear = () => {
32
+ this.shared.fastforwardStorage.clear();
33
+
34
+ return this.emit(this.EVENTS.STORAGE.RM_ALL, { module: this.name });
35
+ };
36
+ }
@@ -1,19 +1,9 @@
1
- import "@vnejs/contracts.core.scenario";
2
-
1
+ import { hashSha256 } from "@vnejs/helpers.hash-sha256";
3
2
  import { ModuleCore } from "@vnejs/module.core";
4
- import type { ScenarioExecPayload } from "@vnejs/contracts.core.scenario";
5
- import type { FastforwardCheckResult, ModuleGlobalStateFastforwardVisits } from "@vnejs/contracts.scenario.fastforward";
3
+ import type { FastforwardCheckResult, FastforwardVisitAddPayload, ModuleGlobalStateFastforwardVisits } from "@vnejs/contracts.scenario.fastforward";
6
4
 
7
5
  import type { FastforwardPluginConstants, FastforwardPluginEvents, FastforwardPluginParams, FastforwardPluginSettings } from "../types.js";
8
6
 
9
- const STORAGE_VALUE = 1;
10
-
11
- const toVisitKey = (key: string) => {
12
- const sep = key.lastIndexOf("/");
13
-
14
- return sep === -1 ? key : key.slice(sep + 1);
15
- };
16
-
17
7
  export class FastforwardVisits extends ModuleCore<
18
8
  FastforwardPluginEvents,
19
9
  FastforwardPluginConstants,
@@ -23,49 +13,27 @@ export class FastforwardVisits extends ModuleCore<
23
13
  > {
24
14
  name = "fastforward.visits";
25
15
 
26
- visits = new Set<string>();
27
-
28
16
  subscribe = () => {
29
- this.on(this.EVENTS.SCENARIO.EXEC, this.onExec);
30
-
31
17
  this.on(this.EVENTS.FASTFORWARD.CHECK, this.onVisitCheck);
32
- this.on(this.EVENTS.FASTFORWARD.CLEAR, this.onClear);
18
+ this.on(this.EVENTS.FASTFORWARD.VISIT_ADD, this.onVisitAdd);
33
19
 
34
- this.on(this.EVENTS.STATE.CLEAR, this.setDefaultState);
35
20
  this.on(this.EVENTS.STATE.SET, this.onSetStateClone);
21
+ this.on(this.EVENTS.STATE.CLEAR, this.setDefaultState);
36
22
  };
37
23
 
38
- init = async () => {
39
- await this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.FASTFORWARD.CHECK_VISITED, value: this.PARAMS.FASTFORWARD.DEFAULT_CHECK_VISITED });
40
-
41
- const stored = (await this.emitOne(this.EVENTS.STORAGE.GET_ALL, { module: this.name })) as Record<string, number>;
42
-
43
- this.visits = new Set(Object.keys(stored).map(toVisitKey));
44
- };
45
-
46
- onExec = async ({ module, block, line, args = {}, keywords = [] }: ScenarioExecPayload = {}) => {
47
- const key = (await this.emitOne(this.EVENTS.VENDORS.HASH, this.state)) as string;
48
- const group = this.state.label || this.globalState.scenario.label;
49
-
50
- this.visits.add(key);
51
- this.emit(this.EVENTS.STORAGE.SET, { module: this.name, group, key, value: STORAGE_VALUE });
52
-
53
- this.setState({ module, block, line, args, keywords, label: this.globalState.scenario.label });
54
- };
24
+ init = () => this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.FASTFORWARD.CHECK_VISITED, value: this.PARAMS.FASTFORWARD.DEFAULT_CHECK_VISITED });
55
25
 
56
26
  onVisitCheck = async () => {
57
- const isAllowed = !this.shared.settings[this.SETTINGS.FASTFORWARD.CHECK_VISITED] || this.visits.has((await this.getHash()) as string);
27
+ const isAllowed =
28
+ !this.shared.settings[this.SETTINGS.FASTFORWARD.CHECK_VISITED] || this.shared.fastforwardStorage.has((await hashSha256(this.state)) as string);
58
29
 
59
30
  return { isAllowed, source: this.name } satisfies FastforwardCheckResult;
60
31
  };
32
+ onVisitAdd = ({ key = "", module, block, line, args = {}, keywords = [] }: FastforwardVisitAddPayload = {}) => {
33
+ this.shared.fastforwardStorage.add(key);
61
34
 
62
- onClear = () => {
63
- this.visits.clear();
64
-
65
- return this.emit(this.EVENTS.STORAGE.RM_ALL, { module: this.name });
35
+ this.setState({ module, block, line, args, keywords, label: this.globalState.scenario.label });
66
36
  };
67
37
 
68
- getHash = () => this.emitOne(this.EVENTS.VENDORS.HASH, this.state);
69
-
70
38
  getDefaultState = (): ModuleGlobalStateFastforwardVisits => ({ label: "", args: {}, keywords: [] });
71
39
  }
@@ -0,0 +1,36 @@
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+
3
+ import { SUBSCRIBE_EVENTS as INTERACT_EVENTS } from "@vnejs/contracts.core.interact";
4
+ import { spyEvent } from "@vnejs/test-utils";
5
+
6
+ import { FastforwardEmit } from "../modules/emit.js";
7
+ import { createFastforwardTestModule, registerFastforwardPluginVne, SUBSCRIBE_EVENTS } from "./setup.js";
8
+
9
+ describe("FastforwardEmit", () => {
10
+ beforeEach(() => {
11
+ registerFastforwardPluginVne();
12
+ vi.useFakeTimers();
13
+ });
14
+
15
+ it("EMIT triggers interact when all checks pass", async () => {
16
+ const { observer } = createFastforwardTestModule(FastforwardEmit);
17
+ const interact = spyEvent(observer, INTERACT_EVENTS.EMIT);
18
+
19
+ observer.subscribe(SUBSCRIBE_EVENTS.CHECK, () => ({ isAllowed: true, source: "test" }));
20
+
21
+ await observer.emit(SUBSCRIBE_EVENTS.EMIT);
22
+
23
+ expect(interact).toHaveBeenCalledOnce();
24
+ });
25
+
26
+ it("EMIT emits STOP when a check fails", async () => {
27
+ const { observer } = createFastforwardTestModule(FastforwardEmit);
28
+ const stop = spyEvent(observer, SUBSCRIBE_EVENTS.STOP);
29
+
30
+ observer.subscribe(SUBSCRIBE_EVENTS.CHECK, () => ({ isAllowed: false, source: "test" }));
31
+
32
+ await observer.emit(SUBSCRIBE_EVENTS.EMIT);
33
+
34
+ expect(stop).toHaveBeenCalledOnce();
35
+ });
36
+ });
@@ -1,8 +1,7 @@
1
1
  import { beforeEach, describe, expect, it, vi } from "vitest";
2
2
 
3
- import { SUBSCRIBE_EVENTS as INTERACT_EVENTS } from "@vnejs/contracts.core.interact";
4
- import { SUBSCRIBE_EVENTS as SYSTEM_EVENTS } from "@vnejs/contracts.core.system";
5
- import { spyEvent } from "@vnejs/test-utils";
3
+ import { SUBSCRIBE_EVENTS as SETTINGS_EVENTS } from "@vnejs/contracts.core.settings";
4
+ import { spyEvent, stubSilentEvent } from "@vnejs/test-utils";
6
5
 
7
6
  import { Fastforward } from "../modules/fastforward.js";
8
7
  import { createFastforwardTestModule, registerFastforwardPluginVne, SUBSCRIBE_EVENTS } from "./setup.js";
@@ -23,34 +22,13 @@ describe("Fastforward", () => {
23
22
  expect(changed).toHaveBeenCalledExactlyOnceWith({ value: true });
24
23
  });
25
24
 
26
- it("EMIT triggers interact when all checks pass", async () => {
27
- const { observer } = createFastforwardTestModule(Fastforward);
28
- const interact = spyEvent(observer, INTERACT_EVENTS.EMIT);
29
-
30
- observer.subscribe(SUBSCRIBE_EVENTS.CHECK, () => ({ isAllowed: true, source: "test" }));
31
-
32
- await observer.emit(SUBSCRIBE_EVENTS.EMIT);
33
-
34
- expect(interact).toHaveBeenCalledOnce();
35
- });
36
-
37
- it("EMIT disables fastforward when a check fails", async () => {
38
- const { observer, shared } = createFastforwardTestModule(Fastforward);
39
-
40
- shared.isFastforwardOn = true;
41
- observer.subscribe(SUBSCRIBE_EVENTS.CHECK, () => ({ isAllowed: false, source: "test" }));
42
-
43
- await observer.emit(SUBSCRIBE_EVENTS.EMIT);
44
-
45
- expect(shared.isFastforwardOn).toBe(false);
46
- });
47
-
48
- it("SYSTEM.STARTED stops fastforward", async () => {
49
- const { observer, shared } = createFastforwardTestModule(Fastforward);
25
+ it("init sets isFastforwardOn to false", async () => {
26
+ const { module, observer, shared } = createFastforwardTestModule(Fastforward, { subscribe: false });
50
27
 
51
28
  shared.isFastforwardOn = true;
29
+ stubSilentEvent(observer, SETTINGS_EVENTS.INIT);
52
30
 
53
- await observer.emit(SYSTEM_EVENTS.STARTED);
31
+ await (module as Fastforward).init();
54
32
 
55
33
  expect(shared.isFastforwardOn).toBe(false);
56
34
  });
@@ -0,0 +1,42 @@
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+
3
+ import { SUBSCRIBE_EVENTS as SCENARIO_EVENTS } from "@vnejs/contracts.core.scenario";
4
+ import { hashSha256 } from "@vnejs/helpers.hash-sha256";
5
+ import { spyEvent } from "@vnejs/test-utils";
6
+
7
+ import { FastforwardScenario } from "../modules/scenario.js";
8
+ import { createFastforwardTestModule, registerFastforwardPluginVne, SUBSCRIBE_EVENTS } from "./setup.js";
9
+
10
+ vi.mock("@vnejs/helpers.hash-sha256", () => ({
11
+ hashSha256: vi.fn(),
12
+ }));
13
+
14
+ describe("FastforwardScenario", () => {
15
+ beforeEach(() => {
16
+ registerFastforwardPluginVne();
17
+ });
18
+
19
+ it("EXEC emits VISIT_ADD with hash of visits state", async () => {
20
+ const { observer } = createFastforwardTestModule(FastforwardScenario, {
21
+ state: {
22
+ "scenario": { label: "intro", curLine: 0 },
23
+ "fastforward.visits": { label: "intro", args: {}, keywords: [] },
24
+ },
25
+ });
26
+ const visitAdd = spyEvent(observer, SUBSCRIBE_EVENTS.VISIT_ADD);
27
+
28
+ vi.mocked(hashSha256).mockResolvedValue("visit-hash");
29
+
30
+ await observer.emit(SCENARIO_EVENTS.EXEC, { module: "text", block: 0, line: "say hi", args: {}, keywords: [] });
31
+
32
+ expect(visitAdd).toHaveBeenCalledExactlyOnceWith({
33
+ module: "text",
34
+ block: 0,
35
+ line: "say hi",
36
+ args: {},
37
+ keywords: [],
38
+ key: "visit-hash",
39
+ group: "intro",
40
+ });
41
+ });
42
+ });
@@ -1,10 +1,10 @@
1
- import { PLUGIN_NAME, PARAMS, SETTINGS_KEYS, SUBSCRIBE_EVENTS } from "@vnejs/contracts.scenario.fastforward";
1
+ import { PLUGIN_NAME, CONSTANTS, PARAMS, SETTINGS_KEYS, SUBSCRIBE_EVENTS } from "@vnejs/contracts.scenario.fastforward";
2
2
  import { createTestModule as baseCreateTestModule, registerCoreVne, registerVnePlugin, stubSilentEvent } from "@vnejs/test-utils";
3
3
  import { SourcesSet } from "@vnejs/sources-set";
4
4
 
5
5
  export const registerFastforwardPluginVne = () => {
6
6
  registerCoreVne();
7
- registerVnePlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, settings: SETTINGS_KEYS, params: PARAMS });
7
+ registerVnePlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, settings: SETTINGS_KEYS, params: PARAMS, constants: CONSTANTS });
8
8
  };
9
9
 
10
10
  export const createFastforwardTestModule = <T extends Parameters<typeof baseCreateTestModule>[0]>(
@@ -18,6 +18,7 @@ export const createFastforwardTestModule = <T extends Parameters<typeof baseCrea
18
18
  [SETTINGS_KEYS.DELAY]: PARAMS.DEFAULT_DELAY,
19
19
  },
20
20
  isFastforwardOn: false,
21
+ fastforwardStorage: new Set<string>(),
21
22
  mediaNoTimeoutSources: new SourcesSet(),
22
23
  viewForceAnimationSources: new SourcesSet(),
23
24
  ...(options.shared as Record<string, unknown>),
@@ -30,4 +31,4 @@ export const createFastforwardTestModule = <T extends Parameters<typeof baseCrea
30
31
  return result;
31
32
  };
32
33
 
33
- export { SUBSCRIBE_EVENTS, PARAMS, SETTINGS_KEYS, PLUGIN_NAME };
34
+ export { SUBSCRIBE_EVENTS, CONSTANTS, PARAMS, SETTINGS_KEYS, PLUGIN_NAME };
@@ -0,0 +1,49 @@
1
+ import { beforeEach, describe, expect, it } from "vitest";
2
+
3
+ import { SUBSCRIBE_EVENTS as STORAGE_EVENTS } from "@vnejs/contracts.core.storage";
4
+ import { spyEvent, stubEvent } from "@vnejs/test-utils";
5
+
6
+ import { FastforwardStorage } from "../modules/storage.js";
7
+ import { createFastforwardTestModule, registerFastforwardPluginVne, SUBSCRIBE_EVENTS } from "./setup.js";
8
+
9
+ describe("FastforwardStorage", () => {
10
+ beforeEach(() => {
11
+ registerFastforwardPluginVne();
12
+ });
13
+
14
+ it("init loads visit hashes from GET_ALL keys that include group", async () => {
15
+ const { module, observer } = createFastforwardTestModule(FastforwardStorage, { subscribe: false });
16
+
17
+ stubEvent(observer, STORAGE_EVENTS.GET_ALL, () => ({ "intro/hash-a": 1, "outro/hash-b": 1 }));
18
+
19
+ await (module as FastforwardStorage).init();
20
+
21
+ expect((module as FastforwardStorage).shared.fastforwardStorage).toEqual(new Set(["hash-a", "hash-b"]));
22
+ });
23
+
24
+ it("VISIT_ADD writes STORAGE.SET under this.name", async () => {
25
+ const { module, observer } = createFastforwardTestModule(FastforwardStorage);
26
+ const storageSet = spyEvent(observer, STORAGE_EVENTS.SET);
27
+
28
+ await observer.emit(SUBSCRIBE_EVENTS.VISIT_ADD, { key: "visit-hash", group: "intro" });
29
+
30
+ expect(storageSet).toHaveBeenCalledExactlyOnceWith({
31
+ module: (module as FastforwardStorage).name,
32
+ group: "intro",
33
+ key: "visit-hash",
34
+ value: 1,
35
+ });
36
+ });
37
+
38
+ it("CLEAR empties storage and RM_ALL by module name", async () => {
39
+ const { module, observer } = createFastforwardTestModule(FastforwardStorage);
40
+ const rmAll = spyEvent(observer, STORAGE_EVENTS.RM_ALL);
41
+
42
+ (module as FastforwardStorage).shared.fastforwardStorage.add("hash");
43
+
44
+ await observer.emit(SUBSCRIBE_EVENTS.CLEAR);
45
+
46
+ expect((module as FastforwardStorage).shared.fastforwardStorage.size).toBe(0);
47
+ expect(rmAll).toHaveBeenCalledExactlyOnceWith({ module: (module as FastforwardStorage).name });
48
+ });
49
+ });
@@ -1,72 +1,62 @@
1
- import { beforeEach, describe, expect, it } from "vitest";
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
2
 
3
- import { SUBSCRIBE_EVENTS as SCENARIO_EVENTS } from "@vnejs/contracts.core.scenario";
4
3
  import { SUBSCRIBE_EVENTS as SETTINGS_EVENTS } from "@vnejs/contracts.core.settings";
5
- import { SUBSCRIBE_EVENTS as STORAGE_EVENTS } from "@vnejs/contracts.core.storage";
6
4
  import { SETTINGS_KEYS } from "@vnejs/contracts.scenario.fastforward";
7
- import { SUBSCRIBE_EVENTS as VENDORS_EVENTS } from "@vnejs/contracts.core.vendors";
8
- import { spyEvent, stubEvent, stubSilentEvent } from "@vnejs/test-utils";
5
+ import { hashSha256 } from "@vnejs/helpers.hash-sha256";
6
+ import { spyEvent } from "@vnejs/test-utils";
9
7
 
10
8
  import { FastforwardVisits } from "../modules/visits.js";
11
- import { createFastforwardTestModule, registerFastforwardPluginVne, SUBSCRIBE_EVENTS } from "./setup.js";
9
+ import { PARAMS, createFastforwardTestModule, registerFastforwardPluginVne, SUBSCRIBE_EVENTS } from "./setup.js";
10
+
11
+ vi.mock("@vnejs/helpers.hash-sha256", () => ({
12
+ hashSha256: vi.fn(),
13
+ }));
12
14
 
13
15
  describe("FastforwardVisits", () => {
14
16
  beforeEach(() => {
15
17
  registerFastforwardPluginVne();
16
18
  });
17
19
 
18
- it("init loads visit hashes from GET_ALL keys that include group", async () => {
20
+ it("init registers CHECK_VISITED setting", async () => {
19
21
  const { module, observer } = createFastforwardTestModule(FastforwardVisits, { subscribe: false });
20
-
21
- stubSilentEvent(observer, SETTINGS_EVENTS.INIT);
22
- stubEvent(observer, STORAGE_EVENTS.GET_ALL, () => ({ "intro/hash-a": 1, "outro/hash-b": 1 }));
22
+ const settingsInit = spyEvent(observer, SETTINGS_EVENTS.INIT);
23
23
 
24
24
  await (module as FastforwardVisits).init();
25
25
 
26
- expect((module as FastforwardVisits).visits).toEqual(new Set(["hash-a", "hash-b"]));
26
+ expect(settingsInit).toHaveBeenCalledExactlyOnceWith({
27
+ name: SETTINGS_KEYS.CHECK_VISITED,
28
+ value: PARAMS.DEFAULT_CHECK_VISITED,
29
+ });
27
30
  });
28
31
 
29
- it("EXEC stores visit under scenario label group", async () => {
32
+ it("VISIT_ADD records visit and updates state", async () => {
30
33
  const { module, observer } = createFastforwardTestModule(FastforwardVisits, {
31
34
  state: { scenario: { label: "intro", curLine: 0 } },
32
35
  });
33
- const storageSet = spyEvent(observer, STORAGE_EVENTS.SET);
34
-
35
- stubEvent(observer, VENDORS_EVENTS.HASH, () => "visit-hash");
36
- (module as FastforwardVisits).setState({ label: "intro", args: {}, keywords: [] });
37
36
 
38
- await observer.emit(SCENARIO_EVENTS.EXEC, { module: "text", block: 0, line: "say hi", args: {}, keywords: [] });
39
-
40
- expect((module as FastforwardVisits).visits.has("visit-hash")).toBe(true);
41
- expect(storageSet).toHaveBeenCalledExactlyOnceWith({
42
- module: "fastforward.visits",
43
- group: "intro",
37
+ await observer.emit(SUBSCRIBE_EVENTS.VISIT_ADD, {
38
+ module: "text",
39
+ block: 0,
40
+ line: "say hi",
41
+ args: {},
42
+ keywords: [],
44
43
  key: "visit-hash",
45
- value: 1,
44
+ group: "intro",
46
45
  });
46
+
47
+ expect((module as FastforwardVisits).shared.fastforwardStorage.has("visit-hash")).toBe(true);
48
+ expect((module as FastforwardVisits).state).toMatchObject({ module: "text", line: "say hi", label: "intro" });
47
49
  });
48
50
 
49
51
  it("CHECK allows visit when hash is known", async () => {
50
52
  const { module, observer } = createFastforwardTestModule(FastforwardVisits);
51
53
 
52
54
  (module as FastforwardVisits).shared.settings[SETTINGS_KEYS.CHECK_VISITED] = true;
53
- (module as FastforwardVisits).visits.add("known-hash");
54
- stubEvent(observer, VENDORS_EVENTS.HASH, () => "known-hash");
55
+ (module as FastforwardVisits).shared.fastforwardStorage.add("known-hash");
56
+ vi.mocked(hashSha256).mockResolvedValue("known-hash");
55
57
 
56
58
  const [result] = await observer.emit(SUBSCRIBE_EVENTS.CHECK);
57
59
 
58
60
  expect(result).toEqual({ isAllowed: true, source: "fastforward.visits" });
59
61
  });
60
-
61
- it("CLEAR empties visits and RM_ALL by module", async () => {
62
- const { module, observer } = createFastforwardTestModule(FastforwardVisits);
63
- const rmAll = spyEvent(observer, STORAGE_EVENTS.RM_ALL);
64
-
65
- (module as FastforwardVisits).visits.add("hash");
66
-
67
- await observer.emit(SUBSCRIBE_EVENTS.CLEAR);
68
-
69
- expect((module as FastforwardVisits).visits.size).toBe(0);
70
- expect(rmAll).toHaveBeenCalledExactlyOnceWith({ module: "fastforward.visits" });
71
- });
72
62
  });