-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Add
BatchingRemoteConnection
helper for batching changes to a polyf…
…illed DOM (#394)
- Loading branch information
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@remote-dom/core': minor | ||
--- | ||
|
||
Add `BatchingRemoteConnection` helper for batching changes to a polyfilled DOM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type {RemoteConnection, RemoteMutationRecord} from '../types.ts'; | ||
|
||
/** | ||
* A wrapper around a `RemoteConnection` that batches mutations. By default, this | ||
* all calls are flushed in a queued microtask, but this can be customized by passing | ||
* a custom `batch` option. | ||
*/ | ||
export class BatchingRemoteConnection { | ||
readonly #connection: RemoteConnection; | ||
#queued: RemoteMutationRecord[] | undefined; | ||
#batch: (action: () => void) => void; | ||
|
||
constructor( | ||
connection: RemoteConnection, | ||
{ | ||
batch = createDefaultBatchFunction(), | ||
}: {batch?: (action: () => void) => void} = {}, | ||
) { | ||
this.#connection = connection; | ||
this.#batch = batch; | ||
} | ||
|
||
mutate(records: any[]) { | ||
let queued = this.#queued; | ||
|
||
if (queued) { | ||
queued.push(...records); | ||
return; | ||
} | ||
|
||
queued = [...records]; | ||
this.#queued = queued; | ||
|
||
this.#batch(() => { | ||
this.#connection.mutate(queued); | ||
this.#queued = undefined; | ||
}); | ||
} | ||
} | ||
|
||
function createDefaultBatchFunction() { | ||
let channel: MessageChannel; | ||
|
||
return (queue: () => void) => { | ||
// In environments without a `MessageChannel`, use a `setTimeout` fallback. | ||
if (typeof MessageChannel !== 'function') { | ||
setTimeout(() => { | ||
queue(); | ||
}, 0); | ||
} | ||
|
||
// `MessageChannel` trick that forces the code to run on the next task. | ||
channel ??= new MessageChannel(); | ||
channel.port1.onmessage = () => { | ||
queue(); | ||
}; | ||
channel.port2.postMessage(null); | ||
}; | ||
} |