@vnejs/compatibility.text-with-scenario.history 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.d.ts CHANGED
@@ -4,6 +4,6 @@ import { ModuleCore } from "@vnejs/module.core";
4
4
  import type { PluginConstants, PluginEvents, PluginParams, PluginSettings } from "./types.js";
5
5
  export declare class CompatibilityTextWithScenarioHistory extends ModuleCore<PluginEvents, PluginConstants, PluginSettings, PluginParams> {
6
6
  name: string;
7
- init: () => Promise<unknown[]> | undefined;
8
- onTextEmitNext: () => Promise<unknown[] | undefined>;
7
+ subscribe: () => void;
8
+ onTextEmitNext: () => Promise<import("@vnejs/shared").EmitResults<"vne:history:add"> | undefined>;
9
9
  }
package/dist/index.js CHANGED
@@ -6,7 +6,9 @@ import { getTextSpeakerInfo } from "@vnejs/contracts.text";
6
6
  const MARK_REG = /\{.*?\}/g;
7
7
  export class CompatibilityTextWithScenarioHistory extends ModuleCore {
8
8
  name = "compatibility.text-with-scenario.history";
9
- init = () => this.emit(this.EVENTS.TEXT.EMIT_NEXT_REG, { handler: this.onTextEmitNext });
9
+ subscribe = () => {
10
+ this.on(this.EVENTS.TEXT.EMIT_NEXT, this.onTextEmitNext);
11
+ };
10
12
  onTextEmitNext = async () => {
11
13
  const texts = this.globalState.text.texts;
12
14
  if (!texts.length)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vnejs/compatibility.text-with-scenario.history",
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
@@ -4,7 +4,7 @@ import "@vnejs/contracts.text";
4
4
  import { ModuleCore } from "@vnejs/module.core";
5
5
  import { regModule } from "@vnejs/shared";
6
6
 
7
- import { getTextSpeakerInfo } from "@vnejs/contracts.text";
7
+ import { getTextSpeakerInfo, type TextSpeakerEntry } from "@vnejs/contracts.text";
8
8
 
9
9
  import type { PluginConstants, PluginEvents, PluginParams, PluginSettings } from "./types.js";
10
10
 
@@ -13,7 +13,9 @@ const MARK_REG = /\{.*?\}/g;
13
13
  export class CompatibilityTextWithScenarioHistory extends ModuleCore<PluginEvents, PluginConstants, PluginSettings, PluginParams> {
14
14
  name = "compatibility.text-with-scenario.history";
15
15
 
16
- init = () => this.emit(this.EVENTS.TEXT.EMIT_NEXT_REG, { handler: this.onTextEmitNext });
16
+ subscribe = () => {
17
+ this.on(this.EVENTS.TEXT.EMIT_NEXT, this.onTextEmitNext);
18
+ };
17
19
 
18
20
  onTextEmitNext = async () => {
19
21
  const texts = this.globalState.text.texts;
@@ -25,7 +27,7 @@ export class CompatibilityTextWithScenarioHistory extends ModuleCore<PluginEvent
25
27
  const args = texts[texts.length - 1].args;
26
28
  const speakerState = this.globalState["text.speaker"];
27
29
  const speaker = speakerState.speaker;
28
- const speakerInfo = await this.emitClone(getTextSpeakerInfo(speakerState, speaker));
30
+ const speakerInfo = await this.emitClone<TextSpeakerEntry | undefined>(getTextSpeakerInfo(speakerState, speaker));
29
31
 
30
32
  return this.emit(this.EVENTS.HISTORY.ADD, { label, text, args, speaker, speakerInfo });
31
33
  };
@@ -8,32 +8,27 @@ import { createCompatibilityTestModule, HISTORY_EVENTS, registerCompatibilityVne
8
8
  describe("CompatibilityTextWithScenarioHistory", () => {
9
9
  beforeEach(() => registerCompatibilityVne());
10
10
 
11
- it("init registers TEXT.EMIT_NEXT handler", async () => {
11
+ it("subscribe registers TEXT.EMIT_NEXT handler", () => {
12
12
  const { module, observer } = createCompatibilityTestModule(CompatibilityTextWithScenarioHistory, { subscribe: false });
13
- const reg = spyEvent(observer, TEXT_EVENTS.EMIT_NEXT_REG);
14
13
 
15
- await (module as CompatibilityTextWithScenarioHistory).init();
14
+ (module as CompatibilityTextWithScenarioHistory).subscribe();
16
15
 
17
- expect(reg).toHaveBeenCalledOnce();
18
- expect(reg.mock.calls[0]?.[0]?.handler).toEqual(expect.any(Function));
16
+ expect(observer.subscribers[TEXT_EVENTS.EMIT_NEXT]?.flatMap((group) => group.subscribers)).toEqual(
17
+ expect.arrayContaining([expect.objectContaining({ func: (module as CompatibilityTextWithScenarioHistory).onTextEmitNext })]),
18
+ );
19
19
  });
20
20
 
21
21
  it("handler emits HISTORY.ADD with stripped text", async () => {
22
- const { module, observer } = createCompatibilityTestModule(CompatibilityTextWithScenarioHistory, {
23
- subscribe: false,
22
+ const { observer } = createCompatibilityTestModule(CompatibilityTextWithScenarioHistory, {
24
23
  state: {
25
24
  "text": { uid: "u1", texts: [{ text: "Hello {mark}", args: { a: 1 } }], tokens: [], speaker: "hero" },
26
25
  "text.speaker": { speaker: "hero", args: {}, speakersInfo: {} },
27
26
  "scenario": { curLine: 1, label: "scene" },
28
27
  },
29
28
  });
30
- const reg = spyEvent(observer, TEXT_EVENTS.EMIT_NEXT_REG);
31
29
  const add = spyEvent(observer, HISTORY_EVENTS.ADD);
32
30
 
33
- await (module as CompatibilityTextWithScenarioHistory).init();
34
-
35
- const handler = reg.mock.calls[0]?.[0]?.handler as () => Promise<unknown>;
36
- await handler();
31
+ await observer.emit(TEXT_EVENTS.EMIT_NEXT);
37
32
 
38
33
  expect(add).toHaveBeenCalledOnce();
39
34
  expect(add.mock.calls[0]?.[0]).toMatchObject({ label: "scene", text: "Hello ", speaker: "hero", args: { a: 1 } });