hookable 5.5.1 → 5.5.3
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 +6 -2
- package/dist/index.cjs +20 -13
- package/dist/index.d.ts +5 -5
- package/dist/index.mjs +20 -13
- package/package.json +10 -9
package/README.md
CHANGED
|
@@ -194,12 +194,16 @@ hookable.removeHooks({
|
|
|
194
194
|
})
|
|
195
195
|
```
|
|
196
196
|
|
|
197
|
+
### `removeAllHooks`
|
|
198
|
+
|
|
199
|
+
Remove all hook handlers.
|
|
200
|
+
|
|
197
201
|
### `beforeEach (syncCallback)`
|
|
198
202
|
|
|
199
203
|
Registers a (sync) callback to be called before each hook is being called.
|
|
200
204
|
|
|
201
205
|
```js
|
|
202
|
-
hookable.beforeEach((event) => { console.log(`${event.name} hook is being called with ${event.args}`)})
|
|
206
|
+
hookable.beforeEach((event) => { console.log(`${event.name} hook is being called with ${event.args}`)}`)
|
|
203
207
|
hookable.hook('test', () => { console.log('running test hook') })
|
|
204
208
|
|
|
205
209
|
// test hook is being called with []
|
|
@@ -212,7 +216,7 @@ await hookable.callHook('test')
|
|
|
212
216
|
Registers a (sync) callback to be called after each hook is being called.
|
|
213
217
|
|
|
214
218
|
```js
|
|
215
|
-
hookable.afterEach((event) => { console.log(`${event.name} hook called with ${event.args}`)})
|
|
219
|
+
hookable.afterEach((event) => { console.log(`${event.name} hook called with ${event.args}`)}`)
|
|
216
220
|
hookable.hook('test', () => { console.log('running test hook') })
|
|
217
221
|
|
|
218
222
|
// running test hook
|
package/dist/index.cjs
CHANGED
|
@@ -58,15 +58,15 @@ function parallelTaskCaller(hooks, args) {
|
|
|
58
58
|
}
|
|
59
59
|
function serialCaller(hooks, arguments_) {
|
|
60
60
|
return hooks.reduce(
|
|
61
|
-
(promise, hookFunction) => promise.then(() => hookFunction(...arguments_)),
|
|
61
|
+
(promise, hookFunction) => promise.then(() => hookFunction(...arguments_ || [])),
|
|
62
62
|
Promise.resolve()
|
|
63
63
|
);
|
|
64
64
|
}
|
|
65
65
|
function parallelCaller(hooks, args) {
|
|
66
|
-
return Promise.all(hooks.map((hook) => hook(...args)));
|
|
66
|
+
return Promise.all(hooks.map((hook) => hook(...args || [])));
|
|
67
67
|
}
|
|
68
68
|
function callEachWith(callbacks, arg0) {
|
|
69
|
-
for (const callback of callbacks) {
|
|
69
|
+
for (const callback of [...callbacks]) {
|
|
70
70
|
callback(arg0);
|
|
71
71
|
}
|
|
72
72
|
}
|
|
@@ -151,7 +151,7 @@ class Hookable {
|
|
|
151
151
|
deprecateHook(name, deprecated) {
|
|
152
152
|
this._deprecatedHooks[name] = typeof deprecated === "string" ? { to: deprecated } : deprecated;
|
|
153
153
|
const _hooks = this._hooks[name] || [];
|
|
154
|
-
this._hooks[name]
|
|
154
|
+
delete this._hooks[name];
|
|
155
155
|
for (const hook of _hooks) {
|
|
156
156
|
this.hook(name, hook);
|
|
157
157
|
}
|
|
@@ -197,7 +197,10 @@ class Hookable {
|
|
|
197
197
|
if (this._before) {
|
|
198
198
|
callEachWith(this._before, event);
|
|
199
199
|
}
|
|
200
|
-
const result = caller(
|
|
200
|
+
const result = caller(
|
|
201
|
+
name in this._hooks ? [...this._hooks[name]] : [],
|
|
202
|
+
arguments_
|
|
203
|
+
);
|
|
201
204
|
if (result instanceof Promise) {
|
|
202
205
|
return result.finally(() => {
|
|
203
206
|
if (this._after && event) {
|
|
@@ -214,9 +217,11 @@ class Hookable {
|
|
|
214
217
|
this._before = this._before || [];
|
|
215
218
|
this._before.push(function_);
|
|
216
219
|
return () => {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
+
if (this._before !== void 0) {
|
|
221
|
+
const index = this._before.indexOf(function_);
|
|
222
|
+
if (index !== -1) {
|
|
223
|
+
this._before.splice(index, 1);
|
|
224
|
+
}
|
|
220
225
|
}
|
|
221
226
|
};
|
|
222
227
|
}
|
|
@@ -224,9 +229,11 @@ class Hookable {
|
|
|
224
229
|
this._after = this._after || [];
|
|
225
230
|
this._after.push(function_);
|
|
226
231
|
return () => {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
232
|
+
if (this._after !== void 0) {
|
|
233
|
+
const index = this._after.indexOf(function_);
|
|
234
|
+
if (index !== -1) {
|
|
235
|
+
this._after.splice(index, 1);
|
|
236
|
+
}
|
|
230
237
|
}
|
|
231
238
|
};
|
|
232
239
|
}
|
|
@@ -249,7 +256,7 @@ function createDebugger(hooks, _options = {}) {
|
|
|
249
256
|
const logPrefix = (event) => _tag + event.name + "".padEnd(event._id, "\0");
|
|
250
257
|
const _idCtr = {};
|
|
251
258
|
const unsubscribeBefore = hooks.beforeEach((event) => {
|
|
252
|
-
if (!filter(event.name)) {
|
|
259
|
+
if (filter !== void 0 && !filter(event.name)) {
|
|
253
260
|
return;
|
|
254
261
|
}
|
|
255
262
|
_idCtr[event.name] = _idCtr[event.name] || 0;
|
|
@@ -257,7 +264,7 @@ function createDebugger(hooks, _options = {}) {
|
|
|
257
264
|
console.time(logPrefix(event));
|
|
258
265
|
});
|
|
259
266
|
const unsubscribeAfter = hooks.afterEach((event) => {
|
|
260
|
-
if (!filter(event.name)) {
|
|
267
|
+
if (filter !== void 0 && !filter(event.name)) {
|
|
261
268
|
return;
|
|
262
269
|
}
|
|
263
270
|
if (options.group) {
|
package/dist/index.d.ts
CHANGED
|
@@ -43,12 +43,12 @@ type InferSpyEvent<HT extends Record<string, any>> = {
|
|
|
43
43
|
context: Record<string, any>;
|
|
44
44
|
};
|
|
45
45
|
}[keyof HT];
|
|
46
|
-
declare class Hookable<HooksT = Record<string, HookCallback>, HookNameT extends HookKeys<HooksT> = HookKeys<HooksT>> {
|
|
46
|
+
declare class Hookable<HooksT extends Record<string, any> = Record<string, HookCallback>, HookNameT extends HookKeys<HooksT> = HookKeys<HooksT>> {
|
|
47
47
|
private _hooks;
|
|
48
|
-
private _before
|
|
49
|
-
private _after
|
|
48
|
+
private _before?;
|
|
49
|
+
private _after?;
|
|
50
50
|
private _deprecatedHooks;
|
|
51
|
-
private _deprecatedMessages
|
|
51
|
+
private _deprecatedMessages?;
|
|
52
52
|
constructor();
|
|
53
53
|
hook<NameT extends HookNameT>(name: NameT, function_: InferCallback<HooksT, NameT>, options?: {
|
|
54
54
|
allowDeprecated?: boolean;
|
|
@@ -66,7 +66,7 @@ declare class Hookable<HooksT = Record<string, HookCallback>, HookNameT extends
|
|
|
66
66
|
beforeEach(function_: (event: InferSpyEvent<HooksT>) => void): () => void;
|
|
67
67
|
afterEach(function_: (event: InferSpyEvent<HooksT>) => void): () => void;
|
|
68
68
|
}
|
|
69
|
-
declare function createHooks<T
|
|
69
|
+
declare function createHooks<T extends Record<string, any>>(): Hookable<T>;
|
|
70
70
|
|
|
71
71
|
declare function flatHooks<T>(configHooks: NestedHooks<T>, hooks?: T, parentName?: string): T;
|
|
72
72
|
declare function mergeHooks<T>(...hooks: NestedHooks<T>[]): T;
|
package/dist/index.mjs
CHANGED
|
@@ -56,15 +56,15 @@ function parallelTaskCaller(hooks, args) {
|
|
|
56
56
|
}
|
|
57
57
|
function serialCaller(hooks, arguments_) {
|
|
58
58
|
return hooks.reduce(
|
|
59
|
-
(promise, hookFunction) => promise.then(() => hookFunction(...arguments_)),
|
|
59
|
+
(promise, hookFunction) => promise.then(() => hookFunction(...arguments_ || [])),
|
|
60
60
|
Promise.resolve()
|
|
61
61
|
);
|
|
62
62
|
}
|
|
63
63
|
function parallelCaller(hooks, args) {
|
|
64
|
-
return Promise.all(hooks.map((hook) => hook(...args)));
|
|
64
|
+
return Promise.all(hooks.map((hook) => hook(...args || [])));
|
|
65
65
|
}
|
|
66
66
|
function callEachWith(callbacks, arg0) {
|
|
67
|
-
for (const callback of callbacks) {
|
|
67
|
+
for (const callback of [...callbacks]) {
|
|
68
68
|
callback(arg0);
|
|
69
69
|
}
|
|
70
70
|
}
|
|
@@ -149,7 +149,7 @@ class Hookable {
|
|
|
149
149
|
deprecateHook(name, deprecated) {
|
|
150
150
|
this._deprecatedHooks[name] = typeof deprecated === "string" ? { to: deprecated } : deprecated;
|
|
151
151
|
const _hooks = this._hooks[name] || [];
|
|
152
|
-
this._hooks[name]
|
|
152
|
+
delete this._hooks[name];
|
|
153
153
|
for (const hook of _hooks) {
|
|
154
154
|
this.hook(name, hook);
|
|
155
155
|
}
|
|
@@ -195,7 +195,10 @@ class Hookable {
|
|
|
195
195
|
if (this._before) {
|
|
196
196
|
callEachWith(this._before, event);
|
|
197
197
|
}
|
|
198
|
-
const result = caller(
|
|
198
|
+
const result = caller(
|
|
199
|
+
name in this._hooks ? [...this._hooks[name]] : [],
|
|
200
|
+
arguments_
|
|
201
|
+
);
|
|
199
202
|
if (result instanceof Promise) {
|
|
200
203
|
return result.finally(() => {
|
|
201
204
|
if (this._after && event) {
|
|
@@ -212,9 +215,11 @@ class Hookable {
|
|
|
212
215
|
this._before = this._before || [];
|
|
213
216
|
this._before.push(function_);
|
|
214
217
|
return () => {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
+
if (this._before !== void 0) {
|
|
219
|
+
const index = this._before.indexOf(function_);
|
|
220
|
+
if (index !== -1) {
|
|
221
|
+
this._before.splice(index, 1);
|
|
222
|
+
}
|
|
218
223
|
}
|
|
219
224
|
};
|
|
220
225
|
}
|
|
@@ -222,9 +227,11 @@ class Hookable {
|
|
|
222
227
|
this._after = this._after || [];
|
|
223
228
|
this._after.push(function_);
|
|
224
229
|
return () => {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
230
|
+
if (this._after !== void 0) {
|
|
231
|
+
const index = this._after.indexOf(function_);
|
|
232
|
+
if (index !== -1) {
|
|
233
|
+
this._after.splice(index, 1);
|
|
234
|
+
}
|
|
228
235
|
}
|
|
229
236
|
};
|
|
230
237
|
}
|
|
@@ -247,7 +254,7 @@ function createDebugger(hooks, _options = {}) {
|
|
|
247
254
|
const logPrefix = (event) => _tag + event.name + "".padEnd(event._id, "\0");
|
|
248
255
|
const _idCtr = {};
|
|
249
256
|
const unsubscribeBefore = hooks.beforeEach((event) => {
|
|
250
|
-
if (!filter(event.name)) {
|
|
257
|
+
if (filter !== void 0 && !filter(event.name)) {
|
|
251
258
|
return;
|
|
252
259
|
}
|
|
253
260
|
_idCtr[event.name] = _idCtr[event.name] || 0;
|
|
@@ -255,7 +262,7 @@ function createDebugger(hooks, _options = {}) {
|
|
|
255
262
|
console.time(logPrefix(event));
|
|
256
263
|
});
|
|
257
264
|
const unsubscribeAfter = hooks.afterEach((event) => {
|
|
258
|
-
if (!filter(event.name)) {
|
|
265
|
+
if (filter !== void 0 && !filter(event.name)) {
|
|
259
266
|
return;
|
|
260
267
|
}
|
|
261
268
|
if (options.group) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hookable",
|
|
3
|
-
"version": "5.5.
|
|
3
|
+
"version": "5.5.3",
|
|
4
4
|
"description": "Awaitable hook system",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"hook",
|
|
@@ -23,18 +23,19 @@
|
|
|
23
23
|
"dist"
|
|
24
24
|
],
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@types/node": "^18.15.
|
|
27
|
-
"@vitest/coverage-c8": "^0.29.
|
|
28
|
-
"changelogen": "^0.5.
|
|
29
|
-
"eslint": "^8.
|
|
26
|
+
"@types/node": "^18.15.11",
|
|
27
|
+
"@vitest/coverage-c8": "^0.29.8",
|
|
28
|
+
"changelogen": "^0.5.2",
|
|
29
|
+
"eslint": "^8.37.0",
|
|
30
30
|
"eslint-config-unjs": "^0.1.0",
|
|
31
31
|
"expect-type": "^0.15.0",
|
|
32
|
-
"prettier": "^2.8.
|
|
33
|
-
"typescript": "^
|
|
32
|
+
"prettier": "^2.8.7",
|
|
33
|
+
"typescript": "^5.0.2",
|
|
34
34
|
"unbuild": "^1.1.2",
|
|
35
|
-
"
|
|
35
|
+
"vite": "^4.2.1",
|
|
36
|
+
"vitest": "^0.29.8"
|
|
36
37
|
},
|
|
37
|
-
"packageManager": "pnpm@
|
|
38
|
+
"packageManager": "pnpm@8.0.0",
|
|
38
39
|
"scripts": {
|
|
39
40
|
"build": "unbuild",
|
|
40
41
|
"dev": "vitest",
|