문제
런타임 문서가 MOCK·HTTP를 고르므로 두 소스가 모든 빌드에 들어간다.
기능 입력 조립(create-tech-log-feature-input.ts)이 MOCK 공개 콘텐츠,
MOCK·HTTP Studio gateway를 정적으로 가져와, 운영(HTTP) 공개 화면을
여는 사람도 그 코드를 초기 JS로 받았다. MOCK이 쓰는 content-format은
마크다운 파서(micromark·mdast·unified)까지 끌고 왔다. 초기 JS는 main
에서 300,869 B(gzip)로 예산 204,800 B를 넘는다(FE-GATE-012).
고른 방법
- 포트는 이미 모두 Promise를 돌려준다. deferAsyncPort가 포트를 먼저
만들고 첫 호출에서 구현 모듈을 동적 import해 넘긴다. 호출하는 쪽과
feature input의 모양(publicContent, createStudioApplication)은 그대로다.
- 포트 키 목록은 `Record<keyof Port, true>`로 적어, 포트에 메서드가
늘거나 줄면 타입 검사가 실패한다.
- 구현 받기에 실패하면 잊고 다음 호출에서 다시 받는다.
- MOCK 공개 콘텐츠는 첫 조회 때 받는다. HTTP 공개 gateway는 공개 첫
화면의 데이터 요청에 쓰이므로 그대로 둔다.
- Studio gateway 셋(MOCK이면 Studio state 하나를 나눠 씀)은 묶음 하나로
받는다(mock-studio-gateways.ts, http-studio-gateways.ts).
createStudioApplication은 Studio가 마운트될 때 불리므로 그때 바로 받기
시작한다(startNow). 첫 조회 때 받으면 화면 청크 → 조회 → gateway 청크
→ 요청으로 한 번 더 기다린다.
버린 대안
- createStudioApplication을 async로 바꾸기: StudioProvider와 시험 15곳이
함께 바뀐다. 포트가 이미 비동기라 필요 없다.
- 공개 HTTP gateway도 지연 로드: 첫 화면 데이터 요청 앞에 청크 왕복이
하나 생긴다.
위험·한계
- MOCK 빌드(로컬·e2e·visual)는 첫 공개 조회와 첫 Studio 조회 앞에 청크를
한 번 받는다.
- 포트 메서드가 동기 값을 돌려주게 바뀌면 이 방식은 맞지 않는다. 타입
제약(모든 멤버가 Promise를 돌려줌)이 그때 컴파일에서 막는다.
검증
- desktop CI env 빌드: 초기 JS 300,869 → 232,337 B(Studio 셸 지연을
포함한 측정, 다음 커밋). 빠진 모듈 179개 중 npm(마크다운 파서) 약
88 KB, mock 23 KB, static 11 KB(모듈 단독 gzip 합).
- tests/features/tech-log/deferred-async-port.test.ts: 한 번만 받기, 동시
첫 호출, 실패 뒤 다시 받기, 키·freeze, gateway 셋의 state 공유, startNow.
113 lines
3.9 KiB
TypeScript
113 lines
3.9 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { test } from "vitest";
|
|
|
|
import { deferAsyncPort } from "../../../src/features/tech-log/adapters/deferred-async-port.ts";
|
|
import { deferStudioGateways } from "../../../src/features/tech-log/adapters/deferred-tech-log-ports.ts";
|
|
import { createMockStudioGateways } from "../../../src/features/tech-log/adapters/mock/mock-studio-gateways.ts";
|
|
import { FIXTURE_IDS } from "../../../src/features/tech-log/adapters/mock/fixtures.ts";
|
|
import type { Asset } from "../../../src/features/tech-log/contracts/studio/contract.ts";
|
|
|
|
type Greeter = Readonly<{
|
|
greet(name: string): Promise<string>;
|
|
count(): Promise<number>;
|
|
}>;
|
|
|
|
function greeterImplementation(): Greeter {
|
|
let calls = 0;
|
|
return {
|
|
async greet(name) {
|
|
calls += 1;
|
|
return `hello ${name}`;
|
|
},
|
|
async count() {
|
|
return calls;
|
|
},
|
|
};
|
|
}
|
|
|
|
const GREETER_METHODS = { greet: true, count: true } as const;
|
|
|
|
test("구현을 첫 호출에서 한 번만 받고 모든 메서드가 같은 구현을 쓴다", async () => {
|
|
let loads = 0;
|
|
const port = deferAsyncPort(async () => {
|
|
loads += 1;
|
|
return greeterImplementation();
|
|
}, GREETER_METHODS);
|
|
|
|
assert.equal(loads, 0, "만들기만 해서는 구현을 받지 않는다");
|
|
assert.equal(await port.greet("a"), "hello a");
|
|
assert.equal(await port.greet("b"), "hello b");
|
|
assert.equal(await port.count(), 2);
|
|
assert.equal(loads, 1);
|
|
});
|
|
|
|
test("동시에 들어온 첫 호출들도 구현을 한 번만 받는다", async () => {
|
|
let loads = 0;
|
|
const port = deferAsyncPort(async () => {
|
|
loads += 1;
|
|
await Promise.resolve();
|
|
return greeterImplementation();
|
|
}, GREETER_METHODS);
|
|
|
|
await Promise.all([port.greet("a"), port.greet("b"), port.count()]);
|
|
assert.equal(loads, 1);
|
|
});
|
|
|
|
test("구현을 받지 못하면 그 호출은 실패하고 다음 호출에서 다시 받는다", async () => {
|
|
let loads = 0;
|
|
const port = deferAsyncPort(async () => {
|
|
loads += 1;
|
|
if (loads === 1) throw new TypeError("Failed to fetch dynamically imported module");
|
|
return greeterImplementation();
|
|
}, GREETER_METHODS);
|
|
|
|
await assert.rejects(port.greet("a"), /dynamically imported module/u);
|
|
assert.equal(await port.greet("b"), "hello b");
|
|
assert.equal(loads, 2);
|
|
});
|
|
|
|
test("포트는 선언한 메서드만 가진 얼린 객체다", () => {
|
|
const port = deferAsyncPort(async () => greeterImplementation(), GREETER_METHODS);
|
|
|
|
assert.deepEqual(Object.keys(port).sort(), ["count", "greet"]);
|
|
assert.equal(Object.isFrozen(port), true);
|
|
});
|
|
|
|
test("Studio gateway 셋은 구현 묶음 하나를 같이 쓴다", async () => {
|
|
let loads = 0;
|
|
const assets = new Map<string, Asset>();
|
|
const gateways = deferStudioGateways(async () => {
|
|
loads += 1;
|
|
return createMockStudioGateways(assets);
|
|
});
|
|
|
|
assert.equal(loads, 0);
|
|
const draft = (await gateways.studio.getDocument(FIXTURE_IDS.conflictCase)).document;
|
|
await gateways.management.deleteDocument("CASE", draft.id, draft.version, {
|
|
idempotencyKey: "deferred-shared-state",
|
|
});
|
|
// 관리 gateway 로 지운 작업본이 같은 묶음의 문서 gateway 에서도 사라진다.
|
|
await assert.rejects(gateways.studio.getDocument(FIXTURE_IDS.conflictCase));
|
|
await gateways.assets.listAssets({});
|
|
assert.equal(loads, 1);
|
|
});
|
|
|
|
test("startNow 면 만들 때 받기 시작하고, 그때의 실패는 첫 호출이 다시 받는다", async () => {
|
|
let loads = 0;
|
|
const assets = new Map<string, Asset>();
|
|
const gateways = deferStudioGateways(
|
|
async () => {
|
|
loads += 1;
|
|
if (loads === 1) throw new TypeError("Failed to fetch dynamically imported module");
|
|
return createMockStudioGateways(assets);
|
|
},
|
|
{ startNow: true },
|
|
);
|
|
|
|
assert.equal(loads, 1, "만들자마자 받기 시작한다");
|
|
// 미리 받기의 실패가 처리된 뒤 첫 호출이 들어온다.
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
await gateways.studio.getDashboard();
|
|
assert.equal(loads, 2);
|
|
});
|