Skip to content

Commit

Permalink
Refactor component.js process() (#4637)
Browse files Browse the repository at this point in the history
  • Loading branch information
jviide authored Jan 18, 2025
1 parent 28a937c commit 765f48e
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,20 +219,27 @@ const depthSort = (a, b) => a._vnode._depth - b._vnode._depth;

/** Flush the render queue by rerendering all queued components */
function process() {
let c;
rerenderQueue.sort(depthSort);
let c,
l = 1;

// Don't update `renderCount` yet. Keep its value non-zero to prevent unnecessary
// process() calls from getting scheduled while `queue` is still being consumed.
while ((c = rerenderQueue.shift())) {
while (rerenderQueue.length) {
// Keep the rerender queue sorted by (depth, insertion order). The queue
// will initially be sorted on the first iteration only if it has more than 1 item.
//
// New items can be added to the queue e.g. when rerendering a provider, so we want to
// keep the order from top to bottom with those new items so we can handle them in a
// single pass
if (rerenderQueue.length > l) {
rerenderQueue.sort(depthSort);
}

c = rerenderQueue.shift();
l = rerenderQueue.length;

if (c._dirty) {
let renderQueueLength = rerenderQueue.length;
renderComponent(c);
if (rerenderQueue.length > renderQueueLength) {
// When i.e. rerendering a provider additional new items can be injected, we want to
// keep the order from top to bottom with those new items so we can handle them in a
// single pass
rerenderQueue.sort(depthSort);
}
}
}
process._rerenderCount = 0;
Expand Down

0 comments on commit 765f48e

Please sign in to comment.