From 164622ea64b619b0ad3a748e122dd87f87d417fd Mon Sep 17 00:00:00 2001 From: David A Kondrad Date: Fri, 24 Jan 2020 17:19:42 -0500 Subject: [PATCH 1/4] internal: scheduler: add failing test for infinite onmount loop This test shows how onMount is called repeatedly when creating a component dynamically. The loop counter is totally arbitrary and is there to prevent a hard-crash in the CI. --- .../lifecycle-onmount-infinite-loop/Child.svelte | 1 + .../lifecycle-onmount-infinite-loop/_config.js | 6 ++++++ .../lifecycle-onmount-infinite-loop/main.svelte | 16 ++++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 test/runtime/samples/lifecycle-onmount-infinite-loop/Child.svelte create mode 100644 test/runtime/samples/lifecycle-onmount-infinite-loop/_config.js create mode 100644 test/runtime/samples/lifecycle-onmount-infinite-loop/main.svelte diff --git a/test/runtime/samples/lifecycle-onmount-infinite-loop/Child.svelte b/test/runtime/samples/lifecycle-onmount-infinite-loop/Child.svelte new file mode 100644 index 000000000000..c1a538c62a2e --- /dev/null +++ b/test/runtime/samples/lifecycle-onmount-infinite-loop/Child.svelte @@ -0,0 +1 @@ +Child \ No newline at end of file diff --git a/test/runtime/samples/lifecycle-onmount-infinite-loop/_config.js b/test/runtime/samples/lifecycle-onmount-infinite-loop/_config.js new file mode 100644 index 000000000000..a76a2686ac27 --- /dev/null +++ b/test/runtime/samples/lifecycle-onmount-infinite-loop/_config.js @@ -0,0 +1,6 @@ +export default { + test({ assert, component }) { + const { count } = component; + assert.deepEqual(count, 1); + } +}; diff --git a/test/runtime/samples/lifecycle-onmount-infinite-loop/main.svelte b/test/runtime/samples/lifecycle-onmount-infinite-loop/main.svelte new file mode 100644 index 000000000000..ba2da663d6fb --- /dev/null +++ b/test/runtime/samples/lifecycle-onmount-infinite-loop/main.svelte @@ -0,0 +1,16 @@ + + +
\ No newline at end of file From 7f85b929d8610d6f474e3d960776207a60c2577c Mon Sep 17 00:00:00 2001 From: David A Kondrad Date: Fri, 24 Jan 2020 16:58:56 -0500 Subject: [PATCH 2/4] internal: scheduler: fix usage of seen_callbacks The set that was added to block infinite loops wasn't actually doing anything. Lift the declaration outside of the function and clear it at the end. --- src/runtime/internal/scheduler.ts | 7 ++++--- .../samples/lifecycle-onmount-infinite-loop/Child.svelte | 2 +- .../samples/lifecycle-onmount-infinite-loop/main.svelte | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/runtime/internal/scheduler.ts b/src/runtime/internal/scheduler.ts index 04c1264ab1d2..1ce255b21772 100644 --- a/src/runtime/internal/scheduler.ts +++ b/src/runtime/internal/scheduler.ts @@ -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 @@ -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(); } } @@ -67,6 +67,7 @@ export function flush() { } update_scheduled = false; + seen_callbacks.clear(); } function update($$) { diff --git a/test/runtime/samples/lifecycle-onmount-infinite-loop/Child.svelte b/test/runtime/samples/lifecycle-onmount-infinite-loop/Child.svelte index c1a538c62a2e..1772cb651325 100644 --- a/test/runtime/samples/lifecycle-onmount-infinite-loop/Child.svelte +++ b/test/runtime/samples/lifecycle-onmount-infinite-loop/Child.svelte @@ -1 +1 @@ -Child \ No newline at end of file +Child diff --git a/test/runtime/samples/lifecycle-onmount-infinite-loop/main.svelte b/test/runtime/samples/lifecycle-onmount-infinite-loop/main.svelte index ba2da663d6fb..293e37d4f1f0 100644 --- a/test/runtime/samples/lifecycle-onmount-infinite-loop/main.svelte +++ b/test/runtime/samples/lifecycle-onmount-infinite-loop/main.svelte @@ -13,4 +13,4 @@ }); -
\ No newline at end of file +
From b6ca05c89b6c0cad2dd7ab033de6f6d4feca9356 Mon Sep 17 00:00:00 2001 From: Conduitry Date: Sat, 25 Jan 2020 12:18:11 -0500 Subject: [PATCH 3/4] tidy --- .../samples/lifecycle-onmount-infinite-loop/Child.svelte | 2 +- .../runtime/samples/lifecycle-onmount-infinite-loop/main.svelte | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/runtime/samples/lifecycle-onmount-infinite-loop/Child.svelte b/test/runtime/samples/lifecycle-onmount-infinite-loop/Child.svelte index 1772cb651325..ef16875b6448 100644 --- a/test/runtime/samples/lifecycle-onmount-infinite-loop/Child.svelte +++ b/test/runtime/samples/lifecycle-onmount-infinite-loop/Child.svelte @@ -1 +1 @@ -Child +Child diff --git a/test/runtime/samples/lifecycle-onmount-infinite-loop/main.svelte b/test/runtime/samples/lifecycle-onmount-infinite-loop/main.svelte index 293e37d4f1f0..8476ef1dd559 100644 --- a/test/runtime/samples/lifecycle-onmount-infinite-loop/main.svelte +++ b/test/runtime/samples/lifecycle-onmount-infinite-loop/main.svelte @@ -8,7 +8,7 @@ onMount(() => { if (count < 5) { count++; - let child = new Child({target:root}); + const child = new Child({ target:root }); } }); From 1b0dd2a0d9797c66e96876f12f7eacbd61e5c6bb Mon Sep 17 00:00:00 2001 From: Conduitry Date: Sat, 25 Jan 2020 12:19:36 -0500 Subject: [PATCH 4/4] tidy pt 2 --- .../runtime/samples/lifecycle-onmount-infinite-loop/main.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtime/samples/lifecycle-onmount-infinite-loop/main.svelte b/test/runtime/samples/lifecycle-onmount-infinite-loop/main.svelte index 8476ef1dd559..1fa4263e39ea 100644 --- a/test/runtime/samples/lifecycle-onmount-infinite-loop/main.svelte +++ b/test/runtime/samples/lifecycle-onmount-infinite-loop/main.svelte @@ -8,7 +8,7 @@ onMount(() => { if (count < 5) { count++; - const child = new Child({ target:root }); + new Child({ target: root }); } });