hookable 5.4.0 → 5.4.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/dist/index.cjs +48 -42
- package/dist/index.d.ts +20 -19
- package/dist/index.mjs +48 -42
- package/package.json +7 -7
package/dist/index.cjs
CHANGED
|
@@ -50,46 +50,6 @@ function callEachWith(callbacks, arg0) {
|
|
|
50
50
|
cb(arg0);
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
|
-
const isBrowser = typeof window !== "undefined";
|
|
54
|
-
function createDebugger(hooks, _options = {}) {
|
|
55
|
-
const options = {
|
|
56
|
-
inspect: isBrowser,
|
|
57
|
-
group: isBrowser,
|
|
58
|
-
filter: () => true,
|
|
59
|
-
..._options
|
|
60
|
-
};
|
|
61
|
-
const _filter = options.filter;
|
|
62
|
-
const filter = typeof _filter === "string" ? (name) => name.startsWith(_filter) : _filter;
|
|
63
|
-
const logPrefix = options.tag ? `[${options.tag}] ` : "";
|
|
64
|
-
const unsubscribeBefore = hooks.beforeEach(({ name }) => {
|
|
65
|
-
if (!filter(name)) {
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
console.time(logPrefix + name);
|
|
69
|
-
});
|
|
70
|
-
const unsubscribeAfter = hooks.afterEach(({ name, args }) => {
|
|
71
|
-
if (!filter(name)) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
if (options.group) {
|
|
75
|
-
console.groupCollapsed(name);
|
|
76
|
-
}
|
|
77
|
-
if (options.inspect) {
|
|
78
|
-
console.timeLog(logPrefix + name, args);
|
|
79
|
-
} else {
|
|
80
|
-
console.timeEnd(logPrefix + name);
|
|
81
|
-
}
|
|
82
|
-
if (options.group) {
|
|
83
|
-
console.groupEnd();
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
return {
|
|
87
|
-
close: () => {
|
|
88
|
-
unsubscribeBefore();
|
|
89
|
-
unsubscribeAfter();
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
53
|
|
|
94
54
|
class Hookable {
|
|
95
55
|
constructor() {
|
|
@@ -198,12 +158,12 @@ class Hookable {
|
|
|
198
158
|
const result = caller(this._hooks[name] || [], args);
|
|
199
159
|
if (result instanceof Promise) {
|
|
200
160
|
return result.finally(() => {
|
|
201
|
-
if (this._after) {
|
|
161
|
+
if (this._after && event) {
|
|
202
162
|
callEachWith(this._after, event);
|
|
203
163
|
}
|
|
204
164
|
});
|
|
205
165
|
}
|
|
206
|
-
if (this._after) {
|
|
166
|
+
if (this._after && event) {
|
|
207
167
|
callEachWith(this._after, event);
|
|
208
168
|
}
|
|
209
169
|
return result;
|
|
@@ -233,6 +193,52 @@ function createHooks() {
|
|
|
233
193
|
return new Hookable();
|
|
234
194
|
}
|
|
235
195
|
|
|
196
|
+
const isBrowser = typeof window !== "undefined";
|
|
197
|
+
function createDebugger(hooks, _options = {}) {
|
|
198
|
+
const options = {
|
|
199
|
+
inspect: isBrowser,
|
|
200
|
+
group: isBrowser,
|
|
201
|
+
filter: () => true,
|
|
202
|
+
..._options
|
|
203
|
+
};
|
|
204
|
+
const _filter = options.filter;
|
|
205
|
+
const filter = typeof _filter === "string" ? (name) => name.startsWith(_filter) : _filter;
|
|
206
|
+
const _tag = options.tag ? `[${options.tag}] ` : "";
|
|
207
|
+
const logPrefix = (event) => _tag + event.name + "".padEnd(event._id, "\0");
|
|
208
|
+
const _idCtr = {};
|
|
209
|
+
const unsubscribeBefore = hooks.beforeEach((event) => {
|
|
210
|
+
if (!filter(event.name)) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
_idCtr[event.name] = _idCtr[event.name] || 0;
|
|
214
|
+
event._id = _idCtr[event.name]++;
|
|
215
|
+
console.time(logPrefix(event));
|
|
216
|
+
});
|
|
217
|
+
const unsubscribeAfter = hooks.afterEach((event) => {
|
|
218
|
+
if (!filter(event.name)) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
if (options.group) {
|
|
222
|
+
console.groupCollapsed(event.name);
|
|
223
|
+
}
|
|
224
|
+
if (options.inspect) {
|
|
225
|
+
console.timeLog(logPrefix(event), event.args);
|
|
226
|
+
} else {
|
|
227
|
+
console.timeEnd(logPrefix(event));
|
|
228
|
+
}
|
|
229
|
+
if (options.group) {
|
|
230
|
+
console.groupEnd();
|
|
231
|
+
}
|
|
232
|
+
_idCtr[event.name]--;
|
|
233
|
+
});
|
|
234
|
+
return {
|
|
235
|
+
close: () => {
|
|
236
|
+
unsubscribeBefore();
|
|
237
|
+
unsubscribeAfter();
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
236
242
|
exports.Hookable = Hookable;
|
|
237
243
|
exports.createDebugger = createDebugger;
|
|
238
244
|
exports.createHooks = createHooks;
|
package/dist/index.d.ts
CHANGED
|
@@ -34,24 +34,6 @@ declare type NestedHooks<T> = (Partial<StripGeneric<T>> | Partial<OnlyGeneric<T>
|
|
|
34
34
|
}> & Partial<{
|
|
35
35
|
[key in BareHooks<StripGeneric<T>>]: T[key];
|
|
36
36
|
}>;
|
|
37
|
-
interface CreateDebuggerOptions {
|
|
38
|
-
/** An optional tag to prefix console logs with */
|
|
39
|
-
tag?: string;
|
|
40
|
-
/**
|
|
41
|
-
* Show hook params to the console output
|
|
42
|
-
*
|
|
43
|
-
* Enabled for browsers by default
|
|
44
|
-
*/
|
|
45
|
-
inspect?: boolean;
|
|
46
|
-
/**
|
|
47
|
-
* Use group/groupEnd wrapper around logs happening during a specific hook
|
|
48
|
-
*
|
|
49
|
-
* Enabled for browsers by default
|
|
50
|
-
*/
|
|
51
|
-
group?: boolean;
|
|
52
|
-
/** Filter which hooks to enable debugger for. Can be a string prefix or fn. */
|
|
53
|
-
filter?: string | ((event: string) => boolean);
|
|
54
|
-
}
|
|
55
37
|
|
|
56
38
|
declare type InferCallback<HT, HN extends keyof HT> = HT[HN] extends HookCallback ? HT[HN] : never;
|
|
57
39
|
declare type InferSpyEvent<HT extends Record<string, any>> = {
|
|
@@ -90,8 +72,27 @@ declare function mergeHooks<T>(...hooks: NestedHooks<T>[]): T;
|
|
|
90
72
|
declare function serial<T>(tasks: T[], fn: (task: T) => Promise<any> | any): Promise<any>;
|
|
91
73
|
declare function serialCaller(hooks: HookCallback[], args?: any[]): Promise<any>;
|
|
92
74
|
declare function parallelCaller(hooks: HookCallback[], args?: any[]): Promise<any[]>;
|
|
75
|
+
|
|
76
|
+
interface CreateDebuggerOptions {
|
|
77
|
+
/** An optional tag to prefix console logs with */
|
|
78
|
+
tag?: string;
|
|
79
|
+
/**
|
|
80
|
+
* Show hook params to the console output
|
|
81
|
+
*
|
|
82
|
+
* Enabled for browsers by default
|
|
83
|
+
*/
|
|
84
|
+
inspect?: boolean;
|
|
85
|
+
/**
|
|
86
|
+
* Use group/groupEnd wrapper around logs happening during a specific hook
|
|
87
|
+
*
|
|
88
|
+
* Enabled for browsers by default
|
|
89
|
+
*/
|
|
90
|
+
group?: boolean;
|
|
91
|
+
/** Filter which hooks to enable debugger for. Can be a string prefix or fn. */
|
|
92
|
+
filter?: string | ((event: string) => boolean);
|
|
93
|
+
}
|
|
93
94
|
/** Start debugging hook names and timing in console */
|
|
94
|
-
declare function createDebugger(hooks: Hookable
|
|
95
|
+
declare function createDebugger(hooks: Hookable<any>, _options?: CreateDebuggerOptions): {
|
|
95
96
|
/** Stop debugging and remove listeners */
|
|
96
97
|
close: () => void;
|
|
97
98
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -46,46 +46,6 @@ function callEachWith(callbacks, arg0) {
|
|
|
46
46
|
cb(arg0);
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
|
-
const isBrowser = typeof window !== "undefined";
|
|
50
|
-
function createDebugger(hooks, _options = {}) {
|
|
51
|
-
const options = {
|
|
52
|
-
inspect: isBrowser,
|
|
53
|
-
group: isBrowser,
|
|
54
|
-
filter: () => true,
|
|
55
|
-
..._options
|
|
56
|
-
};
|
|
57
|
-
const _filter = options.filter;
|
|
58
|
-
const filter = typeof _filter === "string" ? (name) => name.startsWith(_filter) : _filter;
|
|
59
|
-
const logPrefix = options.tag ? `[${options.tag}] ` : "";
|
|
60
|
-
const unsubscribeBefore = hooks.beforeEach(({ name }) => {
|
|
61
|
-
if (!filter(name)) {
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
console.time(logPrefix + name);
|
|
65
|
-
});
|
|
66
|
-
const unsubscribeAfter = hooks.afterEach(({ name, args }) => {
|
|
67
|
-
if (!filter(name)) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
if (options.group) {
|
|
71
|
-
console.groupCollapsed(name);
|
|
72
|
-
}
|
|
73
|
-
if (options.inspect) {
|
|
74
|
-
console.timeLog(logPrefix + name, args);
|
|
75
|
-
} else {
|
|
76
|
-
console.timeEnd(logPrefix + name);
|
|
77
|
-
}
|
|
78
|
-
if (options.group) {
|
|
79
|
-
console.groupEnd();
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
return {
|
|
83
|
-
close: () => {
|
|
84
|
-
unsubscribeBefore();
|
|
85
|
-
unsubscribeAfter();
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
49
|
|
|
90
50
|
class Hookable {
|
|
91
51
|
constructor() {
|
|
@@ -194,12 +154,12 @@ class Hookable {
|
|
|
194
154
|
const result = caller(this._hooks[name] || [], args);
|
|
195
155
|
if (result instanceof Promise) {
|
|
196
156
|
return result.finally(() => {
|
|
197
|
-
if (this._after) {
|
|
157
|
+
if (this._after && event) {
|
|
198
158
|
callEachWith(this._after, event);
|
|
199
159
|
}
|
|
200
160
|
});
|
|
201
161
|
}
|
|
202
|
-
if (this._after) {
|
|
162
|
+
if (this._after && event) {
|
|
203
163
|
callEachWith(this._after, event);
|
|
204
164
|
}
|
|
205
165
|
return result;
|
|
@@ -229,4 +189,50 @@ function createHooks() {
|
|
|
229
189
|
return new Hookable();
|
|
230
190
|
}
|
|
231
191
|
|
|
192
|
+
const isBrowser = typeof window !== "undefined";
|
|
193
|
+
function createDebugger(hooks, _options = {}) {
|
|
194
|
+
const options = {
|
|
195
|
+
inspect: isBrowser,
|
|
196
|
+
group: isBrowser,
|
|
197
|
+
filter: () => true,
|
|
198
|
+
..._options
|
|
199
|
+
};
|
|
200
|
+
const _filter = options.filter;
|
|
201
|
+
const filter = typeof _filter === "string" ? (name) => name.startsWith(_filter) : _filter;
|
|
202
|
+
const _tag = options.tag ? `[${options.tag}] ` : "";
|
|
203
|
+
const logPrefix = (event) => _tag + event.name + "".padEnd(event._id, "\0");
|
|
204
|
+
const _idCtr = {};
|
|
205
|
+
const unsubscribeBefore = hooks.beforeEach((event) => {
|
|
206
|
+
if (!filter(event.name)) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
_idCtr[event.name] = _idCtr[event.name] || 0;
|
|
210
|
+
event._id = _idCtr[event.name]++;
|
|
211
|
+
console.time(logPrefix(event));
|
|
212
|
+
});
|
|
213
|
+
const unsubscribeAfter = hooks.afterEach((event) => {
|
|
214
|
+
if (!filter(event.name)) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (options.group) {
|
|
218
|
+
console.groupCollapsed(event.name);
|
|
219
|
+
}
|
|
220
|
+
if (options.inspect) {
|
|
221
|
+
console.timeLog(logPrefix(event), event.args);
|
|
222
|
+
} else {
|
|
223
|
+
console.timeEnd(logPrefix(event));
|
|
224
|
+
}
|
|
225
|
+
if (options.group) {
|
|
226
|
+
console.groupEnd();
|
|
227
|
+
}
|
|
228
|
+
_idCtr[event.name]--;
|
|
229
|
+
});
|
|
230
|
+
return {
|
|
231
|
+
close: () => {
|
|
232
|
+
unsubscribeBefore();
|
|
233
|
+
unsubscribeAfter();
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
232
238
|
export { Hookable, createDebugger, createHooks, flatHooks, mergeHooks, parallelCaller, serial, serialCaller };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hookable",
|
|
3
|
-
"version": "5.4.
|
|
3
|
+
"version": "5.4.1",
|
|
4
4
|
"description": "Awaitable hook system",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"hook",
|
|
@@ -22,14 +22,14 @@
|
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@nuxtjs/eslint-config-typescript": "
|
|
26
|
-
"@vitest/coverage-c8": "^0.24.
|
|
27
|
-
"eslint": "
|
|
25
|
+
"@nuxtjs/eslint-config-typescript": "^11.0.0",
|
|
26
|
+
"@vitest/coverage-c8": "^0.24.3",
|
|
27
|
+
"eslint": "^8.25.0",
|
|
28
28
|
"expect-type": "^0.14.2",
|
|
29
|
-
"standard-version": "
|
|
29
|
+
"standard-version": "^9.5.0",
|
|
30
30
|
"typescript": "latest",
|
|
31
|
-
"unbuild": "
|
|
32
|
-
"vitest": "
|
|
31
|
+
"unbuild": "^0.8.2",
|
|
32
|
+
"vitest": "^0.24.3"
|
|
33
33
|
},
|
|
34
34
|
"packageManager": "pnpm@7.13.4",
|
|
35
35
|
"scripts": {
|