fix: preserve logical mutation intent

This commit is contained in:
DongHyeonka
2026-08-02 01:07:19 +09:00
parent 53d181fbe4
commit cbcc7b5ed7
21 changed files with 724 additions and 96 deletions
@@ -0,0 +1,50 @@
import type { MutationIntentFactory } from "../../application/ports/mutation-intent-factory.ts";
import { defineMutationIntent } from "../../contracts/mutation-intent.ts";
export type BrowserMutationIntentFactoryDependencies = Readonly<{
randomUUID?: () => string;
monotonicNow?: () => number;
}>;
export function createBrowserMutationIntentFactory(
dependencies: BrowserMutationIntentFactoryDependencies = {},
): MutationIntentFactory {
const randomUUID =
dependencies.randomUUID ??
(() => {
if (
typeof globalThis.crypto === "undefined" ||
typeof globalThis.crypto.randomUUID !== "function"
) {
throw new TypeError("Secure mutation identity generation is unavailable.");
}
return globalThis.crypto.randomUUID();
});
const monotonicNow =
dependencies.monotonicNow ??
(() => {
if (
typeof globalThis.performance === "undefined" ||
typeof globalThis.performance.now !== "function"
) {
throw new TypeError("Monotonic time is unavailable.");
}
return globalThis.performance.now();
});
return Object.freeze({
create(input) {
const intentId = randomUUID();
const idempotencyKey = input.requiresIdempotencyKey
? randomUUID()
: undefined;
return defineMutationIntent({
intentId,
operationId: input.operationId,
canonicalInputIdentity: input.canonicalInputIdentity,
...(idempotencyKey === undefined ? {} : { idempotencyKey }),
createdAtMonotonicMs: monotonicNow(),
});
},
});
}