Skip to content

Commit 5fa56c6

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

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,28 @@ 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 connection = createRemoteConnectionSpy();
38+
const batchingConnection = new BatchingRemoteConnection(connection);
39+
40+
batchingConnection.mutate([1, 2, 3]);
41+
batchingConnection.mutate([4, 5, 6]);
42+
43+
expect(connection.mutate).not.toHaveBeenCalled();
44+
45+
vi.runAllTimers();
46+
47+
expect(connection.mutate).toHaveBeenCalledTimes(1);
48+
expect(connection.mutate).toHaveBeenCalledWith([1, 2, 3, 4, 5, 6]);
49+
50+
vi.restoreAllMocks();
51+
vi.useRealTimers();
52+
});
53+
3254
it('flushes mutations', async () => {
3355
const connection = createRemoteConnectionSpy();
3456
const batchingConnection = new BatchingRemoteConnection(connection);

0 commit comments

Comments
 (0)