Line data Source code
1 : /**
2 : * Admits every human operation to the sender synchronously, then defers only
3 : * delivery until the end of the tick. Admission pins workflow, actor, version,
4 : * and operation identity before a reactive retarget can change them, while the
5 : * deferred flush still lets the sender combine an N-node edit into one batch.
6 : */
7 : import type { GraphOperation } from './graphOperations'
8 :
9 : interface OpCoalescer {
10 : enqueue(operations: GraphOperation[]): void
11 : detach(): void
12 : }
13 :
14 1 : export function createOpCoalescer(
15 : admit: (operations: GraphOperation[]) => void,
16 : flush: () => void
17 : ): OpCoalescer {
18 1 : let flushScheduled = false
19 1 : let detached = false
20 :
21 0 : function flushAdmitted(): void {
22 0 : flushScheduled = false
23 0 : if (!detached) flush()
24 : }
25 :
26 1 : return {
27 : enqueue(operations) {
28 0 : if (detached || operations.length === 0) return
29 0 : admit(operations)
30 0 : if (flushScheduled) return
31 0 : flushScheduled = true
32 0 : void Promise.resolve().then(flushAdmitted)
33 : },
34 : detach() {
35 0 : detached = true
36 : }
37 : }
38 : }
|