Merge branch 'main' into chore/build-toolchain-20260812
This commit is contained in:
@@ -52,6 +52,11 @@ export const buildReadModelDeltaCacheKey = (
|
||||
const canPatch = (value: unknown): value is Record<string, unknown> | unknown[] =>
|
||||
value !== null && typeof value === 'object';
|
||||
|
||||
const snapshotByteLength = (revision: string, serialized: string): number =>
|
||||
Buffer.byteLength(`{"kind":"snapshot","revision":${JSON.stringify(revision)},"data":`) +
|
||||
Buffer.byteLength(serialized) +
|
||||
1;
|
||||
|
||||
const storeSnapshot = async (store: ReadModelDeltaCacheStore, key: string, serialized: string): Promise<void> => {
|
||||
try {
|
||||
await store.set(key, serialized, { EX: CACHE_TTL_SECONDS });
|
||||
@@ -91,7 +96,7 @@ export const createReadModelDelta = async <T>(request: ReadModelDeltaRequest<T>)
|
||||
};
|
||||
const snapshot = { kind: 'snapshot' as const, revision, data: canonicalValue };
|
||||
await storeSnapshot(request.store, currentKey, serialized);
|
||||
return Buffer.byteLength(JSON.stringify(patch)) < Buffer.byteLength(JSON.stringify(snapshot))
|
||||
return Buffer.byteLength(JSON.stringify(patch)) < snapshotByteLength(revision, serialized)
|
||||
? patch
|
||||
: snapshot;
|
||||
}
|
||||
|
||||
@@ -89,6 +89,31 @@ describe('createReadModelDelta', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('keeps the snapshot fallback when a positional patch is larger', async () => {
|
||||
const store = new MemoryStore();
|
||||
const initialValue = { values: Array.from({ length: 24 }, () => 'old') };
|
||||
const initial = await createReadModelDelta({
|
||||
store,
|
||||
profile: 'hwe:default',
|
||||
viewerId: 'user-1',
|
||||
slice: 'context',
|
||||
value: initialValue,
|
||||
forceSnapshot: true,
|
||||
});
|
||||
const nextValue = { values: Array.from({ length: 24 }, () => 'new') };
|
||||
|
||||
const changed = await createReadModelDelta({
|
||||
store,
|
||||
profile: 'hwe:default',
|
||||
viewerId: 'user-1',
|
||||
slice: 'context',
|
||||
value: nextValue,
|
||||
knownRevision: initial.revision,
|
||||
});
|
||||
|
||||
expect(changed).toMatchObject({ kind: 'snapshot', data: nextValue });
|
||||
});
|
||||
|
||||
it('falls back to a snapshot when Redis is unavailable', async () => {
|
||||
const store: ReadModelDeltaCacheStore = {
|
||||
get: async () => {
|
||||
|
||||
Reference in New Issue
Block a user