Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <ienaga@next2d.app> (https://github.com/ienaga/)",
Expand Down Expand Up @@ -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": {
Expand Down
72 changes: 70 additions & 2 deletions src/application/usecase/ApplicationGotoViewUseCase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}));
Expand Down Expand Up @@ -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<void>((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 () =>
Expand Down
36 changes: 23 additions & 13 deletions src/application/usecase/ApplicationGotoViewUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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) {
/**
* 現時点の描画をキャプチャーして表示
Expand Down Expand Up @@ -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<void> = hasLoading
? loadingEndOnExitFrameUseCase()
: Promise.resolve();

/**
* コールバック設定があれば実行
* Execute callback settings if any.
Expand All @@ -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;

/**
* 画面表示時の処理を実行
Expand Down
132 changes: 132 additions & 0 deletions src/application/usecase/LoadingEndOnExitFrameUseCase.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});
Loading
Loading