fix: preserve logical mutation intent
This commit is contained in:
@@ -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(),
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user