Skip to content

Commit

Permalink
Internals: Scheduler: Fix infinite loop in flush (sveltejs#4316)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Kondrad authored and jesseskinner committed Feb 27, 2020
1 parent 24c319f commit 2ff38d4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/runtime/internal/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export function add_flush_callback(fn) {
flush_callbacks.push(fn);
}

const seen_callbacks = new Set();
export function flush() {
const seen_callbacks = new Set();

do {
// first, call beforeUpdate functions
Expand All @@ -52,10 +52,10 @@ export function flush() {
const callback = render_callbacks[i];

if (!seen_callbacks.has(callback)) {
callback();

// ...so guard against infinite loops
seen_callbacks.add(callback);

callback();
}
}

Expand All @@ -67,6 +67,7 @@ export function flush() {
}

update_scheduled = false;
seen_callbacks.clear();
}

function update($$) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Child
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
test({ assert, component }) {
const { count } = component;
assert.deepEqual(count, 1);
}
};
16 changes: 16 additions & 0 deletions test/runtime/samples/lifecycle-onmount-infinite-loop/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script>
import { onMount } from 'svelte';
import Child from './Child.svelte';
let root;
export let count = 0;
onMount(() => {
if (count < 5) {
count++;
new Child({ target: root });
}
});
</script>

<div bind:this={root}></div>

0 comments on commit 2ff38d4

Please sign in to comment.