diff --git a/package.json b/package.json index 25b384a..ec400e0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@next2d/framework", "description": "Next2D Framework is designed according to the principles of clean architecture, domain-driven development, test-driven development, and MVVM, with an emphasis on flexibility, scalability, and maintainability, and a design methodology that keeps each layer loosely coupled.", - "version": "4.0.10", + "version": "4.1.0", "homepage": "https://next2d.app", "bugs": "https://github.com/Next2D/Framework/issues/new", "author": "Toshiyuki Ienaga (https://github.com/ienaga/)", @@ -34,17 +34,18 @@ "@eslint/eslintrc": "^3.3.7", "@eslint/js": "^10.0.1", "@types/node": "^26.6.6", - "@typescript-eslint/eslint-plugin": "^8.72.1", - "@typescript-eslint/parser": "^8.72.1", - "@vitest/coverage-v8": "^5.0.5", - "@vitest/web-worker": "^5.0.5", - "eslint": "^10.14.0", + "@typescript-eslint/eslint-plugin": "^8.73.0", + "@typescript-eslint/parser": "^8.73.0", + "@typescript/native": "npm:typescript@^7.0.2", + "@vitest/coverage-v8": "^5.1.1", + "@vitest/web-worker": "^5.1.1", + "eslint": "^10.15.0", "eslint-plugin-unused-imports": "^4.4.1", - "globals": "^17.13.0", - "jsdom": "^30.1.3", - "typescript": "^6.0.3", - "vite": "^8.3.5", - "vitest": "^5.0.5", + "globals": "^17.14.0", + "jsdom": "^31.0.1", + "typescript": "npm:@typescript/typescript6@^6.0.2", + "vite": "^8.3.8", + "vitest": "^5.1.1", "vitest-webgl-canvas-mock": "^1.1.0" }, "peerDependencies": { diff --git a/src/application/usecase/ApplicationGotoViewUseCase.test.ts b/src/application/usecase/ApplicationGotoViewUseCase.test.ts index 41c8fbc..4aa2fef 100644 --- a/src/application/usecase/ApplicationGotoViewUseCase.test.ts +++ b/src/application/usecase/ApplicationGotoViewUseCase.test.ts @@ -21,6 +21,11 @@ vi.mock("../../domain/service/LoadingService", () => ({ } })); +vi.mock("./LoadingEndOnExitFrameUseCase", () => ({ + execute: vi.fn().mockResolvedValue(undefined), + cancel: vi.fn() +})); + vi.mock("../../infrastructure/usecase/ResponseRemoveVariableUseCase", () => ({ execute: vi.fn() })); @@ -179,12 +184,75 @@ describe("ApplicationGotoViewUseCase Test", () => }); vi.mocked(requestUseCase).mockResolvedValue([]); + const { + execute: loadingEndOnExitFrameUseCase, + cancel: cancelLoadingEndOnExitFrameUseCase + } = await import("./LoadingEndOnExitFrameUseCase"); + const { ViewBinderService } = await import("../../domain/service/ViewBinderService"); + + const mockView = { onEnter: vi.fn().mockResolvedValue(undefined) }; + vi.mocked(ViewBinderService.bind).mockResolvedValue(mockView as any); + await execute(mockApplication, "home"); + expect(cancelLoadingEndOnExitFrameUseCase).toHaveBeenCalled(); expect(ScreenOverlayService.add).toHaveBeenCalled(); expect(LoadingService.start).toHaveBeenCalled(); - expect(LoadingService.end).toHaveBeenCalled(); - expect(ScreenOverlayService.dispose).toHaveBeenCalled(); + expect(loadingEndOnExitFrameUseCase).toHaveBeenCalledTimes(1); + + // bind後に登録し、ローディング終了後にonEnterが呼ばれる + const bindOrder = vi.mocked(ViewBinderService.bind).mock.invocationCallOrder[0]; + const registerOrder = vi.mocked(loadingEndOnExitFrameUseCase).mock.invocationCallOrder[0]; + const enterOrder = mockView.onEnter.mock.invocationCallOrder[0]; + expect(bindOrder).toBeLessThan(registerOrder); + expect(registerOrder).toBeLessThan(enterOrder); + + // 終了処理はEXIT_FRAME側に委譲されている + expect(LoadingService.end).not.toHaveBeenCalled(); + expect(ScreenOverlayService.dispose).not.toHaveBeenCalled(); + }); + + it("execute test case4-2: onEnter waits for loading end on EXIT_FRAME", async () => + { + const { execute: queryStringParserService } = await import("../service/QueryStringParserService"); + const { execute: requestUseCase } = await import("../../infrastructure/usecase/RequestUseCase"); + const { execute: loadingEndOnExitFrameUseCase } = await import("./LoadingEndOnExitFrameUseCase"); + const { ViewBinderService } = await import("../../domain/service/ViewBinderService"); + + $setConfig({ + platform: "web", + spa: false, + stage: { + width: 800, + height: 600, + fps: 60 + }, + loading: { + callback: async () => null + } + }); + + vi.mocked(queryStringParserService).mockReturnValue({ + name: "home", + queryString: "" + }); + vi.mocked(requestUseCase).mockResolvedValue([]); + + const mockView = { onEnter: vi.fn().mockResolvedValue(undefined) }; + vi.mocked(ViewBinderService.bind).mockResolvedValue(mockView as any); + + let endLoading: () => void = () => {}; + vi.mocked(loadingEndOnExitFrameUseCase).mockReturnValueOnce( + new Promise((resolve) => { endLoading = resolve }) + ); + + const promise = execute(mockApplication, "home"); + await new Promise((resolve) => setTimeout(resolve, 0)); + expect(mockView.onEnter).not.toHaveBeenCalled(); + + endLoading(); + await promise; + expect(mockView.onEnter).toHaveBeenCalledTimes(1); }); it("execute test case5: response with callback should execute callback", async () => diff --git a/src/application/usecase/ApplicationGotoViewUseCase.ts b/src/application/usecase/ApplicationGotoViewUseCase.ts index f05073f..5f9b6ef 100644 --- a/src/application/usecase/ApplicationGotoViewUseCase.ts +++ b/src/application/usecase/ApplicationGotoViewUseCase.ts @@ -8,6 +8,10 @@ import { getRequests } from "../../infrastructure/usecase/RequestUseCase"; import { execute as executeCallbackUseCase } from "./ExecuteCallbackUseCase"; +import { + execute as loadingEndOnExitFrameUseCase, + cancel as cancelLoadingEndOnExitFrameUseCase +} from "./LoadingEndOnExitFrameUseCase"; import { execute as responseRemoveVariableUseCase } from "../../infrastructure/usecase/ResponseRemoveVariableUseCase"; import { ViewBinderService } from "../../domain/service/ViewBinderService"; import { LoadingService } from "../../domain/service/LoadingService"; @@ -31,6 +35,12 @@ export const execute = async ( const config = $getConfig(); const hasLoading = !!config.loading; + /** + * 前の遷移で待機中のローディング終了処理を解除 + * Cancel the pending loading end process of the previous transition + */ + cancelLoadingEndOnExitFrameUseCase(); + if (hasLoading) { /** * 現時点の描画をキャプチャーして表示 @@ -117,6 +127,14 @@ export const execute = async ( */ const view = await ViewBinderService.bind(context, application.currentName); + /** + * bind後の最初のEXIT_FRAMEでローディング表示と画面キャプチャーを終了 + * End loading display and screen capture on the first EXIT_FRAME after bind + */ + const loadingEnded: Promise = hasLoading + ? loadingEndOnExitFrameUseCase() + : Promise.resolve(); + /** * コールバック設定があれば実行 * Execute callback settings if any. @@ -125,19 +143,11 @@ export const execute = async ( await executeCallbackUseCase(config.gotoView.callback, view); } - if (hasLoading) { - /** - * ローディング表示を終了 - * End loading display - */ - await LoadingService.end(); - - /** - * 前の画面のキャプチャーを終了 - * End previous screen capture - */ - ScreenOverlayService.dispose(); - } + /** + * ローディングの終了を待機 + * Wait for the loading to end + */ + await loadingEnded; /** * 画面表示時の処理を実行 diff --git a/src/application/usecase/LoadingEndOnExitFrameUseCase.test.ts b/src/application/usecase/LoadingEndOnExitFrameUseCase.test.ts new file mode 100644 index 0000000..c84b090 --- /dev/null +++ b/src/application/usecase/LoadingEndOnExitFrameUseCase.test.ts @@ -0,0 +1,132 @@ +import { describe, expect, it, vi, beforeEach } from "vitest"; +import { Event } from "@next2d/events"; + +vi.mock("@next2d/display", async () => +{ + const { EventDispatcher } = await import("@next2d/events"); + return { "stage": new EventDispatcher() }; +}); + +vi.mock("../../domain/service/LoadingService", () => ({ + "LoadingService": { + "end": vi.fn().mockResolvedValue(undefined) + } +})); + +vi.mock("../../domain/service/ScreenOverlayService", () => ({ + "ScreenOverlayService": { + "dispose": vi.fn() + } +})); + +describe("LoadingEndOnExitFrameUseCase Test", () => +{ + beforeEach(async () => + { + const { cancel } = await import("./LoadingEndOnExitFrameUseCase"); + cancel(); + vi.clearAllMocks(); + }); + + it("execute test: ends loading and disposes overlay on EXIT_FRAME", async () => + { + const { stage } = await import("@next2d/display"); + const { LoadingService } = await import("../../domain/service/LoadingService"); + const { ScreenOverlayService } = await import("../../domain/service/ScreenOverlayService"); + const { execute } = await import("./LoadingEndOnExitFrameUseCase"); + + let resolved = false; + const promise = execute().then(() => { resolved = true }); + + expect(stage.hasEventListener(Event.EXIT_FRAME)).toBe(true); + expect(LoadingService.end).not.toHaveBeenCalled(); + expect(ScreenOverlayService.dispose).not.toHaveBeenCalled(); + + await Promise.resolve(); + expect(resolved).toBe(false); + + stage.dispatchEvent(new Event(Event.EXIT_FRAME)); + await promise; + + expect(resolved).toBe(true); + expect(LoadingService.end).toHaveBeenCalledTimes(1); + expect(ScreenOverlayService.dispose).toHaveBeenCalledTimes(1); + expect(stage.hasEventListener(Event.EXIT_FRAME)).toBe(false); + }); + + it("execute test: runs only once", async () => + { + const { stage } = await import("@next2d/display"); + const { LoadingService } = await import("../../domain/service/LoadingService"); + const { execute } = await import("./LoadingEndOnExitFrameUseCase"); + + const promise = execute(); + stage.dispatchEvent(new Event(Event.EXIT_FRAME)); + await promise; + + stage.dispatchEvent(new Event(Event.EXIT_FRAME)); + await Promise.resolve(); + + expect(LoadingService.end).toHaveBeenCalledTimes(1); + }); + + it("execute test: ENTER_FRAME does not end loading", async () => + { + const { stage } = await import("@next2d/display"); + const { LoadingService } = await import("../../domain/service/LoadingService"); + const { execute } = await import("./LoadingEndOnExitFrameUseCase"); + + execute(); + stage.dispatchEvent(new Event(Event.ENTER_FRAME)); + await Promise.resolve(); + + expect(LoadingService.end).not.toHaveBeenCalled(); + expect(stage.hasEventListener(Event.EXIT_FRAME)).toBe(true); + }); + + it("execute test: previous listener is replaced by a new transition", async () => + { + const { stage } = await import("@next2d/display"); + const { LoadingService } = await import("../../domain/service/LoadingService"); + const { execute } = await import("./LoadingEndOnExitFrameUseCase"); + + execute(); + const promise = execute(); + + stage.dispatchEvent(new Event(Event.EXIT_FRAME)); + await promise; + + expect(LoadingService.end).toHaveBeenCalledTimes(1); + expect(stage.hasEventListener(Event.EXIT_FRAME)).toBe(false); + }); + + it("cancel test: removes pending listener", async () => + { + const { stage } = await import("@next2d/display"); + const { LoadingService } = await import("../../domain/service/LoadingService"); + const { execute, cancel } = await import("./LoadingEndOnExitFrameUseCase"); + + execute(); + cancel(); + + expect(stage.hasEventListener(Event.EXIT_FRAME)).toBe(false); + + stage.dispatchEvent(new Event(Event.EXIT_FRAME)); + await Promise.resolve(); + expect(LoadingService.end).not.toHaveBeenCalled(); + }); + + it("execute test: rejects when loading end fails", async () => + { + const { stage } = await import("@next2d/display"); + const { LoadingService } = await import("../../domain/service/LoadingService"); + const { execute } = await import("./LoadingEndOnExitFrameUseCase"); + + vi.mocked(LoadingService.end).mockRejectedValueOnce(new Error("end failed")); + + const promise = execute(); + stage.dispatchEvent(new Event(Event.EXIT_FRAME)); + + await expect(promise).rejects.toThrow("end failed"); + }); +}); diff --git a/src/application/usecase/LoadingEndOnExitFrameUseCase.ts b/src/application/usecase/LoadingEndOnExitFrameUseCase.ts new file mode 100644 index 0000000..142a673 --- /dev/null +++ b/src/application/usecase/LoadingEndOnExitFrameUseCase.ts @@ -0,0 +1,83 @@ +import { stage } from "@next2d/display"; +import { Event } from "@next2d/events"; +import { LoadingService } from "../../domain/service/LoadingService"; +import { ScreenOverlayService } from "../../domain/service/ScreenOverlayService"; + +/** + * @type {Function | null} + * @private + */ +let $listener: (() => Promise) | null = null; + +/** + * @description 待機中のEXIT_FRAMEリスナーを解除する + * Remove the pending EXIT_FRAME listener + * + * @return {void} + * @method + * @public + */ +export const cancel = (): void => +{ + if (!$listener) { + return ; + } + + stage.removeEventListener(Event.EXIT_FRAME, $listener); + $listener = null; +}; + +/** + * @description 次のEXIT_FRAMEでローディング表示と画面キャプチャーを終了する + * リスナーを登録した時点で返却し、終了処理の完了はPromiseで通知する + * End the loading display and screen capture on the next EXIT_FRAME. + * Returns after registering the listener and notifies completion via Promise. + * + * @return {Promise} + * @method + * @public + */ +export const execute = (): Promise => +{ + /** + * 前の遷移のリスナーが残っていれば解除 + * Remove the listener left by the previous transition + */ + cancel(); + + return new Promise((resolve, reject): void => + { + const listener = async (): Promise => + { + /** + * 一度だけ実行するため、即座に解除 + * Remove immediately so that it runs only once + */ + stage.removeEventListener(Event.EXIT_FRAME, listener); + if ($listener === listener) { + $listener = null; + } + + try { + /** + * ローディング表示を終了 + * End loading display + */ + await LoadingService.end(); + + /** + * 前の画面のキャプチャーを終了 + * End previous screen capture + */ + ScreenOverlayService.dispose(); + + resolve(); + } catch (error) { + reject(error); + } + }; + + $listener = listener; + stage.addEventListener(Event.EXIT_FRAME, listener); + }); +}; diff --git a/src/index.ts b/src/index.ts index 15a5e7b..ce54c66 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,7 @@ import { TextFieldContent } from "./application/content/TextFieldContent"; import { VideoContent } from "./application/content/VideoContent"; // output build version -console.log("%c Next2D Framework %c 4.0.10 %c https://next2d.app", +console.log("%c Next2D Framework %c 4.1.0 %c https://next2d.app", "color: #fff; background: #5f5f5f", "color: #fff; background: #4bc729", "");