hookified 1.4.0 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +451 -4
- package/dist/browser/index.global.js +1 -1
- package/dist/browser/index.global.js.map +1 -1
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/node/index.cjs +134 -14
- package/dist/node/index.d.cts +125 -4
- package/dist/node/index.d.ts +125 -4
- package/dist/node/index.js +134 -14
- package/package.json +6 -6
package/dist/node/index.js
CHANGED
|
@@ -6,6 +6,12 @@ var Eventified = class {
|
|
|
6
6
|
this._eventListeners = /* @__PURE__ */ new Map();
|
|
7
7
|
this._maxListeners = 100;
|
|
8
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* Adds a handler function for a specific event that will run only once
|
|
11
|
+
* @param {string | symbol} eventName
|
|
12
|
+
* @param {EventListener} listener
|
|
13
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
14
|
+
*/
|
|
9
15
|
once(eventName, listener) {
|
|
10
16
|
const onceListener = (...arguments_) => {
|
|
11
17
|
this.off(eventName, onceListener);
|
|
@@ -14,6 +20,11 @@ var Eventified = class {
|
|
|
14
20
|
this.on(eventName, onceListener);
|
|
15
21
|
return this;
|
|
16
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Gets the number of listeners for a specific event. If no event is provided, it returns the total number of listeners
|
|
25
|
+
* @param {string} eventName The event name. Not required
|
|
26
|
+
* @returns {number} The number of listeners
|
|
27
|
+
*/
|
|
17
28
|
listenerCount(eventName) {
|
|
18
29
|
if (!eventName) {
|
|
19
30
|
return this.getAllListeners().length;
|
|
@@ -21,21 +32,42 @@ var Eventified = class {
|
|
|
21
32
|
const listeners = this._eventListeners.get(eventName);
|
|
22
33
|
return listeners ? listeners.length : 0;
|
|
23
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Gets an array of event names
|
|
37
|
+
* @returns {Array<string | symbol>} An array of event names
|
|
38
|
+
*/
|
|
24
39
|
eventNames() {
|
|
25
40
|
return Array.from(this._eventListeners.keys());
|
|
26
41
|
}
|
|
27
|
-
|
|
28
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Gets an array of listeners for a specific event. If no event is provided, it returns all listeners
|
|
44
|
+
* @param {string} [event] (Optional) The event name
|
|
45
|
+
* @returns {EventListener[]} An array of listeners
|
|
46
|
+
*/
|
|
47
|
+
rawListeners(event) {
|
|
48
|
+
if (!event) {
|
|
29
49
|
return this.getAllListeners();
|
|
30
50
|
}
|
|
31
|
-
return this._eventListeners.get(
|
|
51
|
+
return this._eventListeners.get(event) ?? [];
|
|
32
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Prepends a listener to the beginning of the listeners array for the specified event
|
|
55
|
+
* @param {string | symbol} eventName
|
|
56
|
+
* @param {EventListener} listener
|
|
57
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
58
|
+
*/
|
|
33
59
|
prependListener(eventName, listener) {
|
|
34
60
|
const listeners = this._eventListeners.get(eventName) ?? [];
|
|
35
61
|
listeners.unshift(listener);
|
|
36
62
|
this._eventListeners.set(eventName, listeners);
|
|
37
63
|
return this;
|
|
38
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Prepends a one-time listener to the beginning of the listeners array for the specified event
|
|
67
|
+
* @param {string | symbol} eventName
|
|
68
|
+
* @param {EventListener} listener
|
|
69
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
70
|
+
*/
|
|
39
71
|
prependOnceListener(eventName, listener) {
|
|
40
72
|
const onceListener = (...arguments_) => {
|
|
41
73
|
this.off(eventName, onceListener);
|
|
@@ -44,14 +76,29 @@ var Eventified = class {
|
|
|
44
76
|
this.prependListener(eventName, onceListener);
|
|
45
77
|
return this;
|
|
46
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Gets the maximum number of listeners that can be added for a single event
|
|
81
|
+
* @returns {number} The maximum number of listeners
|
|
82
|
+
*/
|
|
47
83
|
maxListeners() {
|
|
48
84
|
return this._maxListeners;
|
|
49
85
|
}
|
|
50
|
-
|
|
86
|
+
/**
|
|
87
|
+
* Adds a listener for a specific event. It is an alias for the on() method
|
|
88
|
+
* @param {string | symbol} event
|
|
89
|
+
* @param {EventListener} listener
|
|
90
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
91
|
+
*/
|
|
51
92
|
addListener(event, listener) {
|
|
52
93
|
this.on(event, listener);
|
|
53
94
|
return this;
|
|
54
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* Adds a listener for a specific event
|
|
98
|
+
* @param {string | symbol} event
|
|
99
|
+
* @param {EventListener} listener
|
|
100
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
101
|
+
*/
|
|
55
102
|
on(event, listener) {
|
|
56
103
|
if (!this._eventListeners.has(event)) {
|
|
57
104
|
this._eventListeners.set(event, []);
|
|
@@ -65,11 +112,22 @@ var Eventified = class {
|
|
|
65
112
|
}
|
|
66
113
|
return this;
|
|
67
114
|
}
|
|
68
|
-
|
|
115
|
+
/**
|
|
116
|
+
* Removes a listener for a specific event. It is an alias for the off() method
|
|
117
|
+
* @param {string | symbol} event
|
|
118
|
+
* @param {EventListener} listener
|
|
119
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
120
|
+
*/
|
|
69
121
|
removeListener(event, listener) {
|
|
70
122
|
this.off(event, listener);
|
|
71
123
|
return this;
|
|
72
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* Removes a listener for a specific event
|
|
127
|
+
* @param {string | symbol} event
|
|
128
|
+
* @param {EventListener} listener
|
|
129
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
130
|
+
*/
|
|
73
131
|
off(event, listener) {
|
|
74
132
|
const listeners = this._eventListeners.get(event) ?? [];
|
|
75
133
|
const index = listeners.indexOf(listener);
|
|
@@ -81,21 +139,36 @@ var Eventified = class {
|
|
|
81
139
|
}
|
|
82
140
|
return this;
|
|
83
141
|
}
|
|
84
|
-
|
|
142
|
+
/**
|
|
143
|
+
* Calls all listeners for a specific event
|
|
144
|
+
* @param {string | symbol} event
|
|
145
|
+
* @param arguments_ The arguments to pass to the listeners
|
|
146
|
+
* @returns {boolean} Returns true if the event had listeners, false otherwise
|
|
147
|
+
*/
|
|
85
148
|
emit(event, ...arguments_) {
|
|
149
|
+
let result = false;
|
|
86
150
|
const listeners = this._eventListeners.get(event);
|
|
87
151
|
if (listeners && listeners.length > 0) {
|
|
88
152
|
for (const listener of listeners) {
|
|
89
153
|
listener(...arguments_);
|
|
154
|
+
result = true;
|
|
90
155
|
}
|
|
91
156
|
}
|
|
92
|
-
return
|
|
157
|
+
return result;
|
|
93
158
|
}
|
|
94
|
-
|
|
159
|
+
/**
|
|
160
|
+
* Gets all listeners for a specific event. If no event is provided, it returns all listeners
|
|
161
|
+
* @param {string} [event] (Optional) The event name
|
|
162
|
+
* @returns {EventListener[]} An array of listeners
|
|
163
|
+
*/
|
|
95
164
|
listeners(event) {
|
|
96
165
|
return this._eventListeners.get(event) ?? [];
|
|
97
166
|
}
|
|
98
|
-
|
|
167
|
+
/**
|
|
168
|
+
* Removes all listeners for a specific event. If no event is provided, it removes all listeners
|
|
169
|
+
* @param {string} [event] (Optional) The event name
|
|
170
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
171
|
+
*/
|
|
99
172
|
removeAllListeners(event) {
|
|
100
173
|
if (event) {
|
|
101
174
|
this._eventListeners.delete(event);
|
|
@@ -104,7 +177,11 @@ var Eventified = class {
|
|
|
104
177
|
}
|
|
105
178
|
return this;
|
|
106
179
|
}
|
|
107
|
-
|
|
180
|
+
/**
|
|
181
|
+
* Sets the maximum number of listeners that can be added for a single event
|
|
182
|
+
* @param {number} n The maximum number of listeners
|
|
183
|
+
* @returns {void}
|
|
184
|
+
*/
|
|
108
185
|
setMaxListeners(n) {
|
|
109
186
|
this._maxListeners = n;
|
|
110
187
|
for (const listeners of this._eventListeners.values()) {
|
|
@@ -113,6 +190,10 @@ var Eventified = class {
|
|
|
113
190
|
}
|
|
114
191
|
}
|
|
115
192
|
}
|
|
193
|
+
/**
|
|
194
|
+
* Gets all listeners
|
|
195
|
+
* @returns {EventListener[]} An array of listeners
|
|
196
|
+
*/
|
|
116
197
|
getAllListeners() {
|
|
117
198
|
let result = new Array();
|
|
118
199
|
for (const listeners of this._eventListeners.values()) {
|
|
@@ -129,7 +210,12 @@ var Hookified = class extends Eventified {
|
|
|
129
210
|
super();
|
|
130
211
|
this._hooks = /* @__PURE__ */ new Map();
|
|
131
212
|
}
|
|
132
|
-
|
|
213
|
+
/**
|
|
214
|
+
* Adds a handler function for a specific event
|
|
215
|
+
* @param {string} event
|
|
216
|
+
* @param {Hook} handler - this can be async or sync
|
|
217
|
+
* @returns {void}
|
|
218
|
+
*/
|
|
133
219
|
onHook(event, handler) {
|
|
134
220
|
const eventHandlers = this._hooks.get(event);
|
|
135
221
|
if (eventHandlers) {
|
|
@@ -138,7 +224,24 @@ var Hookified = class extends Eventified {
|
|
|
138
224
|
this._hooks.set(event, [handler]);
|
|
139
225
|
}
|
|
140
226
|
}
|
|
141
|
-
|
|
227
|
+
/**
|
|
228
|
+
* Adds a handler that only executes once for a specific event
|
|
229
|
+
* @param event
|
|
230
|
+
* @param handler
|
|
231
|
+
*/
|
|
232
|
+
onceHook(event, handler) {
|
|
233
|
+
const hook = async (...arguments_) => {
|
|
234
|
+
this.removeHook(event, hook);
|
|
235
|
+
return handler(...arguments_);
|
|
236
|
+
};
|
|
237
|
+
this.onHook(event, hook);
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Removes a handler function for a specific event
|
|
241
|
+
* @param {string} event
|
|
242
|
+
* @param {Hook} handler
|
|
243
|
+
* @returns {void}
|
|
244
|
+
*/
|
|
142
245
|
removeHook(event, handler) {
|
|
143
246
|
const eventHandlers = this._hooks.get(event);
|
|
144
247
|
if (eventHandlers) {
|
|
@@ -148,7 +251,12 @@ var Hookified = class extends Eventified {
|
|
|
148
251
|
}
|
|
149
252
|
}
|
|
150
253
|
}
|
|
151
|
-
|
|
254
|
+
/**
|
|
255
|
+
* Calls all handlers for a specific event
|
|
256
|
+
* @param {string} event
|
|
257
|
+
* @param {T[]} arguments_
|
|
258
|
+
* @returns {Promise<void>}
|
|
259
|
+
*/
|
|
152
260
|
async hook(event, ...arguments_) {
|
|
153
261
|
const eventHandlers = this._hooks.get(event);
|
|
154
262
|
if (eventHandlers) {
|
|
@@ -161,13 +269,25 @@ var Hookified = class extends Eventified {
|
|
|
161
269
|
}
|
|
162
270
|
}
|
|
163
271
|
}
|
|
164
|
-
|
|
272
|
+
/**
|
|
273
|
+
* Gets all hooks
|
|
274
|
+
* @returns {Map<string, Hook[]>}
|
|
275
|
+
*/
|
|
165
276
|
get hooks() {
|
|
166
277
|
return this._hooks;
|
|
167
278
|
}
|
|
279
|
+
/**
|
|
280
|
+
* Gets all hooks for a specific event
|
|
281
|
+
* @param {string} event
|
|
282
|
+
* @returns {Hook[]}
|
|
283
|
+
*/
|
|
168
284
|
getHooks(event) {
|
|
169
285
|
return this._hooks.get(event);
|
|
170
286
|
}
|
|
287
|
+
/**
|
|
288
|
+
* Removes all hooks
|
|
289
|
+
* @returns {void}
|
|
290
|
+
*/
|
|
171
291
|
clearHooks() {
|
|
172
292
|
this._hooks.clear();
|
|
173
293
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hookified",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.1",
|
|
4
4
|
"description": "Event and Middleware Hooks",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/node/index.cjs",
|
|
@@ -57,12 +57,12 @@
|
|
|
57
57
|
},
|
|
58
58
|
"homepage": "https://github.com/jaredwray/hookified#readme",
|
|
59
59
|
"devDependencies": {
|
|
60
|
-
"@vitest/coverage-v8": "^2.1.
|
|
61
|
-
"docula": "^0.9.
|
|
60
|
+
"@vitest/coverage-v8": "^2.1.5",
|
|
61
|
+
"docula": "^0.9.5",
|
|
62
62
|
"rimraf": "^6.0.1",
|
|
63
|
-
"tsup": "^8.3.
|
|
64
|
-
"typescript": "^5.
|
|
65
|
-
"vitest": "^2.1.
|
|
63
|
+
"tsup": "^8.3.5",
|
|
64
|
+
"typescript": "^5.7.2",
|
|
65
|
+
"vitest": "^2.1.5",
|
|
66
66
|
"xo": "^0.59.3"
|
|
67
67
|
},
|
|
68
68
|
"files": [
|