fix(game): recover realtime dashboard delta cloning

This commit is contained in:
2026-08-11 15:09:32 +00:00
parent d1c35cc380
commit 6e95dbd487
8 changed files with 518 additions and 74 deletions
+38 -6
View File
@@ -31,12 +31,37 @@ export class ReadModelDeltaMismatchError extends Error {
}
}
export class ReadModelDeltaApplyError extends Error {
constructor(message: string, options?: ErrorOptions) {
super(message, options);
this.name = 'ReadModelDeltaApplyError';
}
}
export interface AppliedReadModelDelta<T> {
data: T;
revision: string;
}
const cloneJsonValue = <T>(value: T): T => structuredClone(value);
/**
* Read-model deltas operate on JSON documents received over tRPC. Serializing
* through JSON also unwraps Vue's nested reactive proxies, which a root-level
* `toRaw()` does not remove and `structuredClone()` cannot clone.
*/
export const cloneReadModelJson = <T>(value: T): T => {
try {
const serialized = JSON.stringify(value);
if (serialized === undefined) {
throw new TypeError('The read-model value is not a JSON document.');
}
return JSON.parse(serialized) as T;
} catch (error) {
if (error instanceof ReadModelDeltaApplyError) {
throw error;
}
throw new ReadModelDeltaApplyError('Failed to clone the read-model JSON document.', { cause: error });
}
};
export const createJsonPatch = (current: unknown, next: unknown): JsonPatchOperation[] => createPatch(current, next);
@@ -74,11 +99,18 @@ export const applyReadModelDelta = <T>(
);
}
const next = cloneJsonValue(current);
const errors = applyPatch(next, delta.operations as Operation[]);
const failure = errors.find((error) => error !== null);
if (failure) {
throw new ReadModelDeltaMismatchError(`JSON Patch application failed: ${failure.message}`);
const next = cloneReadModelJson(current);
try {
const errors = applyPatch(next, delta.operations as Operation[]);
const failure = errors.find((error) => error !== null);
if (failure) {
throw new ReadModelDeltaApplyError(`JSON Patch application failed: ${failure.message}`);
}
} catch (error) {
if (error instanceof ReadModelDeltaApplyError) {
throw error;
}
throw new ReadModelDeltaApplyError('JSON Patch application failed.', { cause: error });
}
return {
+41 -1
View File
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import { applyReadModelDelta, ReadModelDeltaMismatchError } from '../src/realtime/delta.js';
import { applyReadModelDelta, ReadModelDeltaApplyError, ReadModelDeltaMismatchError } from '../src/realtime/delta.js';
describe('applyReadModelDelta', () => {
it('applies a JSON Patch without mutating the previous snapshot', () => {
@@ -46,4 +46,44 @@ describe('applyReadModelDelta', () => {
})
).toThrow(ReadModelDeltaMismatchError);
});
it('unwraps nested reactive-style proxies before applying a later patch', () => {
const proxiedStableBranch = new Proxy(
{
values: [{ key: '이동', possible: true, status: 'available' }],
},
{}
);
const current = {
general: [
{ category: '일반', values: [{ key: '휴식', possible: false, status: 'blocked' }] },
proxiedStableBranch,
],
};
expect(() => structuredClone(current)).toThrow();
const applied = applyReadModelDelta(current, 'revision-1', {
kind: 'patch',
baseRevision: 'revision-1',
revision: 'revision-2',
operations: [{ op: 'replace', path: '/general/1/values/0/possible', value: false }],
});
expect(applied.data.general[1]?.values[0]?.possible).toBe(false);
expect(applied.data.general[1]).not.toBe(proxiedStableBranch);
});
it('reports a non-JSON baseline as a recoverable delta application error', () => {
const current: { value: number; self?: unknown } = { value: 1 };
current.self = current;
expect(() =>
applyReadModelDelta(current, 'revision-1', {
kind: 'patch',
baseRevision: 'revision-1',
revision: 'revision-2',
operations: [{ op: 'replace', path: '/value', value: 2 }],
})
).toThrow(ReadModelDeltaApplyError);
});
});