This repository was archived by the owner on Feb 6, 2026. It is now read-only.
forked from extend-chrome/jest-chrome
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-event.ts
More file actions
214 lines (198 loc) · 5.26 KB
/
Copy pathcreate-event.ts
File metadata and controls
214 lines (198 loc) · 5.26 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
export interface EventCallback {
(...args: any[]): void
}
export interface EventSelector<C extends EventCallback> {
(...args: any[]): Parameters<C>
}
export interface MonotypeEventSelector<C extends EventCallback> {
(...args: Parameters<C>): Parameters<C>
}
export interface OptionsSelector {
(...options: any[]): any[]
}
/** An object which allows the addition, removal, and invocation of listener functions. */
type CoreEvent<C extends EventCallback> = {
/**
* Registers an event listener callback to an event.
* @param callback Called when an event occurs. The parameters of this function depend on the type of event.
* The callback parameter should be a function that looks like this:
* function() {...};
*/
addListener(callback: C): void
/**
* @param callback Listener whose registration status shall be tested.
*/
hasListener(callback: C): boolean
hasListeners(): boolean
/**
* Deregisters an event listener callback from an event.
* @param callback Listener that shall be unregistered.
* The callback parameter should be a function that looks like this:
* function() {...};
*/
removeListener(callback: C): void
}
type Callable<
C extends EventCallback,
R extends MonotypeEventSelector<C> | EventSelector<C>
> = {
/**
* Calls all listeners with a data argument.
*/
callListeners: (...args: Parameters<R>) => void
/**
* Remove all listeners
*/
clearListeners(): void
/**
* Get the listener Set
*/
getListeners(): Set<C>
/**
* Returns CallableEvent without callListeners
*/
toEvent(): CoreEvent<C>
}
export type CallableEvent<
C extends EventCallback,
R extends MonotypeEventSelector<C> | EventSelector<C>
> = CoreEvent<C> & Callable<C, R>
export const createEvent = <C extends EventCallback>(
// Use to map the arguments for Event#callListeners
selector: EventSelector<C>,
// Use to validate extra option arguments
validator?: OptionsSelector,
) => {
if (validator) {
return createMapEvent(selector, validator)
} else {
return createSetEvent(selector)
}
}
/**
* Create an event that takes a single callback as an argument
*
* @export
* @template C The callback signature
* @template R The event selector signature
* @param {R} selector Validate a call to callListeners and map it to the callback arguments
* @returns {CallableEvent<C, R>}
*/
export function createSetEvent<
C extends EventCallback,
R extends EventSelector<C> | MonotypeEventSelector<C>
>(selector: R): CallableEvent<C, R> {
const _cbs = new Set<C>()
return {
addListener,
hasListener,
hasListeners,
callListeners,
clearListeners,
getListeners,
removeListener,
toEvent() {
return {
addListener,
hasListener,
hasListeners,
removeListener,
}
},
}
function addListener(cb: C) {
_cbs.add(cb)
}
function hasListener(cb: C) {
return _cbs.has(cb)
}
function hasListeners() {
return _cbs.size > 0
}
function callListeners(...args: Parameters<R>) {
// eslint-disable-next-line
// @ts-ignore
const cbArgs = selector(...args)
if (cbArgs) {
_cbs.forEach((cb) => {
cb(...cbArgs)
})
}
}
function removeListener(cb: C) {
_cbs.delete(cb)
}
function clearListeners() {
_cbs.clear()
}
function getListeners() {
return new Set(_cbs.values())
}
}
/**
* Create an event that takes a single callback and any number of option arguments
*
* @export
* @template C The listener callback signature. Will be called with the result of the event selector
* @template E The event selector function signature. Will be called with the result of the options selector and the arguments for callListeners
* @template O The options selector function signature. Will be called with the options arguments from addListener
* @param {E} eventSelector Validate a call to callListeners and map it to the callback arguments
* @param {O} [optionsSelector] Validate the options arguments passed to addListener
* @returns {CallableEvent<C, E>}
*/
export function createMapEvent<
C extends EventCallback,
E extends EventSelector<C> | MonotypeEventSelector<C>,
O extends OptionsSelector
>(eventSelector: E, optionsSelector?: O): CallableEvent<C, E> {
const _cbs: Map<C, any[]> = new Map()
return {
addListener,
hasListener,
hasListeners,
callListeners,
clearListeners,
getListeners,
removeListener,
toEvent() {
return {
addListener,
hasListener,
hasListeners,
removeListener,
}
},
}
function addListener(cb: C, ...options: any[]) {
const _options =
(typeof optionsSelector === 'function' &&
optionsSelector(...options)) ||
options
_cbs.set(cb, _options)
}
function hasListener(cb: C) {
return _cbs.has(cb)
}
function hasListeners() {
return _cbs.size > 0
}
function callListeners(...args: any[]) {
_cbs.forEach((options, cb) => {
// eslint-disable-next-line
// @ts-ignore
const cbArgs = eventSelector(...options, ...args)
if (cbArgs) {
cb(...cbArgs)
}
})
}
function removeListener(cb: C) {
_cbs.delete(cb)
}
function clearListeners() {
_cbs.clear()
}
function getListeners() {
return new Set(_cbs.keys())
}
}