From c766af7541fc86fb53b0a8bde1b78cbe07cedeeb Mon Sep 17 00:00:00 2001 From: Timothy Johnson Date: Thu, 26 Mar 2020 13:40:05 -0700 Subject: [PATCH] Support lets --- src/compiler/compile/render_dom/index.ts | 2 +- src/runtime/internal/slots.ts | 39 ++++++++++++------- test/runtime/samples/$$slots-let/Child.svelte | 8 ++++ test/runtime/samples/$$slots-let/_config.js | 18 +++++++++ test/runtime/samples/$$slots-let/main.svelte | 7 ++++ 5 files changed, 58 insertions(+), 16 deletions(-) create mode 100644 test/runtime/samples/$$slots-let/Child.svelte create mode 100644 test/runtime/samples/$$slots-let/_config.js create mode 100644 test/runtime/samples/$$slots-let/main.svelte diff --git a/src/compiler/compile/render_dom/index.ts b/src/compiler/compile/render_dom/index.ts index d69c94fd0d..6ff3c37fc1 100644 --- a/src/compiler/compile/render_dom/index.ts +++ b/src/compiler/compile/render_dom/index.ts @@ -455,7 +455,7 @@ export default function dom( ${(reactive_declarations.length > 0 || uses_slots) && b` $$self.$$.update = () => { if (${renderer.dirty(['$$scope'], true)}) { - #update_$$slots($$scope, $$self.$$.dirty) + #update_$$slots($$scope) } ${reactive_declarations} diff --git a/src/runtime/internal/slots.ts b/src/runtime/internal/slots.ts index 41ab083774..e457bb7b2f 100644 --- a/src/runtime/internal/slots.ts +++ b/src/runtime/internal/slots.ts @@ -1,5 +1,5 @@ import { onDestroy } from './lifecycle'; -import { assign } from './utils' +import { assign, noop } from './utils'; export function create_slot(definition, ctx, $$scope, fn) { if (definition) { @@ -39,28 +39,37 @@ export function get_slot_changes(definition, $$scope, dirty, fn) { } export function create_slots_accessor(slots, scope) { - const slot_list = []; - function update(scope, dirty) { - slot_list.forEach(({ slot, definition }) => - slot.p( - get_slot_context(definition, [], scope, null), - get_slot_changes(definition, scope, dirty, null) - ) - ); + const update_list = []; + function update(scope) { + update_list.forEach(fn => fn(scope)); } const $$slots = {}; for (const key in slots) { - $$slots[key] = function () { - let definition = slots[key]; - let slot = create_slot(definition, [], scope, null); + $$slots[key] = function (ctx, callback = noop) { + const definition = slots[key]; + const slot = create_slot(definition, null, scope, () => ctx); + const content = slot.c(); + + function local_update (scope, ctx_fn, dirty_fn) { + slot.p( + get_slot_context(definition, null, scope, ctx_fn), + get_slot_changes(definition, scope, 0, dirty_fn) + ); + callback(content); + } if (slot.d) onDestroy(slot.d); - if (slot.p) slot_list.push({ definition, slot }); + if (slot.p) update_list.push(local_update); return { - content: slot.c(), - mount: slot.m + content, + mount: slot.m, + update: props => local_update( + scope, + () => assign(ctx, props), + () => Object.keys(props).reduce((o, k) => (o[k] = true, o), {}) + ) }; }; } diff --git a/test/runtime/samples/$$slots-let/Child.svelte b/test/runtime/samples/$$slots-let/Child.svelte new file mode 100644 index 0000000000..52bfbeaf84 --- /dev/null +++ b/test/runtime/samples/$$slots-let/Child.svelte @@ -0,0 +1,8 @@ + + + +
diff --git a/test/runtime/samples/$$slots-let/_config.js b/test/runtime/samples/$$slots-let/_config.js new file mode 100644 index 0000000000..50a74db74e --- /dev/null +++ b/test/runtime/samples/$$slots-let/_config.js @@ -0,0 +1,18 @@ +export default { + async test({ assert, target, window, }) { + assert.htmlEqual(target.innerHTML, ` + +
foo
+ `); + + const btn = target.querySelector('button'); + const clickEvent = new window.MouseEvent('click'); + + await btn.dispatchEvent(clickEvent); + + assert.htmlEqual(target.innerHTML, ` + +
bar
+ `); + } +}; diff --git a/test/runtime/samples/$$slots-let/main.svelte b/test/runtime/samples/$$slots-let/main.svelte new file mode 100644 index 0000000000..b2fdcbac5c --- /dev/null +++ b/test/runtime/samples/$$slots-let/main.svelte @@ -0,0 +1,7 @@ + + + + {value} +