Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger onMount callbacks in same order as child components #2752

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/internal/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ export function mount_component(component, target, anchor) {

fragment.m(target, anchor);

// onMount happens after the initial afterUpdate. Because
// afterUpdate callbacks happen in reverse order (inner first)
// we schedule onMount callbacks before afterUpdate callbacks
// afterUpdate callbacks happen in reverse order (inner first) so
// reverse their position so callbacks are properly ordered
after_render.reverse().forEach(add_render_callback);

// onMount happens after the initial afterUpdate
add_render_callback(() => {
const new_on_destroy = on_mount.map(run).filter(is_function);
if (on_destroy) {
Expand All @@ -28,8 +30,6 @@ export function mount_component(component, target, anchor) {
}
component.$$.on_mount = [];
});

after_render.forEach(add_render_callback);
}

function destroy(component, detaching) {
Expand Down
2 changes: 1 addition & 1 deletion src/internal/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function flush() {
// afterUpdate functions. This may cause
// subsequent updates...
while (render_callbacks.length) {
const callback = render_callbacks.pop();
const callback = render_callbacks.shift();
if (!seen_callbacks.has(callback)) {
callback();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<script>
import { onMount, beforeUpdate, afterUpdate } from 'svelte';
import order from './order.js';

export let index;
export let id;
export let name;

function logRender () {
order.push(`${index}: render`);
return index;
}

beforeUpdate(() => {
order.push(`${index}: beforeUpdate`);
});

afterUpdate(() => {
order.push(`${index}: afterUpdate`);
});

onMount(() => {
order.push(`${index}: onMount`);
});
</script>

<li>
{logRender()}
</li>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import order from './order.js';

export default {
skip_if_ssr: true,

test({ assert, component, target }) {
assert.deepEqual(order, [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the order you'd expect here @Rich-Harris?

'0: beforeUpdate',
'0: render',
'1: beforeUpdate',
'1: render',
'2: beforeUpdate',
'2: render',
'3: beforeUpdate',
'3: render',
'1: afterUpdate',
'1: onMount',
'2: afterUpdate',
'2: onMount',
'3: afterUpdate',
'3: onMount',
'0: afterUpdate',
'0: onMount',
]);

order.length = 0;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<script>
import { onMount, beforeUpdate, afterUpdate } from 'svelte';
import order from './order.js';
import Item from './Item.svelte';

const parentIndex = 0;

function logRender () {
order.push(`${parentIndex}: render`);
return parentIndex;
}

beforeUpdate(() => {
order.push(`${parentIndex}: beforeUpdate`);
});

afterUpdate(() => {
order.push(`${parentIndex}: afterUpdate`);
});

onMount(() => {
order.push(`${parentIndex}: onMount`);
})
</script>

{logRender()}
<ul>
{#each [1,2,3] as index}
<Item {index} />
{/each}
</ul>


Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default [];