fix(frontend): smooth realtime dashboard refreshes

This commit is contained in:
2026-08-07 06:12:43 +00:00
parent e198f70756
commit ebc54198d7
8 changed files with 381 additions and 28 deletions
@@ -0,0 +1,30 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { createLatestRefreshQueue } from '../src/utils/latestRefreshQueue.ts';
void test('coalesces an event burst into one final refresh without losing it', async () => {
const releases: Array<() => void> = [];
let runs = 0;
const queue = createLatestRefreshQueue(async () => {
runs += 1;
await new Promise<void>((resolve) => releases.push(resolve));
});
const first = queue.request();
assert.equal(queue.isRunning(), true);
const second = queue.request();
const third = queue.request();
assert.equal(second, first);
assert.equal(third, first);
assert.equal(runs, 1);
releases.shift()?.();
await new Promise<void>((resolve) => setImmediate(resolve));
assert.equal(runs, 2);
releases.shift()?.();
await first;
assert.equal(queue.isRunning(), false);
assert.equal(runs, 2);
});
@@ -0,0 +1,39 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { structurallyShare } from '../src/utils/structuralShare.ts';
void test('reuses a completely unchanged tRPC snapshot', () => {
const current = {
general: { id: 7, name: '장수' },
records: [{ id: 3, text: '기록' }],
createdAt: new Date('2026-08-07T00:00:00.000Z'),
};
const incoming = {
general: { id: 7, name: '장수' },
records: [{ id: 3, text: '기록' }],
createdAt: new Date('2026-08-07T00:00:00.000Z'),
};
assert.equal(structurallyShare(current, incoming), current);
});
void test('replaces only changed branches and preserves sibling identities', () => {
const current = {
general: { id: 7, name: '이전 이름' },
city: { id: 1, name: '업' },
records: [{ id: 3, text: '기록' }],
};
const incoming = {
general: { id: 7, name: '새 이름' },
city: { id: 1, name: '업' },
records: [{ id: 3, text: '기록' }],
};
const shared = structurallyShare(current, incoming);
assert.notEqual(shared, current);
assert.notEqual(shared.general, current.general);
assert.deepEqual(shared.general, incoming.general);
assert.equal(shared.city, current.city);
assert.equal(shared.records, current.records);
});