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

fix: beforeUpdate called twice with bound reference #6858

Closed
Show file tree
Hide file tree
Changes from 4 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
18 changes: 12 additions & 6 deletions src/runtime/internal/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export function add_flush_callback(fn) {

let flushing = false;
const seen_callbacks = new Set();
let previous_dirty_components = [];
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved

export function flush() {
if (flushing) return;
flushing = true;
Expand All @@ -43,14 +45,18 @@ export function flush() {
for (let i = 0; i < dirty_components.length; i += 1) {
const component = dirty_components[i];
set_current_component(component);
update(component.$$);
const is_previous_dirty = previous_dirty_components.indexOf(component) > -1;
update(component.$$, is_previous_dirty);
}
previous_dirty_components = [];
set_current_component(null);

dirty_components.length = 0;

while (binding_callbacks.length) binding_callbacks.pop()();

while (binding_callbacks.length) {
binding_callbacks.pop()();
}
previous_dirty_components = [...dirty_components];
// then, once components are updated, call
// afterUpdate functions. This may cause
// subsequent updates...
Expand All @@ -77,14 +83,14 @@ export function flush() {
seen_callbacks.clear();
}

function update($$) {
function update($$, is_previous_dirty) {
if ($$.fragment !== null) {
$$.update();
run_all($$.before_update);
if (!is_previous_dirty) run_all($$.before_update);
const dirty = $$.dirty;
$$.dirty = [-1];
$$.fragment && $$.fragment.p($$.ctx, dirty);

// if (!is_previous_dirty) run_all($$.after_update);
$$.after_update.forEach(add_render_callback);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<script>
import { onMount, beforeUpdate, afterUpdate } from 'svelte';
import order from './order.js';

let i;
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 bind:this={i}>
{logRender()}
</li>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import order from './order.js';

export default {
skip_if_ssr: true,
before_test() {
order.length = 0;
},
test({ assert, compileOptions }) {
if (compileOptions.hydratable) {
assert.deepEqual(order, [
'0: beforeUpdate',
'0: render',
'1: beforeUpdate',
'1: render',
'2: beforeUpdate',
'2: render',
'3: beforeUpdate',
'3: render',
'1: onMount',
'1: afterUpdate',
'2: onMount',
'2: afterUpdate',
'3: onMount',
'3: afterUpdate',
'0: onMount',
'0: afterUpdate'
]);
} else {
assert.deepEqual(order, [
'0: beforeUpdate',
'0: render',
'1: beforeUpdate',
'2: beforeUpdate',
'3: beforeUpdate',
'1: render',
'2: render',
'3: render',
'1: onMount',
'1: afterUpdate',
'2: onMount',
'2: afterUpdate',
'3: onMount',
'3: afterUpdate',
'0: onMount',
'0: afterUpdate'
]);
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<script>
import { onMount, beforeUpdate, afterUpdate } from 'svelte';
import order from './order.js';
import Item from './Item.svelte';

const parentIndex = 0;
let ul;
let ulW;
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 bind:this={ul} bind:clientWidth={ulW}>
{#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 [];