Skip to content

Commit fd7586c

Browse files
committed
Add extensive explanatory comments
As requested during code review, here's an extensive list of comments explaining the flush() function in more detail for future maintainers.
1 parent 91415bf commit fd7586c

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/runtime/internal/scheduler.ts

+18
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ export function add_flush_callback(fn) {
3131
flush_callbacks.push(fn);
3232
}
3333

34+
// flush() calls callbacks in this order:
35+
// 1. All beforeUpdate callbacks, in order: parents before children
36+
// 2. All bind:this callbacks, in reverse order: children before parents.
37+
// 3. All afterUpdate callbacks, in order: parents before children. EXCEPT
38+
// for afterUpdates called during the initial onMount, which are called in
39+
// reverse order: children before parents.
40+
// Since callbacks might update component values, which could trigger another
41+
// call to flush(), the following steps guard against this:
42+
// 1. During beforeUpdate, any updated components will be added to the
43+
// dirty_components array and will cause a reentrant call to flush(). Because
44+
// the flush index is kept outside the function, the reentrant call will pick
45+
// up where the earlier call left off and go through all dirty components. The
46+
// current_component value is saved and restored so that the reentrant call will
47+
// not interfere with the "parent" flush() call.
48+
// 2. bind:this callbacks cannot trigger new flush() calls.
49+
// 3. During afterUpdate, any updated components will NOT have their afterUpdate
50+
// callback called a second time; the seen_callbacks set, outside the flush()
51+
// function, guarantees this behavior.
3452
const seen_callbacks = new Set();
3553
let flushidx = 0; // Do *not* move this inside the flush() function
3654
export function flush() {

0 commit comments

Comments
 (0)