()
| 568 | } |
| 569 | |
| 570 | queueUpdate(): void { |
| 571 | if (this.dirtyNodes.size === 0 || this.queuedRender) { |
| 572 | return; |
| 573 | } |
| 574 | |
| 575 | // Only trigger subscriptions once during an update, when the first item changes. |
| 576 | // React's useSyncExternalStore will call getCollection immediately, to check whether the snapshot changed. |
| 577 | // If so, React will queue a render to happen after the current commit to our fake DOM finishes. |
| 578 | // We track whether getCollection is called in a subscription, and once it is called during render, |
| 579 | // we reset queuedRender back to false. |
| 580 | this.queuedRender = true; |
| 581 | this.inSubscription = true; |
| 582 | |
| 583 | // Clone the collection to ensure that React queues a render. It will call getCollection again |
| 584 | // during render, at which point all the updates will be complete and we can return |
| 585 | // the new collection. |
| 586 | if (!this.isSSR) { |
| 587 | this.collection = this.collection.clone(); |
| 588 | } |
| 589 | |
| 590 | for (let fn of this.subscriptions) { |
| 591 | fn(); |
| 592 | } |
| 593 | |
| 594 | this.inSubscription = false; |
| 595 | } |
| 596 | |
| 597 | subscribe(fn: () => void) { |
| 598 | this.subscriptions.add(fn); |
no test coverage detected