diff --git a/src/compile/nodes/shared/Expression.ts b/src/compile/nodes/shared/Expression.ts index 4d8e831078..22cb403f75 100644 --- a/src/compile/nodes/shared/Expression.ts +++ b/src/compile/nodes/shared/Expression.ts @@ -364,7 +364,18 @@ export default class Expression { let body = code.slice(node.body.start, node.body.end).trim(); if (node.body.type !== 'BlockStatement') { if (pending_assignments.size > 0) { - const insert = Array.from(pending_assignments).map(name => component.invalidate(name)).join('; '); + const dependencies = new Set(); + pending_assignments.forEach(name => { + if (template_scope.names.has(name)) { + template_scope.dependencies_for_name.get(name).forEach(dependency => { + dependencies.add(dependency); + }); + } else { + dependencies.add(name); + } + }); + + const insert = Array.from(dependencies).map(name => component.invalidate(name)).join('; '); pending_assignments = new Set(); component.has_reactive_assignments = true; diff --git a/test/runtime/samples/event-handler-each-context-invalidation/_config.js b/test/runtime/samples/event-handler-each-context-invalidation/_config.js new file mode 100644 index 0000000000..07e7170357 --- /dev/null +++ b/test/runtime/samples/event-handler-each-context-invalidation/_config.js @@ -0,0 +1,35 @@ +export default { + html: ` + + + +

on: 1

+ `, + + async test({ assert, component, target, window }) { + const buttons = target.querySelectorAll('button'); + const event = new window.MouseEvent('click'); + + await buttons[0].dispatchEvent(event); + assert.htmlEqual(target.innerHTML, ` + + + +

on: 2

+ `); + + await buttons[2].dispatchEvent(event); + assert.htmlEqual(target.innerHTML, ` + + + +

on: 3

+ `); + + assert.deepEqual(component.switches, [ + { on: true }, + { on: true }, + { on: true } + ]); + } +}; diff --git a/test/runtime/samples/event-handler-each-context-invalidation/main.svelte b/test/runtime/samples/event-handler-each-context-invalidation/main.svelte new file mode 100644 index 0000000000..77c6b7f197 --- /dev/null +++ b/test/runtime/samples/event-handler-each-context-invalidation/main.svelte @@ -0,0 +1,15 @@ + + +{#each switches as s} + +{/each} + +

on: {switches.filter(s => !!s.on).length}

\ No newline at end of file