From 00767fe8e23aad9d84c0aff6c87cadcb055439ea Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Mon, 18 May 2020 20:53:14 -0400 Subject: [PATCH] alternative fix for #4693 --- src/compiler/compile/render_dom/Block.ts | 21 ++++++++++++------- .../Component.svelte | 5 +++++ .../_config.js | 21 +++++++++++++++++++ .../main.svelte | 17 +++++++++++++++ 4 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 test/runtime/samples/each-block-keyed-component-action/Component.svelte create mode 100644 test/runtime/samples/each-block-keyed-component-action/_config.js create mode 100644 test/runtime/samples/each-block-keyed-component-action/main.svelte diff --git a/src/compiler/compile/render_dom/Block.ts b/src/compiler/compile/render_dom/Block.ts index b29b4d4afd95..b77cf6111253 100644 --- a/src/compiler/compile/render_dom/Block.ts +++ b/src/compiler/compile/render_dom/Block.ts @@ -299,7 +299,7 @@ export default class Block { ${this.chunks.mount} }`; } else { - properties.mount = x`function #mount(#target, #anchor, #remount) { + properties.mount = x`function #mount(#target, #anchor) { ${this.chunks.mount} }`; } @@ -452,6 +452,9 @@ export default class Block { render_listeners(chunk: string = '') { if (this.event_listeners.length > 0) { + this.add_variable({ type: 'Identifier', name: '#mounted' }); + this.chunks.destroy.push(b`#mounted = false`); + const dispose: Identifier = { type: 'Identifier', name: `#dispose${chunk}` @@ -462,8 +465,10 @@ export default class Block { if (this.event_listeners.length === 1) { this.chunks.mount.push( b` - if (#remount) ${dispose}(); - ${dispose} = ${this.event_listeners[0]}; + if (!#mounted) { + ${dispose} = ${this.event_listeners[0]}; + #mounted = true; + } ` ); @@ -472,10 +477,12 @@ export default class Block { ); } else { this.chunks.mount.push(b` - if (#remount) @run_all(${dispose}); - ${dispose} = [ - ${this.event_listeners} - ]; + if (!#mounted) { + ${dispose} = [ + ${this.event_listeners} + ]; + #mounted = true; + } `); this.chunks.destroy.push( diff --git a/test/runtime/samples/each-block-keyed-component-action/Component.svelte b/test/runtime/samples/each-block-keyed-component-action/Component.svelte new file mode 100644 index 000000000000..18a6c7452a01 --- /dev/null +++ b/test/runtime/samples/each-block-keyed-component-action/Component.svelte @@ -0,0 +1,5 @@ + + +
diff --git a/test/runtime/samples/each-block-keyed-component-action/_config.js b/test/runtime/samples/each-block-keyed-component-action/_config.js new file mode 100644 index 000000000000..c4421f89aea2 --- /dev/null +++ b/test/runtime/samples/each-block-keyed-component-action/_config.js @@ -0,0 +1,21 @@ +export default { + test({ assert, component, raf }) { + assert.equal(component.count, 0); + + component.arr = ["2"]; + + assert.equal(component.count, 1); + + component.arr = ["1", "2"]; + + assert.equal(component.count, 2); + + component.arr = ["2", "1"]; + + assert.equal(component.count, 2); + + component.arr = []; + + assert.equal(component.count, 0); + }, +}; diff --git a/test/runtime/samples/each-block-keyed-component-action/main.svelte b/test/runtime/samples/each-block-keyed-component-action/main.svelte new file mode 100644 index 000000000000..bfacdf402a02 --- /dev/null +++ b/test/runtime/samples/each-block-keyed-component-action/main.svelte @@ -0,0 +1,17 @@ + + +{#each arr as item (item)} + +{/each}