Skip to content

Commit

Permalink
Merge pull request #3097 from sveltejs/gh-3058
Browse files Browse the repository at this point in the history
always run onDestroy functions
  • Loading branch information
Rich-Harris authored Jun 24, 2019
2 parents bb6cd30 + db722c8 commit 8431aff
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/runtime/internal/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function destroy_component(component, detaching) {
if (component.$$.fragment) {
run_all(component.$$.on_destroy);

if (detaching) component.$$.fragment.d(1);
component.$$.fragment.d(detaching);

// TODO null out other refs, including component.$$ (but need to
// preserve final state?)
Expand Down
15 changes: 15 additions & 0 deletions test/runtime/samples/ondestroy-deep/A.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script>
import { onDestroy } from 'svelte';
import { destroyed } from './destroyed.js';
import B from './B.svelte';
let yes = 1;
onDestroy(() => destroyed.push('A'));
</script>

<div>
{#if yes}
<B/>
{/if}
</div>
15 changes: 15 additions & 0 deletions test/runtime/samples/ondestroy-deep/B.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script>
import { onDestroy } from 'svelte';
import { destroyed } from './destroyed.js';
import C from './C.svelte';
let yes = 1;
onDestroy(() => destroyed.push('B'));
</script>

<div>
{#if yes}
<C/>
{/if}
</div>
8 changes: 8 additions & 0 deletions test/runtime/samples/ondestroy-deep/C.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
import { onDestroy } from 'svelte';
import { destroyed } from './destroyed.js';
let yes = 1;
onDestroy(() => destroyed.push('C'));
</script>
10 changes: 10 additions & 0 deletions test/runtime/samples/ondestroy-deep/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { destroyed, reset } from './destroyed.js';

export default {
test({ assert, component }) {
component.visible = false;
assert.deepEqual(destroyed, ['A', 'B', 'C']);

reset();
}
};
3 changes: 3 additions & 0 deletions test/runtime/samples/ondestroy-deep/destroyed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const destroyed = [];

export const reset = () => destroyed.length = 0;
9 changes: 9 additions & 0 deletions test/runtime/samples/ondestroy-deep/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
import A from './A.svelte';
export let visible = true;
</script>

{#if visible}
<A/>
{/if}

0 comments on commit 8431aff

Please sign in to comment.