hookable 5.4.2 → 5.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 CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  [![npm version][npm-version-src]][npm-version-href]
4
4
  [![npm downloads][npm-downloads-src]][npm-downloads-href]
5
- [![packagephobia][packagephobia-src]][packagephobia-href]
6
- [![Github Actions CI][github-actions-ci-src]][github-actions-ci-href]
5
+ [![bundle][bundle-src]][bundle-href]
7
6
  [![Codecov][codecov-src]][codecov-href]
7
+ [![License][license-src]][license-href]
8
8
 
9
- > Awaitable hook system
9
+ Awaitable hooks system.
10
10
 
11
11
  ## Install
12
12
 
@@ -255,17 +255,13 @@ Thanks to [Joe Paice](https://github.com/RGBboy) for donating [hookable](https:/
255
255
  MIT - Made with 💖
256
256
 
257
257
  <!-- Badges -->
258
- [npm-version-src]: https://flat.badgen.net/npm/dt/hookable
258
+ [npm-version-src]: https://img.shields.io/npm/v/hookable?style=flat&colorA=18181B&colorB=F0DB4F
259
259
  [npm-version-href]: https://npmjs.com/package/hookable
260
-
261
- [npm-downloads-src]: https://flat.badgen.net/npm/v/hookable
260
+ [npm-downloads-src]: https://img.shields.io/npm/dm/hookable?style=flat&colorA=18181B&colorB=F0DB4F
262
261
  [npm-downloads-href]: https://npmjs.com/package/hookable
263
-
264
- [github-actions-ci-src]: https://flat.badgen.net/github/checks/unjs/hookable/main
265
- [github-actions-ci-href]: https://github.com/unjs/hookable/actions
266
-
267
- [codecov-src]: https://flat.badgen.net/codecov/c/github/unjs/hookable
268
- [codecov-href]: https://codecov.io/gh/unjs/hookable
269
-
270
- [packagephobia-src]: https://flat.badgen.net/packagephobia/install/hookable
271
- [packagephobia-href]: https://packagephobia.now.sh/result?p=hookable
262
+ [codecov-src]: https://img.shields.io/codecov/c/gh/unjs/hookable/main?style=flat&colorA=18181B&colorB=F0DB4F
263
+ [codecov-href]: https://codecov.io/gh/unjs/h3
264
+ [bundle-src]: https://img.shields.io/bundlephobia/minzip/hookable?style=flat&colorA=18181B&colorB=F0DB4F
265
+ [bundle-href]: https://bundlephobia.com/result?p=hookable
266
+ [license-src]: https://img.shields.io/github/license/unjs/hookable.svg?style=flat&colorA=18181B&colorB=F0DB4F
267
+ [license-href]: https://github.com/unjs/hookable/blob/main/LICENSE
package/dist/index.cjs CHANGED
@@ -35,17 +35,39 @@ function mergeHooks(...hooks) {
35
35
  return finalHooks;
36
36
  }
37
37
  function serial(tasks, function_) {
38
- return tasks.reduce((promise, task) => promise.then(() => function_(task)), Promise.resolve());
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, args) {
47
+ const name = args.shift();
48
+ const task = createTask(name);
49
+ return hooks.reduce(
50
+ (promise, hookFunction) => promise.then(() => task.run(() => hookFunction(...args))),
51
+ Promise.resolve()
52
+ );
53
+ }
54
+ function parallelTaskCaller(hooks, args) {
55
+ const name = args.shift();
56
+ const task = createTask(name);
57
+ return Promise.all(hooks.map((hook) => task.run(() => hook(...args))));
39
58
  }
40
59
  function serialCaller(hooks, arguments_) {
41
- return hooks.reduce((promise, hookFunction) => promise.then(() => hookFunction.apply(void 0, arguments_)), Promise.resolve());
60
+ return hooks.reduce(
61
+ (promise, hookFunction) => promise.then(() => hookFunction(...arguments_)),
62
+ Promise.resolve()
63
+ );
42
64
  }
43
- function parallelCaller(hooks, arguments_) {
44
- return Promise.all(hooks.map((hook) => hook.apply(void 0, arguments_)));
65
+ function parallelCaller(hooks, args) {
66
+ return Promise.all(hooks.map((hook) => hook(...args)));
45
67
  }
46
- function callEachWith(callbacks, argument0) {
68
+ function callEachWith(callbacks, arg0) {
47
69
  for (const callback of callbacks) {
48
- callback(argument0);
70
+ callback(arg0);
49
71
  }
50
72
  }
51
73
 
@@ -84,6 +106,15 @@ class Hookable {
84
106
  this._deprecatedMessages.add(message);
85
107
  }
86
108
  }
109
+ if (!function_.name) {
110
+ try {
111
+ Object.defineProperty(function_, "name", {
112
+ get: () => "_" + name.replace(/\W+/g, "_") + "_hook_cb",
113
+ configurable: true
114
+ });
115
+ } catch {
116
+ }
117
+ }
87
118
  this._hooks[name] = this._hooks[name] || [];
88
119
  this._hooks[name].push(function_);
89
120
  return () => {
@@ -133,7 +164,9 @@ class Hookable {
133
164
  }
134
165
  addHooks(configHooks) {
135
166
  const hooks = flatHooks(configHooks);
136
- const removeFns = Object.keys(hooks).map((key) => this.hook(key, hooks[key]));
167
+ const removeFns = Object.keys(hooks).map(
168
+ (key) => this.hook(key, hooks[key])
169
+ );
137
170
  return () => {
138
171
  for (const unreg of removeFns.splice(0, removeFns.length)) {
139
172
  unreg();
@@ -146,11 +179,18 @@ class Hookable {
146
179
  this.removeHook(key, hooks[key]);
147
180
  }
148
181
  }
182
+ removeAllHooks() {
183
+ for (const key in this._hooks) {
184
+ delete this._hooks[key];
185
+ }
186
+ }
149
187
  callHook(name, ...arguments_) {
150
- return this.callHookWith(serialCaller, name, ...arguments_);
188
+ arguments_.unshift(name);
189
+ return this.callHookWith(serialTaskCaller, name, ...arguments_);
151
190
  }
152
191
  callHookParallel(name, ...arguments_) {
153
- return this.callHookWith(parallelCaller, name, ...arguments_);
192
+ arguments_.unshift(name);
193
+ return this.callHookWith(parallelTaskCaller, name, ...arguments_);
154
194
  }
155
195
  callHookWith(caller, name, ...arguments_) {
156
196
  const event = this._before || this._after ? { name, args: arguments_, context: {} } : void 0;
@@ -234,6 +274,7 @@ function createDebugger(hooks, _options = {}) {
234
274
  _idCtr[event.name]--;
235
275
  });
236
276
  return {
277
+ /** Stop debugging and remove listeners */
237
278
  close: () => {
238
279
  unsubscribeBefore();
239
280
  unsubscribeAfter();
package/dist/index.d.ts CHANGED
@@ -1,42 +1,42 @@
1
- declare type HookCallback = (...arguments_: any) => Promise<void> | void;
1
+ type HookCallback = (...arguments_: any) => Promise<void> | void;
2
2
  interface Hooks {
3
3
  [key: string]: HookCallback;
4
4
  }
5
- declare type HookKeys<T> = keyof T & string;
6
- declare type DeprecatedHook<T> = {
5
+ type HookKeys<T> = keyof T & string;
6
+ type DeprecatedHook<T> = {
7
7
  message?: string;
8
8
  to: HookKeys<T>;
9
9
  };
10
- declare type DeprecatedHooks<T> = {
10
+ type DeprecatedHooks<T> = {
11
11
  [name in HookKeys<T>]: DeprecatedHook<T>;
12
12
  };
13
- declare type ValueOf<C> = C extends Record<any, any> ? C[keyof C] : never;
14
- declare type Strings<T> = Exclude<keyof T, number | symbol>;
15
- declare type KnownKeys<T> = keyof {
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
- declare type StripGeneric<T> = Pick<T, KnownKeys<T> extends keyof T ? KnownKeys<T> : never>;
19
- declare type OnlyGeneric<T> = Omit<T, KnownKeys<T> extends keyof T ? KnownKeys<T> : never>;
20
- declare type Namespaces<T> = ValueOf<{
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
- declare type BareHooks<T> = ValueOf<{
23
+ type BareHooks<T> = ValueOf<{
24
24
  [key in Strings<T>]: key extends `${string}:${string}` ? never : key;
25
25
  }>;
26
- declare type HooksInNamespace<T, Namespace extends string> = ValueOf<{
26
+ type HooksInNamespace<T, Namespace extends string> = ValueOf<{
27
27
  [key in Strings<T>]: key extends `${Namespace}:${infer HookName}` ? HookName : never;
28
28
  }>;
29
- declare type WithoutNamespace<T, Namespace extends string> = {
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
- declare type NestedHooks<T> = (Partial<StripGeneric<T>> | Partial<OnlyGeneric<T>>) & Partial<{
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
- declare type InferCallback<HT, HN extends keyof HT> = HT[HN] extends HookCallback ? HT[HN] : never;
39
- declare type InferSpyEvent<HT extends Record<string, any>> = {
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
- declare function serialCaller(hooks: HookCallback[], arguments_?: any[]): Promise<any>;
74
- declare function parallelCaller(hooks: HookCallback[], arguments_?: any[]): Promise<any[]>;
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,39 @@ function mergeHooks(...hooks) {
33
33
  return finalHooks;
34
34
  }
35
35
  function serial(tasks, function_) {
36
- return tasks.reduce((promise, task) => promise.then(() => function_(task)), Promise.resolve());
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, args) {
45
+ const name = args.shift();
46
+ const task = createTask(name);
47
+ return hooks.reduce(
48
+ (promise, hookFunction) => promise.then(() => task.run(() => hookFunction(...args))),
49
+ Promise.resolve()
50
+ );
51
+ }
52
+ function parallelTaskCaller(hooks, args) {
53
+ const name = args.shift();
54
+ const task = createTask(name);
55
+ return Promise.all(hooks.map((hook) => task.run(() => hook(...args))));
37
56
  }
38
57
  function serialCaller(hooks, arguments_) {
39
- return hooks.reduce((promise, hookFunction) => promise.then(() => hookFunction.apply(void 0, arguments_)), Promise.resolve());
58
+ return hooks.reduce(
59
+ (promise, hookFunction) => promise.then(() => hookFunction(...arguments_)),
60
+ Promise.resolve()
61
+ );
40
62
  }
41
- function parallelCaller(hooks, arguments_) {
42
- return Promise.all(hooks.map((hook) => hook.apply(void 0, arguments_)));
63
+ function parallelCaller(hooks, args) {
64
+ return Promise.all(hooks.map((hook) => hook(...args)));
43
65
  }
44
- function callEachWith(callbacks, argument0) {
66
+ function callEachWith(callbacks, arg0) {
45
67
  for (const callback of callbacks) {
46
- callback(argument0);
68
+ callback(arg0);
47
69
  }
48
70
  }
49
71
 
@@ -82,6 +104,15 @@ class Hookable {
82
104
  this._deprecatedMessages.add(message);
83
105
  }
84
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
+ }
85
116
  this._hooks[name] = this._hooks[name] || [];
86
117
  this._hooks[name].push(function_);
87
118
  return () => {
@@ -131,7 +162,9 @@ class Hookable {
131
162
  }
132
163
  addHooks(configHooks) {
133
164
  const hooks = flatHooks(configHooks);
134
- const removeFns = Object.keys(hooks).map((key) => this.hook(key, hooks[key]));
165
+ const removeFns = Object.keys(hooks).map(
166
+ (key) => this.hook(key, hooks[key])
167
+ );
135
168
  return () => {
136
169
  for (const unreg of removeFns.splice(0, removeFns.length)) {
137
170
  unreg();
@@ -144,11 +177,18 @@ class Hookable {
144
177
  this.removeHook(key, hooks[key]);
145
178
  }
146
179
  }
180
+ removeAllHooks() {
181
+ for (const key in this._hooks) {
182
+ delete this._hooks[key];
183
+ }
184
+ }
147
185
  callHook(name, ...arguments_) {
148
- return this.callHookWith(serialCaller, name, ...arguments_);
186
+ arguments_.unshift(name);
187
+ return this.callHookWith(serialTaskCaller, name, ...arguments_);
149
188
  }
150
189
  callHookParallel(name, ...arguments_) {
151
- return this.callHookWith(parallelCaller, name, ...arguments_);
190
+ arguments_.unshift(name);
191
+ return this.callHookWith(parallelTaskCaller, name, ...arguments_);
152
192
  }
153
193
  callHookWith(caller, name, ...arguments_) {
154
194
  const event = this._before || this._after ? { name, args: arguments_, context: {} } : void 0;
@@ -232,6 +272,7 @@ function createDebugger(hooks, _options = {}) {
232
272
  _idCtr[event.name]--;
233
273
  });
234
274
  return {
275
+ /** Stop debugging and remove listeners */
235
276
  close: () => {
236
277
  unsubscribeBefore();
237
278
  unsubscribeAfter();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hookable",
3
- "version": "5.4.2",
3
+ "version": "5.5.1",
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
- "@vitest/coverage-c8": "^0.25.2",
27
- "eslint": "^8.27.0",
28
- "eslint-config-unjs": "^0.0.2",
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
- "standard-version": "^9.5.0",
31
- "typescript": "^4.8.4",
32
- "unbuild": "^0.9.4",
33
- "vitest": "^0.25.2"
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.16.0",
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 && standard-version && git push --follow-tags && pnpm publish",
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
  }