Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/synchronizers/synchronizer-local/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const createLocalSynchronizer = ((
send,
registerReceive,
destroy,
0.01,
0.05,
onSend,
onReceive,
onIgnoredError,
Expand Down
43 changes: 43 additions & 0 deletions test/unit/synchronizers/synchronizers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,44 @@ test('Local Synchronizer cancels scheduled messages on destroy', async () => {
await receiver.destroy();
});

test('Local Synchronizer tolerates delayed timer delivery', async () => {
vi.useFakeTimers();
const setTimeoutImpl = globalThis.setTimeout;
const timerSpy = vi
.spyOn(globalThis, 'setTimeout')
.mockImplementation(((
callback: (...args: any[]) => void,
delay = 0,
...args: any[]
) =>
setTimeoutImpl(
callback,
delay === 0 ? 15 : delay,
...args,
)) as typeof setTimeout);
const errors: Error[] = [];
const source = createLocalSynchronizer(
createMergeableStore().setValue('v1', 'value'),
);
const store = createMergeableStore();
const target = createLocalSynchronizer(store, undefined, undefined, (error) =>
errors.push(error),
);
try {
await source.startAutoSave();
const loading = target.load();
await vi.advanceTimersByTimeAsync(200);
await loading;
expect(store.getValue('v1')).toBe('value');
expect(errors).toEqual([]);
} finally {
await target.destroy();
await source.destroy();
timerSpy.mockRestore();
vi.useRealTimers();
}
});

test('Local Synchronizer excludes late recipients', async () => {
const receive = vi.fn();
const sender = createLocalSynchronizer(createMergeableStore());
Expand Down Expand Up @@ -435,6 +473,11 @@ describe.each([
await synchronizer1.startSync();
await synchronizer2.startSync();
await pause(synchronizable.pauseMilliseconds);
await vi.waitFor(() =>
expect(store1.getMergeableContent()).toEqual(
store2.getMergeableContent(),
),
);
};

beforeEach(() => {
Expand Down