From e81281c3f2b9f64fbb5329fa8764a5853ee3f6a1 Mon Sep 17 00:00:00 2001 From: Tom Shaw Date: Fri, 22 Oct 2021 21:40:25 +0100 Subject: [PATCH] resolve #2837 added dynamic event bindings --- src/runtime/internal/Component.ts | 7 +++++++ src/runtime/internal/lifecycle.ts | 9 ++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/runtime/internal/Component.ts b/src/runtime/internal/Component.ts index 3e48010547..c3aabdfbd0 100644 --- a/src/runtime/internal/Component.ts +++ b/src/runtime/internal/Component.ts @@ -237,6 +237,13 @@ if (typeof HTMLElement === 'function') { export class SvelteComponent { $$: T$$; $$set?: ($$props: any) => void; + eventBindings: any; + + constructor(props) { + if (props && props.props && Object.prototype.hasOwnProperty.call(props.props, 'event:bindings')) { + this.eventBindings = props.props['event:bindings']; + } + } $destroy() { destroy_component(this, 1); diff --git a/src/runtime/internal/lifecycle.ts b/src/runtime/internal/lifecycle.ts index bb3df3d295..a26453cd5f 100644 --- a/src/runtime/internal/lifecycle.ts +++ b/src/runtime/internal/lifecycle.ts @@ -34,6 +34,7 @@ export function createEventDispatcher< return (type: string, detail?: any) => { const callbacks = component.$$.callbacks[type]; + const eventBinding = component.eventBindings && Object.prototype.hasOwnProperty.call(component.eventBindings, type) ? component.eventBindings[type] : undefined; if (callbacks) { // TODO are there situations where events could be dispatched @@ -43,6 +44,12 @@ export function createEventDispatcher< fn.call(component, event); }); } + + if (eventBinding) { + // in a server (non-DOM) environment? + const event = custom_event(type, detail); + eventBinding.call(component, event); + } }; } @@ -59,7 +66,7 @@ export function getAllContexts = Map>(): T { } export function hasContext(key): boolean { - return get_current_component().$$.context.has(key); + return get_current_component().$$.context.has(key); } // TODO figure out if we still want to support