Skip to content

Commit

Permalink
improve types for createEventDispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanhofer committed Feb 4, 2022
1 parent eb6fb66 commit c17384b
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/runtime/internal/lifecycle.ts
Expand Up @@ -27,12 +27,29 @@ export function onDestroy(fn: () => any) {
get_current_component().$$.on_destroy.push(fn);
}

type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void
? I
: never

type ExtractObjectValues<Object extends Record<any, any>> = Object[keyof Object]

type ConstructDispatchFunction<EventMap extends Record<string, any>, EventKey extends keyof EventMap> =
EventMap[EventKey] extends never
? (type: EventKey) => void
: (type: EventKey, detail: EventMap[EventKey]) => void

type CreateDispatchFunctionMap<EventMap> = {
[Key in keyof EventMap]: ConstructDispatchFunction<EventMap, Key>
}

type EventDispatcher<EventMap extends Record<string, any>> = UnionToIntersection<ExtractObjectValues<CreateDispatchFunctionMap<EventMap>>>

export function createEventDispatcher<
EventMap extends {} = any
>(): <EventKey extends Extract<keyof EventMap, string>>(type: EventKey, detail?: EventMap[EventKey]) => void {
EventMap extends Record<string, any> = any
>(): EventDispatcher<EventMap> {
const component = get_current_component();

return (type: string, detail?: any) => {
return ((type: string, detail?: any) => {
const callbacks = component.$$.callbacks[type];

if (callbacks) {
Expand All @@ -43,7 +60,7 @@ export function createEventDispatcher<
fn.call(component, event);
});
}
};
}) as EventDispatcher<EventMap>;
}

export function setContext<T>(key, context: T) {
Expand All @@ -59,7 +76,7 @@ export function getAllContexts<T extends Map<any, any> = Map<any, any>>(): 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
Expand Down

0 comments on commit c17384b

Please sign in to comment.