diff --git a/src/features/local-draft-feature/adapters/create-local-draft-feature-input.ts b/src/features/local-draft-feature/adapters/create-local-draft-feature-input.ts index 93b62d1e..9b7461cc 100644 --- a/src/features/local-draft-feature/adapters/create-local-draft-feature-input.ts +++ b/src/features/local-draft-feature/adapters/create-local-draft-feature-input.ts @@ -1,6 +1,3 @@ -import type { - IndexedDbRepositoryPort, -} from "../../../application/ports/browser-file-storage/index.ts"; import { defineFeatureAdapterContribution, } from "../../feature-adapter-contribution.ts"; @@ -8,22 +5,24 @@ import type {} from "../../feature-adapter-contribution.ts"; import { createLocalDraftFeatureInput, } from "../application/local-draft-api.ts"; -import type { LocalDraft } from "../domain/local-draft.ts"; import { LOCAL_DRAFT_FEATURE_ID, LOCAL_DRAFT_REPOSITORY_ID, } from "../contracts/local-draft-contract.ts"; -import { createLocalDraftStore } from "./local-draft-indexeddb-store.ts"; +import { + createLocalDraftStore, + type LocalDraftRepository, +} from "./local-draft-indexeddb-store.ts"; declare module "../../feature-adapter-contribution.ts" { interface FeatureIndexedDbRepositories { - "local-draft": IndexedDbRepositoryPort; + "local-draft": LocalDraftRepository; } } export function createLocalDraftFeatureInstalledInput(context: Readonly<{ indexedDb: Readonly<{ - get(repositoryId: "local-draft"): IndexedDbRepositoryPort; + get(repositoryId: "local-draft"): LocalDraftRepository; }>; }>) { const repository = context.indexedDb.get(LOCAL_DRAFT_REPOSITORY_ID); diff --git a/src/features/local-draft-feature/adapters/local-draft-indexeddb-store.ts b/src/features/local-draft-feature/adapters/local-draft-indexeddb-store.ts index bc777beb..9f0fd0a9 100644 --- a/src/features/local-draft-feature/adapters/local-draft-indexeddb-store.ts +++ b/src/features/local-draft-feature/adapters/local-draft-indexeddb-store.ts @@ -1,7 +1,3 @@ -import type { - BrowserDataFailure, - IndexedDbRepositoryPort, -} from "../../../application/ports/browser-file-storage/index.ts"; import { createFailure, type AppFailure, @@ -16,7 +12,43 @@ import type { } from "../application/local-draft-api.ts"; import type { LocalDraft } from "../domain/local-draft.ts"; -function failureKind(failure: BrowserDataFailure): FailureKind { +type RepositoryFailure = Readonly<{ code: string }>; +type RepositoryResult = Result; + +/** + * 00-02. The part of the `indexedDb` capability this feature calls. The host + * hands in the reusable runtime (`IndexedDbRepositoryPort`), + * 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 | null> + >; + compareAndSwap( + input: Readonly<{ + key: string; + value: LocalDraft; + expectedRevision: number | null; + idempotencyKey: string; + signal?: AbortSignal; + }>, + ): Promise>>; + remove( + input: Readonly<{ + key: string; + expectedRevision: number; + idempotencyKey: string; + signal?: AbortSignal; + }>, + ): Promise>; +}>; + +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"; @@ -24,7 +56,7 @@ function failureKind(failure: BrowserDataFailure): FailureKind { } function mapFailure( - failure: BrowserDataFailure, + failure: RepositoryFailure, operationId: string, ): AppFailure { return createFailure(failureKind(failure), operationId, 0, { @@ -33,7 +65,7 @@ function mapFailure( } function failed( - failure: BrowserDataFailure, + failure: RepositoryFailure, operationId: string, ): Result { return Object.freeze({ @@ -48,7 +80,7 @@ function failed( * cross this file. */ export function createLocalDraftStore( - repository: IndexedDbRepositoryPort, + repository: LocalDraftRepository, ): LocalDraftStore { return Object.freeze({ async save(command: SaveLocalDraftCommand, signal?: AbortSignal) {