@vnejs/compatibility.text-with-scenario.history 0.2.1
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.d.ts +9 -0
- package/dist/index.js +23 -0
- package/dist/types.d.ts +8 -0
- package/dist/types.js +1 -0
- package/package.json +43 -0
- package/src/index.ts +34 -0
- package/src/tests/history.test.ts +41 -0
- package/src/tests/setup.ts +25 -0
- package/src/types.ts +13 -0
- package/tsconfig.json +9 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import "@vnejs/contracts.scenario.history";
|
|
2
|
+
import "@vnejs/contracts.text";
|
|
3
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
4
|
+
import type { PluginConstants, PluginEvents, PluginParams, PluginSettings } from "./types.js";
|
|
5
|
+
export declare class CompatibilityTextWithScenarioHistory extends ModuleCore<PluginEvents, PluginConstants, PluginSettings, PluginParams> {
|
|
6
|
+
name: string;
|
|
7
|
+
init: () => Promise<unknown[]> | undefined;
|
|
8
|
+
onTextEmitNext: () => Promise<unknown[] | undefined>;
|
|
9
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import "@vnejs/contracts.scenario.history";
|
|
2
|
+
import "@vnejs/contracts.text";
|
|
3
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
4
|
+
import { regModule } from "@vnejs/shared";
|
|
5
|
+
import { getTextSpeakerInfo } from "@vnejs/contracts.text";
|
|
6
|
+
const MARK_REG = /\{.*?\}/g;
|
|
7
|
+
export class CompatibilityTextWithScenarioHistory extends ModuleCore {
|
|
8
|
+
name = "compatibility.text-with-scenario.history";
|
|
9
|
+
init = () => this.emit(this.EVENTS.TEXT.EMIT_NEXT_REG, { handler: this.onTextEmitNext });
|
|
10
|
+
onTextEmitNext = async () => {
|
|
11
|
+
const texts = this.globalState.text.texts;
|
|
12
|
+
if (!texts.length)
|
|
13
|
+
return;
|
|
14
|
+
const label = this.globalState.scenario.label;
|
|
15
|
+
const text = texts[texts.length - 1].text.replace(MARK_REG, "");
|
|
16
|
+
const args = texts[texts.length - 1].args;
|
|
17
|
+
const speakerState = this.globalState["text.speaker"];
|
|
18
|
+
const speaker = speakerState.speaker;
|
|
19
|
+
const speakerInfo = await this.emitClone(getTextSpeakerInfo(speakerState, speaker));
|
|
20
|
+
return this.emit(this.EVENTS.HISTORY.ADD, { label, text, args, speaker, speakerInfo });
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
regModule(CompatibilityTextWithScenarioHistory);
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import "@vnejs/contracts.scenario.history";
|
|
2
|
+
import type { ModuleCoreConstants, ModuleCoreEvents, ModuleCoreParams, ModuleCoreSettings } from "@vnejs/module.core";
|
|
3
|
+
import type { Constants as TextConstants, PluginName as TextPluginName, SubscribeEvents as TextSubscribeEvents } from "@vnejs/contracts.text";
|
|
4
|
+
import type { PluginName as HistoryPluginName, SubscribeEvents as HistorySubscribeEvents } from "@vnejs/contracts.scenario.history";
|
|
5
|
+
export type PluginEvents = ModuleCoreEvents & Record<TextPluginName, TextSubscribeEvents> & Record<HistoryPluginName, HistorySubscribeEvents>;
|
|
6
|
+
export type PluginConstants = ModuleCoreConstants & Record<TextPluginName, TextConstants>;
|
|
7
|
+
export type PluginSettings = ModuleCoreSettings;
|
|
8
|
+
export type PluginParams = ModuleCoreParams;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "@vnejs/contracts.scenario.history";
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vnejs/compatibility.text-with-scenario.history",
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"require": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src",
|
|
18
|
+
"tsconfig.json"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "npx @vnejs/monorepo test",
|
|
22
|
+
"build": "npx @vnejs/monorepo package",
|
|
23
|
+
"publish:major:plugin": "npm run publish:major",
|
|
24
|
+
"publish:minor:plugin": "npm run publish:minor",
|
|
25
|
+
"publish:patch:plugin": "npm run publish:patch",
|
|
26
|
+
"publish:major": "npx @vnejs/monorepo publish major --access public",
|
|
27
|
+
"publish:minor": "npx @vnejs/monorepo publish minor --access public",
|
|
28
|
+
"publish:patch": "npx @vnejs/monorepo publish patch --access public"
|
|
29
|
+
},
|
|
30
|
+
"author": "",
|
|
31
|
+
"license": "ISC",
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"@vnejs/module.core": "~0.2.0",
|
|
34
|
+
"@vnejs/contracts.text": "~0.2.0",
|
|
35
|
+
"@vnejs/contracts.scenario.history": "~0.2.0",
|
|
36
|
+
"@vnejs/shared": "~0.2.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@vnejs/configs.ts-common": "~0.2.0",
|
|
40
|
+
"@vnejs/configs.vitest": "~0.2.0",
|
|
41
|
+
"@vnejs/test-utils": "~0.2.0"
|
|
42
|
+
}
|
|
43
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import "@vnejs/contracts.scenario.history";
|
|
2
|
+
import "@vnejs/contracts.text";
|
|
3
|
+
|
|
4
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
5
|
+
import { regModule } from "@vnejs/shared";
|
|
6
|
+
|
|
7
|
+
import { getTextSpeakerInfo } from "@vnejs/contracts.text";
|
|
8
|
+
|
|
9
|
+
import type { PluginConstants, PluginEvents, PluginParams, PluginSettings } from "./types.js";
|
|
10
|
+
|
|
11
|
+
const MARK_REG = /\{.*?\}/g;
|
|
12
|
+
|
|
13
|
+
export class CompatibilityTextWithScenarioHistory extends ModuleCore<PluginEvents, PluginConstants, PluginSettings, PluginParams> {
|
|
14
|
+
name = "compatibility.text-with-scenario.history";
|
|
15
|
+
|
|
16
|
+
init = () => this.emit(this.EVENTS.TEXT.EMIT_NEXT_REG, { handler: this.onTextEmitNext });
|
|
17
|
+
|
|
18
|
+
onTextEmitNext = async () => {
|
|
19
|
+
const texts = this.globalState.text.texts;
|
|
20
|
+
|
|
21
|
+
if (!texts.length) return;
|
|
22
|
+
|
|
23
|
+
const label = this.globalState.scenario.label;
|
|
24
|
+
const text = texts[texts.length - 1].text.replace(MARK_REG, "");
|
|
25
|
+
const args = texts[texts.length - 1].args;
|
|
26
|
+
const speakerState = this.globalState["text.speaker"];
|
|
27
|
+
const speaker = speakerState.speaker;
|
|
28
|
+
const speakerInfo = await this.emitClone(getTextSpeakerInfo(speakerState, speaker));
|
|
29
|
+
|
|
30
|
+
return this.emit(this.EVENTS.HISTORY.ADD, { label, text, args, speaker, speakerInfo });
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
regModule(CompatibilityTextWithScenarioHistory);
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { spyEvent } from "@vnejs/test-utils";
|
|
4
|
+
|
|
5
|
+
import { CompatibilityTextWithScenarioHistory } from "../index.js";
|
|
6
|
+
import { createCompatibilityTestModule, HISTORY_EVENTS, registerCompatibilityVne, TEXT_EVENTS } from "./setup.js";
|
|
7
|
+
|
|
8
|
+
describe("CompatibilityTextWithScenarioHistory", () => {
|
|
9
|
+
beforeEach(() => registerCompatibilityVne());
|
|
10
|
+
|
|
11
|
+
it("init registers TEXT.EMIT_NEXT handler", async () => {
|
|
12
|
+
const { module, observer } = createCompatibilityTestModule(CompatibilityTextWithScenarioHistory, { subscribe: false });
|
|
13
|
+
const reg = spyEvent(observer, TEXT_EVENTS.EMIT_NEXT_REG);
|
|
14
|
+
|
|
15
|
+
await (module as CompatibilityTextWithScenarioHistory).init();
|
|
16
|
+
|
|
17
|
+
expect(reg).toHaveBeenCalledOnce();
|
|
18
|
+
expect(reg.mock.calls[0]?.[0]?.handler).toEqual(expect.any(Function));
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("handler emits HISTORY.ADD with stripped text", async () => {
|
|
22
|
+
const { module, observer } = createCompatibilityTestModule(CompatibilityTextWithScenarioHistory, {
|
|
23
|
+
subscribe: false,
|
|
24
|
+
state: {
|
|
25
|
+
"text": { uid: "u1", texts: [{ text: "Hello {mark}", args: { a: 1 } }], tokens: [], speaker: "hero" },
|
|
26
|
+
"text.speaker": { speaker: "hero", args: {}, speakersInfo: {} },
|
|
27
|
+
"scenario": { curLine: 1, label: "scene" },
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
const reg = spyEvent(observer, TEXT_EVENTS.EMIT_NEXT_REG);
|
|
31
|
+
const add = spyEvent(observer, HISTORY_EVENTS.ADD);
|
|
32
|
+
|
|
33
|
+
await (module as CompatibilityTextWithScenarioHistory).init();
|
|
34
|
+
|
|
35
|
+
const handler = reg.mock.calls[0]?.[0]?.handler as () => Promise<unknown>;
|
|
36
|
+
await handler();
|
|
37
|
+
|
|
38
|
+
expect(add).toHaveBeenCalledOnce();
|
|
39
|
+
expect(add.mock.calls[0]?.[0]).toMatchObject({ label: "scene", text: "Hello ", speaker: "hero", args: { a: 1 } });
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { PARAMS as TEXT_PARAMS, PLUGIN_NAME as TEXT, SETTINGS_KEYS as TEXT_SETTINGS, SUBSCRIBE_EVENTS as TEXT_EVENTS } from "@vnejs/contracts.text";
|
|
2
|
+
import { SUBSCRIBE_EVENTS as HISTORY_EVENTS, PLUGIN_NAME as HISTORY } from "@vnejs/contracts.scenario.history";
|
|
3
|
+
import { createTestModule as baseCreateTestModule, registerCoreVne, registerVnePlugin } from "@vnejs/test-utils";
|
|
4
|
+
|
|
5
|
+
export const registerCompatibilityVne = () => {
|
|
6
|
+
registerCoreVne();
|
|
7
|
+
registerVnePlugin(TEXT, { events: TEXT_EVENTS, params: TEXT_PARAMS, settings: TEXT_SETTINGS });
|
|
8
|
+
registerVnePlugin(HISTORY, { events: HISTORY_EVENTS });
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const createCompatibilityTestModule = <T extends Parameters<typeof baseCreateTestModule>[0]>(
|
|
12
|
+
ModuleClass: T,
|
|
13
|
+
options: Parameters<typeof baseCreateTestModule>[1] = {},
|
|
14
|
+
) =>
|
|
15
|
+
baseCreateTestModule(ModuleClass, {
|
|
16
|
+
state: {
|
|
17
|
+
"text": { uid: "", texts: [], tokens: [], speaker: "" },
|
|
18
|
+
"text.speaker": { speakers: {} },
|
|
19
|
+
"scenario": { curLine: 0, label: "start" },
|
|
20
|
+
...(options.state ?? {}),
|
|
21
|
+
},
|
|
22
|
+
...options,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export { HISTORY_EVENTS, TEXT_EVENTS };
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import "@vnejs/contracts.scenario.history";
|
|
2
|
+
|
|
3
|
+
import type { ModuleCoreConstants, ModuleCoreEvents, ModuleCoreParams, ModuleCoreSettings } from "@vnejs/module.core";
|
|
4
|
+
import type { Constants as TextConstants, PluginName as TextPluginName, SubscribeEvents as TextSubscribeEvents } from "@vnejs/contracts.text";
|
|
5
|
+
import type { PluginName as HistoryPluginName, SubscribeEvents as HistorySubscribeEvents } from "@vnejs/contracts.scenario.history";
|
|
6
|
+
|
|
7
|
+
export type PluginEvents = ModuleCoreEvents & Record<TextPluginName, TextSubscribeEvents> & Record<HistoryPluginName, HistorySubscribeEvents>;
|
|
8
|
+
|
|
9
|
+
export type PluginConstants = ModuleCoreConstants & Record<TextPluginName, TextConstants>;
|
|
10
|
+
|
|
11
|
+
export type PluginSettings = ModuleCoreSettings;
|
|
12
|
+
|
|
13
|
+
export type PluginParams = ModuleCoreParams;
|