Skip to content

Commit df294ab

Browse files
committed
return early if falling back to setTimeout for MessageChannel
1 parent bb7e941 commit df294ab

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

.changeset/olive-owls-search.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@remote-dom/core': patch
3+
---
4+
5+
return early if falling back to setTimeout for MessageChannel

packages/core/source/elements/connection.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ function createDefaultBatchFunction() {
5858
setTimeout(() => {
5959
queue();
6060
}, 0);
61+
return;
6162
}
6263

6364
// `MessageChannel` trick that forces the code to run on the next task.

packages/core/source/elements/tests/connection.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,30 @@ describe('BatchingRemoteConnection', () => {
2929
expect(connection.mutate).toHaveBeenCalledWith([7, 8, 9]);
3030
});
3131

32+
it('batches mutations with setTimeout when there is no MessageChannel', async () => {
33+
vi.useFakeTimers();
34+
vi.spyOn(globalThis, 'MessageChannel', 'get').mockReturnValue(
35+
undefined as any,
36+
);
37+
const setTimeoutSpy = vi.spyOn(globalThis, 'setTimeout');
38+
const connection = createRemoteConnectionSpy();
39+
const batchingConnection = new BatchingRemoteConnection(connection);
40+
41+
batchingConnection.mutate([1, 2, 3]);
42+
batchingConnection.mutate([4, 5, 6]);
43+
44+
expect(connection.mutate).not.toHaveBeenCalled();
45+
46+
vi.runAllTimers();
47+
48+
expect(setTimeoutSpy).toHaveBeenCalledTimes(1);
49+
expect(connection.mutate).toHaveBeenCalledTimes(1);
50+
expect(connection.mutate).toHaveBeenCalledWith([1, 2, 3, 4, 5, 6]);
51+
52+
vi.restoreAllMocks();
53+
vi.useRealTimers();
54+
});
55+
3256
it('flushes mutations', async () => {
3357
const connection = createRemoteConnectionSpy();
3458
const batchingConnection = new BatchingRemoteConnection(connection);

0 commit comments

Comments
 (0)