| 1093 | |
| 1094 | /** @internal */ |
| 1095 | export function batch(f: () => void): void { |
| 1096 | batchState.phase = BatchPhase.collect |
| 1097 | batchState.depth++ |
| 1098 | try { |
| 1099 | f() |
| 1100 | if (batchState.depth === 1) { |
| 1101 | for (let i = 0; i < batchState.stale.length; i++) { |
| 1102 | batchRebuildNode(batchState.stale[i]) |
| 1103 | } |
| 1104 | batchState.phase = BatchPhase.commit |
| 1105 | for (const node of batchState.notify) { |
| 1106 | node.notify() |
| 1107 | } |
| 1108 | batchState.notify.clear() |
| 1109 | } |
| 1110 | } finally { |
| 1111 | batchState.depth-- |
| 1112 | if (batchState.depth === 0) { |
| 1113 | batchState.phase = BatchPhase.disabled |
| 1114 | batchState.stale = [] |
| 1115 | } |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | function batchRebuildNode(node: NodeImpl<any>) { |
| 1120 | if (node.state === NodeState.valid) { |