Skip to content

Commit

Permalink
alternative fix for #4693
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed May 19, 2020
1 parent 0cd40b7 commit 00767fe
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/compiler/compile/render_dom/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}`;
}
Expand Down Expand Up @@ -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}`
Expand All @@ -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;
}
`
);

Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let action;
</script>

<div use:action />
21 changes: 21 additions & 0 deletions test/runtime/samples/each-block-keyed-component-action/_config.js
Original file line number Diff line number Diff line change
@@ -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);
},
};
17 changes: 17 additions & 0 deletions test/runtime/samples/each-block-keyed-component-action/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script>
import Component from "./Component.svelte";
export let arr = [];
export let count = 0;
function action(node, params) {
count += 1;
return {
destroy() {
count -= 1;
}
};
}
</script>

{#each arr as item (item)}
<Component {action} />
{/each}

0 comments on commit 00767fe

Please sign in to comment.