diff --git a/src/runtime/internal/lifecycle.ts b/src/runtime/internal/lifecycle.ts index 6008b687f0..c60e4dffec 100644 --- a/src/runtime/internal/lifecycle.ts +++ b/src/runtime/internal/lifecycle.ts @@ -1,4 +1,4 @@ -import { custom_event } from './dom'; +import {custom_event} from './dom'; export let current_component; @@ -27,15 +27,13 @@ export function onDestroy(fn: () => any) { get_current_component().$$.on_destroy.push(fn); } -export function createEventDispatcher< - EventMap extends {} = any ->(): >(type: EventKey, detail?: EventMap[EventKey]) => void { +export function createEventDispatcher(): >(type: EventKey, detail?: EventMap[EventKey]) => void { const component = get_current_component(); 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; - const catchAll = component.eventBindings && Object.prototype.hasOwnProperty.call(component.eventBindings, '*') ? component.eventBindings['*'] : undefined; + const catchAll = component.eventBindings && Object.prototype.hasOwnProperty.call(component.eventBindings, '*') ? component.eventBindings['*'] : undefined; const catchAllBinding = component.$$.callbacks && Object.prototype.hasOwnProperty.call(component.$$.callbacks, '*') ? component.$$.callbacks['*'] : undefined; if (callbacks) { @@ -51,7 +49,8 @@ export function createEventDispatcher< // in a server (non-DOM) environment? try { const event = custom_event(type, detail); - eventBinding[0].call(component, event, eventBinding[1].data); + const data = eventBinding[1] && Object.prototype.hasOwnProperty.call(eventBinding[1], 'data') ? eventBinding[1].data : {}; + eventBinding[0].call(component, event, data); } catch (e) { console.warn(`A component was instantiated with invalid event:bindings - ${e}`); } @@ -61,7 +60,8 @@ export function createEventDispatcher< // in a server (non-DOM) environment? try { const event = custom_event(type, detail); - catchAll[0].call(component, event, catchAll[1].data); + const data = catchAll[1] && Object.prototype.hasOwnProperty.call(catchAll[1], 'data') ? catchAll[1].data : {}; + catchAll[0].call(component, event, data); } catch (e) { console.warn(`A component was instantiated with invalid event:bindings - ${e}`); } @@ -71,7 +71,8 @@ export function createEventDispatcher< // in a server (non-DOM) environment? try { const event = custom_event(type, detail); - catchAllBinding[0].call(component, event, catchAllBinding[1].data); + const data = catchAllBinding[1] && Object.prototype.hasOwnProperty.call(catchAllBinding[1], 'data') ? catchAllBinding[1].data : {}; + catchAllBinding[0].call(component, event, data); } catch (e) { console.warn(`A component was instantiated with invalid on:* configuration - ${e}`); } @@ -95,14 +96,9 @@ export function hasContext(key): boolean { return get_current_component().$$.context.has(key); } -// TODO figure out if we still want to support +// TODO figure out if we still want to support - yes we do :) // shorthand events, or if we want to implement // a real bubbling mechanism export function bubble(component, event) { - const callbacks = component.$$.callbacks[event.type]; - - if (callbacks) { - // @ts-ignore - callbacks.slice().forEach(fn => fn.call(this, event)); - } + component.$$.ctx[0]['event:bindings'][event.type][0](component.$$.ctx[0]['event:bindings'][event.type][1]); }