hookable 5.4.2 → 5.5.0
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 +46 -9
- package/dist/index.d.ts +21 -18
- package/dist/index.mjs +46 -9
- package/package.json +14 -11
package/dist/index.cjs
CHANGED
|
@@ -35,17 +35,37 @@ function mergeHooks(...hooks) {
|
|
|
35
35
|
return finalHooks;
|
|
36
36
|
}
|
|
37
37
|
function serial(tasks, function_) {
|
|
38
|
-
return tasks.reduce(
|
|
38
|
+
return tasks.reduce(
|
|
39
|
+
(promise, task) => promise.then(() => function_(task)),
|
|
40
|
+
Promise.resolve()
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
const defaultTask = { run: (function_) => function_() };
|
|
44
|
+
const _createTask = () => defaultTask;
|
|
45
|
+
const createTask = typeof console.createTask !== "undefined" ? console.createTask : _createTask;
|
|
46
|
+
function serialTaskCaller(hooks, name, ...args) {
|
|
47
|
+
const task = createTask(name);
|
|
48
|
+
return hooks.reduce(
|
|
49
|
+
(promise, hookFunction) => promise.then(() => task.run(() => hookFunction(...args))),
|
|
50
|
+
Promise.resolve()
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
function parallelTaskCaller(hooks, name, ...args) {
|
|
54
|
+
const task = createTask(name);
|
|
55
|
+
return Promise.all(hooks.map((hook) => task.run(() => hook(...args))));
|
|
39
56
|
}
|
|
40
57
|
function serialCaller(hooks, arguments_) {
|
|
41
|
-
return hooks.reduce(
|
|
58
|
+
return hooks.reduce(
|
|
59
|
+
(promise, hookFunction) => promise.then(() => hookFunction(...arguments_)),
|
|
60
|
+
Promise.resolve()
|
|
61
|
+
);
|
|
42
62
|
}
|
|
43
|
-
function parallelCaller(hooks,
|
|
44
|
-
return Promise.all(hooks.map((hook) => hook
|
|
63
|
+
function parallelCaller(hooks, args) {
|
|
64
|
+
return Promise.all(hooks.map((hook) => hook(...args)));
|
|
45
65
|
}
|
|
46
|
-
function callEachWith(callbacks,
|
|
66
|
+
function callEachWith(callbacks, arg0) {
|
|
47
67
|
for (const callback of callbacks) {
|
|
48
|
-
callback(
|
|
68
|
+
callback(arg0);
|
|
49
69
|
}
|
|
50
70
|
}
|
|
51
71
|
|
|
@@ -84,6 +104,15 @@ class Hookable {
|
|
|
84
104
|
this._deprecatedMessages.add(message);
|
|
85
105
|
}
|
|
86
106
|
}
|
|
107
|
+
if (!function_.name) {
|
|
108
|
+
try {
|
|
109
|
+
Object.defineProperty(function_, "name", {
|
|
110
|
+
get: () => "_" + name.replace(/\W+/g, "_") + "_hook_cb",
|
|
111
|
+
configurable: true
|
|
112
|
+
});
|
|
113
|
+
} catch {
|
|
114
|
+
}
|
|
115
|
+
}
|
|
87
116
|
this._hooks[name] = this._hooks[name] || [];
|
|
88
117
|
this._hooks[name].push(function_);
|
|
89
118
|
return () => {
|
|
@@ -133,7 +162,9 @@ class Hookable {
|
|
|
133
162
|
}
|
|
134
163
|
addHooks(configHooks) {
|
|
135
164
|
const hooks = flatHooks(configHooks);
|
|
136
|
-
const removeFns = Object.keys(hooks).map(
|
|
165
|
+
const removeFns = Object.keys(hooks).map(
|
|
166
|
+
(key) => this.hook(key, hooks[key])
|
|
167
|
+
);
|
|
137
168
|
return () => {
|
|
138
169
|
for (const unreg of removeFns.splice(0, removeFns.length)) {
|
|
139
170
|
unreg();
|
|
@@ -146,11 +177,16 @@ class Hookable {
|
|
|
146
177
|
this.removeHook(key, hooks[key]);
|
|
147
178
|
}
|
|
148
179
|
}
|
|
180
|
+
removeAllHooks() {
|
|
181
|
+
for (const key in this._hooks) {
|
|
182
|
+
delete this._hooks[key];
|
|
183
|
+
}
|
|
184
|
+
}
|
|
149
185
|
callHook(name, ...arguments_) {
|
|
150
|
-
return this.callHookWith(
|
|
186
|
+
return this.callHookWith(serialTaskCaller, name, ...arguments_);
|
|
151
187
|
}
|
|
152
188
|
callHookParallel(name, ...arguments_) {
|
|
153
|
-
return this.callHookWith(
|
|
189
|
+
return this.callHookWith(parallelTaskCaller, name, ...arguments_);
|
|
154
190
|
}
|
|
155
191
|
callHookWith(caller, name, ...arguments_) {
|
|
156
192
|
const event = this._before || this._after ? { name, args: arguments_, context: {} } : void 0;
|
|
@@ -234,6 +270,7 @@ function createDebugger(hooks, _options = {}) {
|
|
|
234
270
|
_idCtr[event.name]--;
|
|
235
271
|
});
|
|
236
272
|
return {
|
|
273
|
+
/** Stop debugging and remove listeners */
|
|
237
274
|
close: () => {
|
|
238
275
|
unsubscribeBefore();
|
|
239
276
|
unsubscribeAfter();
|
package/dist/index.d.ts
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
|
|
1
|
+
type HookCallback = (...arguments_: any) => Promise<void> | void;
|
|
2
2
|
interface Hooks {
|
|
3
3
|
[key: string]: HookCallback;
|
|
4
4
|
}
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
type HookKeys<T> = keyof T & string;
|
|
6
|
+
type DeprecatedHook<T> = {
|
|
7
7
|
message?: string;
|
|
8
8
|
to: HookKeys<T>;
|
|
9
9
|
};
|
|
10
|
-
|
|
10
|
+
type DeprecatedHooks<T> = {
|
|
11
11
|
[name in HookKeys<T>]: DeprecatedHook<T>;
|
|
12
12
|
};
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
type ValueOf<C> = C extends Record<any, any> ? C[keyof C] : never;
|
|
14
|
+
type Strings<T> = Exclude<keyof T, number | symbol>;
|
|
15
|
+
type KnownKeys<T> = keyof {
|
|
16
16
|
[K in keyof T as string extends K ? never : number extends K ? never : K]: never;
|
|
17
17
|
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
type StripGeneric<T> = Pick<T, KnownKeys<T> extends keyof T ? KnownKeys<T> : never>;
|
|
19
|
+
type OnlyGeneric<T> = Omit<T, KnownKeys<T> extends keyof T ? KnownKeys<T> : never>;
|
|
20
|
+
type Namespaces<T> = ValueOf<{
|
|
21
21
|
[key in Strings<T>]: key extends `${infer Namespace}:${string}` ? Namespace : never;
|
|
22
22
|
}>;
|
|
23
|
-
|
|
23
|
+
type BareHooks<T> = ValueOf<{
|
|
24
24
|
[key in Strings<T>]: key extends `${string}:${string}` ? never : key;
|
|
25
25
|
}>;
|
|
26
|
-
|
|
26
|
+
type HooksInNamespace<T, Namespace extends string> = ValueOf<{
|
|
27
27
|
[key in Strings<T>]: key extends `${Namespace}:${infer HookName}` ? HookName : never;
|
|
28
28
|
}>;
|
|
29
|
-
|
|
29
|
+
type WithoutNamespace<T, Namespace extends string> = {
|
|
30
30
|
[key in HooksInNamespace<T, Namespace>]: `${Namespace}:${key}` extends keyof T ? T[`${Namespace}:${key}`] : never;
|
|
31
31
|
};
|
|
32
|
-
|
|
32
|
+
type NestedHooks<T> = (Partial<StripGeneric<T>> | Partial<OnlyGeneric<T>>) & Partial<{
|
|
33
33
|
[key in Namespaces<StripGeneric<T>>]: NestedHooks<WithoutNamespace<T, key>>;
|
|
34
34
|
}> & Partial<{
|
|
35
35
|
[key in BareHooks<StripGeneric<T>>]: T[key];
|
|
36
36
|
}>;
|
|
37
37
|
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
type InferCallback<HT, HN extends keyof HT> = HT[HN] extends HookCallback ? HT[HN] : never;
|
|
39
|
+
type InferSpyEvent<HT extends Record<string, any>> = {
|
|
40
40
|
[key in keyof HT]: {
|
|
41
41
|
name: key;
|
|
42
42
|
args: Parameters<HT[key]>;
|
|
@@ -59,6 +59,7 @@ declare class Hookable<HooksT = Record<string, HookCallback>, HookNameT extends
|
|
|
59
59
|
deprecateHooks(deprecatedHooks: Partial<Record<HookNameT, DeprecatedHook<HooksT>>>): void;
|
|
60
60
|
addHooks(configHooks: NestedHooks<HooksT>): () => void;
|
|
61
61
|
removeHooks(configHooks: NestedHooks<HooksT>): void;
|
|
62
|
+
removeAllHooks(): void;
|
|
62
63
|
callHook<NameT extends HookNameT>(name: NameT, ...arguments_: Parameters<InferCallback<HooksT, NameT>>): Promise<any>;
|
|
63
64
|
callHookParallel<NameT extends HookNameT>(name: NameT, ...arguments_: Parameters<InferCallback<HooksT, NameT>>): Promise<any[]>;
|
|
64
65
|
callHookWith<NameT extends HookNameT, CallFunction extends (hooks: HookCallback[], arguments_: Parameters<InferCallback<HooksT, NameT>>) => any>(caller: CallFunction, name: NameT, ...arguments_: Parameters<InferCallback<HooksT, NameT>>): ReturnType<CallFunction>;
|
|
@@ -70,8 +71,10 @@ declare function createHooks<T>(): Hookable<T>;
|
|
|
70
71
|
declare function flatHooks<T>(configHooks: NestedHooks<T>, hooks?: T, parentName?: string): T;
|
|
71
72
|
declare function mergeHooks<T>(...hooks: NestedHooks<T>[]): T;
|
|
72
73
|
declare function serial<T>(tasks: T[], function_: (task: T) => Promise<any> | any): Promise<any>;
|
|
73
|
-
|
|
74
|
-
declare function
|
|
74
|
+
/** @deprecated */
|
|
75
|
+
declare function serialCaller(hooks: HookCallback[], arguments_?: any[]): Promise<void>;
|
|
76
|
+
/** @deprecated */
|
|
77
|
+
declare function parallelCaller(hooks: HookCallback[], args?: any[]): Promise<void[]>;
|
|
75
78
|
|
|
76
79
|
interface CreateDebuggerOptions {
|
|
77
80
|
/** An optional tag to prefix console logs with */
|
package/dist/index.mjs
CHANGED
|
@@ -33,17 +33,37 @@ function mergeHooks(...hooks) {
|
|
|
33
33
|
return finalHooks;
|
|
34
34
|
}
|
|
35
35
|
function serial(tasks, function_) {
|
|
36
|
-
return tasks.reduce(
|
|
36
|
+
return tasks.reduce(
|
|
37
|
+
(promise, task) => promise.then(() => function_(task)),
|
|
38
|
+
Promise.resolve()
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
const defaultTask = { run: (function_) => function_() };
|
|
42
|
+
const _createTask = () => defaultTask;
|
|
43
|
+
const createTask = typeof console.createTask !== "undefined" ? console.createTask : _createTask;
|
|
44
|
+
function serialTaskCaller(hooks, name, ...args) {
|
|
45
|
+
const task = createTask(name);
|
|
46
|
+
return hooks.reduce(
|
|
47
|
+
(promise, hookFunction) => promise.then(() => task.run(() => hookFunction(...args))),
|
|
48
|
+
Promise.resolve()
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
function parallelTaskCaller(hooks, name, ...args) {
|
|
52
|
+
const task = createTask(name);
|
|
53
|
+
return Promise.all(hooks.map((hook) => task.run(() => hook(...args))));
|
|
37
54
|
}
|
|
38
55
|
function serialCaller(hooks, arguments_) {
|
|
39
|
-
return hooks.reduce(
|
|
56
|
+
return hooks.reduce(
|
|
57
|
+
(promise, hookFunction) => promise.then(() => hookFunction(...arguments_)),
|
|
58
|
+
Promise.resolve()
|
|
59
|
+
);
|
|
40
60
|
}
|
|
41
|
-
function parallelCaller(hooks,
|
|
42
|
-
return Promise.all(hooks.map((hook) => hook
|
|
61
|
+
function parallelCaller(hooks, args) {
|
|
62
|
+
return Promise.all(hooks.map((hook) => hook(...args)));
|
|
43
63
|
}
|
|
44
|
-
function callEachWith(callbacks,
|
|
64
|
+
function callEachWith(callbacks, arg0) {
|
|
45
65
|
for (const callback of callbacks) {
|
|
46
|
-
callback(
|
|
66
|
+
callback(arg0);
|
|
47
67
|
}
|
|
48
68
|
}
|
|
49
69
|
|
|
@@ -82,6 +102,15 @@ class Hookable {
|
|
|
82
102
|
this._deprecatedMessages.add(message);
|
|
83
103
|
}
|
|
84
104
|
}
|
|
105
|
+
if (!function_.name) {
|
|
106
|
+
try {
|
|
107
|
+
Object.defineProperty(function_, "name", {
|
|
108
|
+
get: () => "_" + name.replace(/\W+/g, "_") + "_hook_cb",
|
|
109
|
+
configurable: true
|
|
110
|
+
});
|
|
111
|
+
} catch {
|
|
112
|
+
}
|
|
113
|
+
}
|
|
85
114
|
this._hooks[name] = this._hooks[name] || [];
|
|
86
115
|
this._hooks[name].push(function_);
|
|
87
116
|
return () => {
|
|
@@ -131,7 +160,9 @@ class Hookable {
|
|
|
131
160
|
}
|
|
132
161
|
addHooks(configHooks) {
|
|
133
162
|
const hooks = flatHooks(configHooks);
|
|
134
|
-
const removeFns = Object.keys(hooks).map(
|
|
163
|
+
const removeFns = Object.keys(hooks).map(
|
|
164
|
+
(key) => this.hook(key, hooks[key])
|
|
165
|
+
);
|
|
135
166
|
return () => {
|
|
136
167
|
for (const unreg of removeFns.splice(0, removeFns.length)) {
|
|
137
168
|
unreg();
|
|
@@ -144,11 +175,16 @@ class Hookable {
|
|
|
144
175
|
this.removeHook(key, hooks[key]);
|
|
145
176
|
}
|
|
146
177
|
}
|
|
178
|
+
removeAllHooks() {
|
|
179
|
+
for (const key in this._hooks) {
|
|
180
|
+
delete this._hooks[key];
|
|
181
|
+
}
|
|
182
|
+
}
|
|
147
183
|
callHook(name, ...arguments_) {
|
|
148
|
-
return this.callHookWith(
|
|
184
|
+
return this.callHookWith(serialTaskCaller, name, ...arguments_);
|
|
149
185
|
}
|
|
150
186
|
callHookParallel(name, ...arguments_) {
|
|
151
|
-
return this.callHookWith(
|
|
187
|
+
return this.callHookWith(parallelTaskCaller, name, ...arguments_);
|
|
152
188
|
}
|
|
153
189
|
callHookWith(caller, name, ...arguments_) {
|
|
154
190
|
const event = this._before || this._after ? { name, args: arguments_, context: {} } : void 0;
|
|
@@ -232,6 +268,7 @@ function createDebugger(hooks, _options = {}) {
|
|
|
232
268
|
_idCtr[event.name]--;
|
|
233
269
|
});
|
|
234
270
|
return {
|
|
271
|
+
/** Stop debugging and remove listeners */
|
|
235
272
|
close: () => {
|
|
236
273
|
unsubscribeBefore();
|
|
237
274
|
unsubscribeAfter();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hookable",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.5.0",
|
|
4
4
|
"description": "Awaitable hook system",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"hook",
|
|
@@ -23,22 +23,25 @@
|
|
|
23
23
|
"dist"
|
|
24
24
|
],
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@
|
|
27
|
-
"
|
|
28
|
-
"
|
|
26
|
+
"@types/node": "^18.15.0",
|
|
27
|
+
"@vitest/coverage-c8": "^0.29.2",
|
|
28
|
+
"changelogen": "^0.5.1",
|
|
29
|
+
"eslint": "^8.35.0",
|
|
30
|
+
"eslint-config-unjs": "^0.1.0",
|
|
29
31
|
"expect-type": "^0.15.0",
|
|
30
|
-
"
|
|
31
|
-
"typescript": "^4.
|
|
32
|
-
"unbuild": "^
|
|
33
|
-
"vitest": "^0.
|
|
32
|
+
"prettier": "^2.8.4",
|
|
33
|
+
"typescript": "^4.9.5",
|
|
34
|
+
"unbuild": "^1.1.2",
|
|
35
|
+
"vitest": "^0.29.2"
|
|
34
36
|
},
|
|
35
|
-
"packageManager": "pnpm@7.
|
|
37
|
+
"packageManager": "pnpm@7.29.1",
|
|
36
38
|
"scripts": {
|
|
37
39
|
"build": "unbuild",
|
|
38
40
|
"dev": "vitest",
|
|
39
|
-
"lint": "eslint --ext .ts src",
|
|
41
|
+
"lint": "eslint --cache --ext .ts,.js,.mjs,.cjs . && prettier -c src test",
|
|
42
|
+
"lint:fix": "eslint --cache --ext .ts,.js,.mjs,.cjs . --fix && prettier -c src test -w",
|
|
40
43
|
"prepublish": "pnpm build",
|
|
41
|
-
"release": "pnpm test && pnpm build &&
|
|
44
|
+
"release": "pnpm test && pnpm build && changelogen --release --push && pnpm publish",
|
|
42
45
|
"test": "pnpm lint && vitest run --coverage",
|
|
43
46
|
"test:types": "tsc --noEmit"
|
|
44
47
|
}
|