Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/olive-owls-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@remote-dom/core': patch
---

return early if falling back to setTimeout for MessageChannel
1 change: 1 addition & 0 deletions packages/core/source/elements/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function createDefaultBatchFunction() {
setTimeout(() => {
queue();
}, 0);
return;
}

// `MessageChannel` trick that forces the code to run on the next task.
Expand Down
24 changes: 24 additions & 0 deletions packages/core/source/elements/tests/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ describe('BatchingRemoteConnection', () => {
expect(connection.mutate).toHaveBeenCalledWith([7, 8, 9]);
});

it('batches mutations with setTimeout when there is no MessageChannel', async () => {
vi.useFakeTimers();
vi.spyOn(globalThis, 'MessageChannel', 'get').mockReturnValue(
undefined as any,
);
const setTimeoutSpy = vi.spyOn(globalThis, 'setTimeout');
const connection = createRemoteConnectionSpy();
const batchingConnection = new BatchingRemoteConnection(connection);

batchingConnection.mutate([1, 2, 3]);
batchingConnection.mutate([4, 5, 6]);

expect(connection.mutate).not.toHaveBeenCalled();

vi.runAllTimers();

expect(setTimeoutSpy).toHaveBeenCalledTimes(1);
expect(connection.mutate).toHaveBeenCalledTimes(1);
expect(connection.mutate).toHaveBeenCalledWith([1, 2, 3, 4, 5, 6]);

vi.restoreAllMocks();
vi.useRealTimers();
});

it('flushes mutations', async () => {
const connection = createRemoteConnectionSpy();
const batchingConnection = new BatchingRemoteConnection(connection);
Expand Down