-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
lifecycle.ts
118 lines (90 loc) · 3.08 KB
/
lifecycle.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { custom_event } from './dom';
export let current_component;
export function set_current_component(component) {
current_component = component;
}
export function get_current_component() {
if (!current_component) throw new Error('Function called outside component initialization');
return current_component;
}
export function beforeUpdate(fn: () => any) {
get_current_component().$$.before_update.push(fn);
}
export function onMount(fn: () => any) {
get_current_component().$$.on_mount.push(fn);
}
export function afterUpdate(fn: () => any) {
get_current_component().$$.after_update.push(fn);
}
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
: undefined extends EventMap[EventKey]
? (type: EventKey, detail?: EventMap[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 Record<string, any> = any
>(): EventDispatcher<EventMap> {
const component = get_current_component();
return ((type: string, detail?: any) => {
const callbacks = component.$$.callbacks[type];
if (callbacks) {
// TODO are there situations where events could be dispatched
// in a server (non-DOM) environment?
const event = custom_event(type, detail);
callbacks.slice().forEach(fn => {
fn.call(component, event);
});
}
}) as EventDispatcher<EventMap>;
}
const dispatch = createEventDispatcher<{
loaded: never
change: string
valid: boolean
}>();
// @ts-expect-error
dispatch('some-event');
dispatch('loaded');
// @ts-expect-error
dispatch('loaded', 123);
dispatch('change', 'string');
// @ts-expect-error
dispatch('change', 123);
dispatch('valid', true);
// @ts-expect-error
dispatch('valid', 'string');
dispatch('change', undefined);
export function setContext<T>(key, context: T) {
get_current_component().$$.context.set(key, context);
}
export function getContext<T>(key): T {
return get_current_component().$$.context.get(key);
}
export function getAllContexts<T extends Map<any, any> = Map<any, any>>(): T {
return get_current_component().$$.context;
}
export function hasContext(key): boolean {
return get_current_component().$$.context.has(key);
}
// TODO figure out if we still want to support
// 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));
}
}