-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathindex.d.ts
105 lines (98 loc) · 2.15 KB
/
index.d.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
interface EventsMap {
[event: string]: any
}
interface DefaultEvents extends EventsMap {
[event: string]: (...args: any) => void
}
export interface Unsubscribe {
(): void
}
export interface Emitter<Events extends EventsMap = DefaultEvents> {
/**
* Calls each of the listeners registered for a given event.
*
* ```js
* ee.emit('tick', tickType, tickDuration)
* ```
*
* @param event The event name.
* @param args The arguments for listeners.
*/
emit<K extends keyof Events>(
this: this,
event: K,
...args: Parameters<Events[K]>
): void
/**
* Event names in keys and arrays with listeners in values.
*
* ```js
* emitter1.events = emitter2.events
* emitter2.events = { }
* ```
*/
events: Partial<{ [E in keyof Events]: Events[E][] }>
/**
* Add a listener for a given event.
*
* ```js
* const unbind = ee.on('tick', (tickType, tickDuration) => {
* count += 1
* })
*
* disable () {
* unbind()
* }
* ```
*
* @param event The event name.
* @param cb The listener function.
* @returns Unbind listener from event.
*/
on<K extends keyof Events>(this: this, event: K, cb: Events[K]): Unsubscribe
}
/**
* Create event emitter.
*
* ```js
* import { createNanoEvents } from 'nanoevents'
*
* class Ticker {
* constructor() {
* this.emitter = createNanoEvents()
* }
* on(...args) {
* return this.emitter.on(...args)
* }
* tick() {
* this.emitter.emit('tick')
* }
* }
* ```
*/
export function createNanoEvents<
Events extends EventsMap = DefaultEvents
>(): Emitter<Events>
/**
* An interface for mixins that expose the `on` function (without the emitter
* bound to `this`)
*
* ```js
* import { createNanoEvents } from 'nanoevents'
*
* class Ticker implements EmitterMixin<Events> {
* constructor() {
* this.emitter = createNanoEvents()
* }
* on(...args) {
* return this.emitter.on(...args)
* }
* tick() {
* this.emitter.emit('tick')
* }
* }
* ```
*/
export interface EmitterMixin<Events extends EventsMap = DefaultEvents> {
on<K extends keyof Events>(event: K, cb: Events[K]): Unsubscribe
}