From f6ba16cd107daea2010937c99fe78f2e8bcbbb23 Mon Sep 17 00:00:00 2001 From: Josh Payette Date: Mon, 18 May 2026 20:49:15 -0400 Subject: [PATCH] Fixed synchronous awaits that were not dependent on each other. --- src/features/dal/hooks/use-dal-mutation.ts | 30 ++++++++++++---------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/features/dal/hooks/use-dal-mutation.ts b/src/features/dal/hooks/use-dal-mutation.ts index 3f0ebe0..52c08e3 100644 --- a/src/features/dal/hooks/use-dal-mutation.ts +++ b/src/features/dal/hooks/use-dal-mutation.ts @@ -48,9 +48,11 @@ const useDalMutation = ( }; /** - * Executes the local write then enqueues the op for sync. - * Local write runs first so the UI reflects the change immediately; - * enqueueing second ensures the op is persisted before the process could be killed. + * Executes the local write and enqueues the op for sync. + * `getServerUpdatedAt` runs first because it reads the pre-write `updatedAt` baseline + * from the same prisma-idb record that `action.local` mutates — racing them would lose + * the baseline. `action.local` and `enqueueOp` target separate IndexedDB databases and + * share no data, so they race in parallel. */ const runLocalWithEnqueue = async ( action: DalWriteAction, @@ -60,16 +62,18 @@ const runLocalWithEnqueue = async ( const serverUpdatedAt = action.getServerUpdatedAt ? await action.getServerUpdatedAt(input, ctx) : undefined; - const result = await action.local(input, ctx); - const op = await enqueueOp({ - anonUserId: ctx.anonUserId, - entity: action.entity, - operation: action.operation, - payload: input, - idempotencyKey: action.buildIdempotencyKey(input, ctx), - serverUpdatedAt: serverUpdatedAt ?? undefined, - summary: action.describe?.(input, ctx), - }); + const [result, op] = await Promise.all([ + action.local(input, ctx), + enqueueOp({ + anonUserId: ctx.anonUserId, + entity: action.entity, + operation: action.operation, + payload: input, + idempotencyKey: action.buildIdempotencyKey(input, ctx), + serverUpdatedAt: serverUpdatedAt ?? undefined, + summary: action.describe?.(input, ctx), + }), + ]); return { result, branch: "local", enqueuedOpId: op?.id ?? null }; }; -- 2.51.2