Skip to content

Commit 8d805a0

Browse files
committed
onMount before first afterUpdate
1 parent 3595f50 commit 8d805a0

File tree

6 files changed

+27
-26
lines changed

6 files changed

+27
-26
lines changed

src/runtime/internal/Component.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ interface T$$ {
1111
bound: any;
1212
update: () => void;
1313
callbacks: any;
14-
after_render: any[];
14+
after_update: any[];
1515
props: any;
1616
fragment: null|any;
1717
not_equal: any;
18-
before_render: any[];
18+
before_update: any[];
1919
context: Map<any, any>;
2020
on_mount: any[];
2121
on_destroy: any[];
@@ -28,15 +28,11 @@ export function bind(component, name, callback) {
2828
}
2929

3030
export function mount_component(component, target, anchor) {
31-
const { fragment, on_mount, on_destroy, after_render } = component.$$;
31+
const { fragment, on_mount, on_destroy, after_update } = component.$$;
3232

3333
fragment.m(target, anchor);
3434

35-
// afterUpdate callbacks happen in reverse order (inner first) so
36-
// reverse their position so callbacks are properly ordered
37-
after_render.reverse().forEach(add_render_callback);
38-
39-
// onMount happens after the initial afterUpdate
35+
// onMount happens before the initial afterUpdate
4036
add_render_callback(() => {
4137
const new_on_destroy = on_mount.map(run).filter(is_function);
4238
if (on_destroy) {
@@ -48,6 +44,8 @@ export function mount_component(component, target, anchor) {
4844
}
4945
component.$$.on_mount = [];
5046
});
47+
48+
after_update.forEach(add_render_callback);
5149
}
5250

5351
export function destroy_component(component, detaching) {
@@ -91,8 +89,8 @@ export function init(component, options, instance, create_fragment, not_equal, p
9189
// lifecycle
9290
on_mount: [],
9391
on_destroy: [],
94-
before_render: [],
95-
after_render: [],
92+
before_update: [],
93+
after_update: [],
9694
context: new Map(parent_component ? parent_component.$$.context : []),
9795

9896
// everything else
@@ -113,7 +111,7 @@ export function init(component, options, instance, create_fragment, not_equal, p
113111

114112
$$.update();
115113
ready = true;
116-
run_all($$.before_render);
114+
run_all($$.before_update);
117115
$$.fragment = create_fragment($$.ctx);
118116

119117
if (options.target) {

src/runtime/internal/lifecycle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ function get_current_component() {
1212
}
1313

1414
export function beforeUpdate(fn) {
15-
get_current_component().$$.before_render.push(fn);
15+
get_current_component().$$.before_update.push(fn);
1616
}
1717

1818
export function onMount(fn) {
1919
get_current_component().$$.on_mount.push(fn);
2020
}
2121

2222
export function afterUpdate(fn) {
23-
get_current_component().$$.after_render.push(fn);
23+
get_current_component().$$.after_update.push(fn);
2424
}
2525

2626
export function onDestroy(fn) {

src/runtime/internal/scheduler.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,18 @@ export function flush() {
4848
// then, once components are updated, call
4949
// afterUpdate functions. This may cause
5050
// subsequent updates...
51-
while (render_callbacks.length) {
52-
const callback = render_callbacks.shift();
51+
for (let i = 0; i < render_callbacks.length; i += 1) {
52+
const callback = render_callbacks[i];
53+
5354
if (!seen_callbacks.has(callback)) {
5455
callback();
5556

5657
// ...so guard against infinite loops
5758
seen_callbacks.add(callback);
5859
}
5960
}
61+
62+
render_callbacks.length = 0;
6063
} while (dirty_components.length);
6164

6265
while (flush_callbacks.length) {
@@ -69,10 +72,10 @@ export function flush() {
6972
function update($$) {
7073
if ($$.fragment) {
7174
$$.update($$.dirty);
72-
run_all($$.before_render);
75+
run_all($$.before_update);
7376
$$.fragment.p($$.dirty, $$.ctx);
7477
$$.dirty = null;
7578

76-
$$.after_render.forEach(add_render_callback);
79+
$$.after_update.forEach(add_render_callback);
7780
}
7881
}

src/runtime/internal/ssr.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ export function create_ssr_component(fn) {
7878

7979
// these will be immediately discarded
8080
on_mount: [],
81-
before_render: [],
82-
after_render: [],
81+
before_update: [],
82+
after_update: [],
8383
callbacks: blank_object()
8484
};
8585

test/runtime/samples/lifecycle-render-order-for-children/_config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ export default {
1313
'2: render',
1414
'3: beforeUpdate',
1515
'3: render',
16-
'1: afterUpdate',
1716
'1: onMount',
18-
'2: afterUpdate',
17+
'1: afterUpdate',
1918
'2: onMount',
20-
'3: afterUpdate',
19+
'2: afterUpdate',
2120
'3: onMount',
22-
'0: afterUpdate',
21+
'3: afterUpdate',
2322
'0: onMount',
23+
'0: afterUpdate'
2424
]);
2525

2626
order.length = 0;

test/runtime/samples/lifecycle-render-order/_config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import order from './order.js';
33
export default {
44
skip_if_ssr: true,
55

6-
test({ assert, component, target }) {
6+
test({ assert }) {
77
assert.deepEqual(order, [
88
'beforeUpdate',
99
'render',
10-
'afterUpdate',
11-
'onMount'
10+
'onMount',
11+
'afterUpdate'
1212
]);
1313

1414
order.length = 0;

0 commit comments

Comments
 (0)