src/features/local-draft-feature는 기능이 아니라 IndexedDB recipe의 소비자다. 시험 5개와 recipe가 쓰고, 그래서 생성기에 NOT_A_FEATURE 예외가 있었다(S4 3.1절 S-3). 코드를 tests/fixtures/indexeddb-consumer/로 옮기고 import만 고쳤다. NOT_A_FEATURE 표식은 이 폴더만 썼고 다운스트림 둘에도 없어 생성기에서 지웠다. 선언 없는 폴더는 밖으로 옮기라는 메시지로 실패한다. 매뉴얼 오류표, README, starter-experience, capability-consumer-experience, patterns를 고쳤다. 위험: fixture는 기능 폴더 밖이라 기능 계층 규칙(dependency-cruiser)이 걸리지 않는다. recipe가 tests/ 아래 파일을 type import한다. 검증(desktop): test:contract, test:recipes, test:browser-capabilities, test:unit 통과.
138 lines
4.0 KiB
TypeScript
138 lines
4.0 KiB
TypeScript
import {
|
|
createFailure,
|
|
type AppFailure,
|
|
type FailureKind,
|
|
} from "../../../../src/contracts/errors.ts";
|
|
import type { Result } from "../../../../src/contracts/result.ts";
|
|
import type {
|
|
LocalDraftStore,
|
|
LocalDraftRecord,
|
|
RemoveLocalDraftCommand,
|
|
SaveLocalDraftCommand,
|
|
} from "../application/local-draft-api.ts";
|
|
import type { LocalDraft } from "../domain/local-draft.ts";
|
|
|
|
type RepositoryFailure = Readonly<{ code: string }>;
|
|
type RepositoryResult<Value> = Result<Value, RepositoryFailure>;
|
|
|
|
/**
|
|
* 00-02. The part of the `indexedDb` capability this feature calls. The host
|
|
* hands in the reusable runtime (`IndexedDbRepositoryPort<LocalDraft, never>`),
|
|
* which satisfies this shape structurally (the recipe and the browser spec
|
|
* compile that assignment). Importing the port instead tied the feature to the
|
|
* browser storage runtime, which a product may remove.
|
|
*/
|
|
export type LocalDraftRepository = Readonly<{
|
|
read(
|
|
key: string,
|
|
signal?: AbortSignal,
|
|
): Promise<
|
|
RepositoryResult<Readonly<{ value: LocalDraft; revision: number }> | null>
|
|
>;
|
|
compareAndSwap(
|
|
input: Readonly<{
|
|
key: string;
|
|
value: LocalDraft;
|
|
expectedRevision: number | null;
|
|
idempotencyKey: string;
|
|
signal?: AbortSignal;
|
|
}>,
|
|
): Promise<RepositoryResult<Readonly<{ revision: number }>>>;
|
|
remove(
|
|
input: Readonly<{
|
|
key: string;
|
|
expectedRevision: number;
|
|
idempotencyKey: string;
|
|
signal?: AbortSignal;
|
|
}>,
|
|
): Promise<RepositoryResult<unknown>>;
|
|
}>;
|
|
|
|
function failureKind(failure: RepositoryFailure): FailureKind {
|
|
if (failure.code === "QUOTA_EXCEEDED") return "STORAGE_QUOTA_EXCEEDED";
|
|
if (failure.code === "CONFLICT") return "CONFLICT";
|
|
if (failure.code === "ABORTED") return "REQUEST_ABORTED";
|
|
return "STORAGE_UNAVAILABLE";
|
|
}
|
|
|
|
function mapFailure(
|
|
failure: RepositoryFailure,
|
|
operationId: string,
|
|
): AppFailure {
|
|
return createFailure(failureKind(failure), operationId, 0, {
|
|
code: `INDEXEDDB_${failure.code}`,
|
|
});
|
|
}
|
|
|
|
function failed<Value>(
|
|
failure: RepositoryFailure,
|
|
operationId: string,
|
|
): Result<Value> {
|
|
return Object.freeze({
|
|
ok: false as const,
|
|
error: mapFailure(failure, operationId),
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Feature-owned adapter over the reusable IndexedDB repository capability.
|
|
* Native IDB objects, migrations, connection leases and quota internals do not
|
|
* cross this file.
|
|
*/
|
|
export function createLocalDraftStore(
|
|
repository: LocalDraftRepository,
|
|
): LocalDraftStore {
|
|
return Object.freeze({
|
|
async save(command: SaveLocalDraftCommand, signal?: AbortSignal) {
|
|
const result = await repository.compareAndSwap({
|
|
key: command.draft.draftId,
|
|
value: command.draft,
|
|
expectedRevision: command.expectedRevision,
|
|
idempotencyKey: command.idempotencyKey,
|
|
signal,
|
|
});
|
|
return result.ok
|
|
? Object.freeze({
|
|
ok: true as const,
|
|
value: Object.freeze({ revision: result.value.revision }),
|
|
})
|
|
: failed<Readonly<{ revision: number }>>(
|
|
result.error,
|
|
"LOCAL_DRAFT_SAVE",
|
|
);
|
|
},
|
|
|
|
async find(draftId: string, signal?: AbortSignal) {
|
|
const result = await repository.read(draftId, signal);
|
|
if (!result.ok) {
|
|
return failed<LocalDraftRecord | null>(
|
|
result.error,
|
|
"LOCAL_DRAFT_FIND",
|
|
);
|
|
}
|
|
return Object.freeze({
|
|
ok: true as const,
|
|
value:
|
|
result.value === null
|
|
? null
|
|
: Object.freeze({
|
|
draft: result.value.value,
|
|
revision: result.value.revision,
|
|
}),
|
|
});
|
|
},
|
|
|
|
async remove(command: RemoveLocalDraftCommand, signal?: AbortSignal) {
|
|
const result = await repository.remove({
|
|
key: command.draftId,
|
|
expectedRevision: command.expectedRevision,
|
|
idempotencyKey: command.idempotencyKey,
|
|
signal,
|
|
});
|
|
return result.ok
|
|
? Object.freeze({ ok: true as const, value: undefined })
|
|
: failed<void>(result.error, "LOCAL_DRAFT_REMOVE");
|
|
},
|
|
});
|
|
}
|