-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.ts
27 lines (21 loc) · 888 Bytes
/
event.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
export type Unsubscribe = () => void
export type Emitter<T> = (value: T) => void
export type Initializer<T, X> = (emit: Emitter<T>, event: Event<T> & X) => X
export type Subscriber<T> = (input: T) => void
export type Event<T> = (subscriber: Subscriber<T>) => Unsubscribe
export default <T, X>(init: Initializer<T, X>): Event<T> & X => {
const subscribers: Subscriber<T>[] = []
const emit = (event: T) =>
subscribers.forEach(s => s(event))
const subscribe: any = (subscriber: Subscriber<T>): Unsubscribe => {
subscribers.push(subscriber)
return () => subscribers.splice(subscribers.indexOf(subscriber), 1)
}
const props = init(emit, subscribe)
if (!props) return subscribe
for (const p of Object.getOwnPropertyNames(props)) {
const desc = Object.getOwnPropertyDescriptor(props, p)
Object.defineProperty(subscribe, p, desc)
}
return subscribe
}