hookable 5.5.0 → 5.5.2

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
 
@@ -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
@@ -255,17 +259,13 @@ Thanks to [Joe Paice](https://github.com/RGBboy) for donating [hookable](https:/
255
259
  MIT - Made with 💖
256
260
 
257
261
  <!-- Badges -->
258
- [npm-version-src]: https://flat.badgen.net/npm/dt/hookable
262
+ [npm-version-src]: https://img.shields.io/npm/v/hookable?style=flat&colorA=18181B&colorB=F0DB4F
259
263
  [npm-version-href]: https://npmjs.com/package/hookable
260
-
261
- [npm-downloads-src]: https://flat.badgen.net/npm/v/hookable
264
+ [npm-downloads-src]: https://img.shields.io/npm/dm/hookable?style=flat&colorA=18181B&colorB=F0DB4F
262
265
  [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
266
+ [codecov-src]: https://img.shields.io/codecov/c/gh/unjs/hookable/main?style=flat&colorA=18181B&colorB=F0DB4F
267
+ [codecov-href]: https://codecov.io/gh/unjs/h3
268
+ [bundle-src]: https://img.shields.io/bundlephobia/minzip/hookable?style=flat&colorA=18181B&colorB=F0DB4F
269
+ [bundle-href]: https://bundlephobia.com/result?p=hookable
270
+ [license-src]: https://img.shields.io/github/license/unjs/hookable.svg?style=flat&colorA=18181B&colorB=F0DB4F
271
+ [license-href]: https://github.com/unjs/hookable/blob/main/LICENSE
package/dist/index.cjs CHANGED
@@ -43,25 +43,27 @@ function serial(tasks, function_) {
43
43
  const defaultTask = { run: (function_) => function_() };
44
44
  const _createTask = () => defaultTask;
45
45
  const createTask = typeof console.createTask !== "undefined" ? console.createTask : _createTask;
46
- function serialTaskCaller(hooks, name, ...args) {
46
+ function serialTaskCaller(hooks, args) {
47
+ const name = args.shift();
47
48
  const task = createTask(name);
48
49
  return hooks.reduce(
49
50
  (promise, hookFunction) => promise.then(() => task.run(() => hookFunction(...args))),
50
51
  Promise.resolve()
51
52
  );
52
53
  }
53
- function parallelTaskCaller(hooks, name, ...args) {
54
+ function parallelTaskCaller(hooks, args) {
55
+ const name = args.shift();
54
56
  const task = createTask(name);
55
57
  return Promise.all(hooks.map((hook) => task.run(() => hook(...args))));
56
58
  }
57
59
  function serialCaller(hooks, arguments_) {
58
60
  return hooks.reduce(
59
- (promise, hookFunction) => promise.then(() => hookFunction(...arguments_)),
61
+ (promise, hookFunction) => promise.then(() => hookFunction(...arguments_ || [])),
60
62
  Promise.resolve()
61
63
  );
62
64
  }
63
65
  function parallelCaller(hooks, args) {
64
- return Promise.all(hooks.map((hook) => hook(...args)));
66
+ return Promise.all(hooks.map((hook) => hook(...args || [])));
65
67
  }
66
68
  function callEachWith(callbacks, arg0) {
67
69
  for (const callback of callbacks) {
@@ -149,7 +151,7 @@ class Hookable {
149
151
  deprecateHook(name, deprecated) {
150
152
  this._deprecatedHooks[name] = typeof deprecated === "string" ? { to: deprecated } : deprecated;
151
153
  const _hooks = this._hooks[name] || [];
152
- this._hooks[name] = void 0;
154
+ delete this._hooks[name];
153
155
  for (const hook of _hooks) {
154
156
  this.hook(name, hook);
155
157
  }
@@ -183,9 +185,11 @@ class Hookable {
183
185
  }
184
186
  }
185
187
  callHook(name, ...arguments_) {
188
+ arguments_.unshift(name);
186
189
  return this.callHookWith(serialTaskCaller, name, ...arguments_);
187
190
  }
188
191
  callHookParallel(name, ...arguments_) {
192
+ arguments_.unshift(name);
189
193
  return this.callHookWith(parallelTaskCaller, name, ...arguments_);
190
194
  }
191
195
  callHookWith(caller, name, ...arguments_) {
@@ -210,9 +214,11 @@ class Hookable {
210
214
  this._before = this._before || [];
211
215
  this._before.push(function_);
212
216
  return () => {
213
- const index = this._before.indexOf(function_);
214
- if (index !== -1) {
215
- this._before.splice(index, 1);
217
+ if (this._before !== void 0) {
218
+ const index = this._before.indexOf(function_);
219
+ if (index !== -1) {
220
+ this._before.splice(index, 1);
221
+ }
216
222
  }
217
223
  };
218
224
  }
@@ -220,9 +226,11 @@ class Hookable {
220
226
  this._after = this._after || [];
221
227
  this._after.push(function_);
222
228
  return () => {
223
- const index = this._after.indexOf(function_);
224
- if (index !== -1) {
225
- this._after.splice(index, 1);
229
+ if (this._after !== void 0) {
230
+ const index = this._after.indexOf(function_);
231
+ if (index !== -1) {
232
+ this._after.splice(index, 1);
233
+ }
226
234
  }
227
235
  };
228
236
  }
@@ -245,7 +253,7 @@ function createDebugger(hooks, _options = {}) {
245
253
  const logPrefix = (event) => _tag + event.name + "".padEnd(event._id, "\0");
246
254
  const _idCtr = {};
247
255
  const unsubscribeBefore = hooks.beforeEach((event) => {
248
- if (!filter(event.name)) {
256
+ if (filter !== void 0 && !filter(event.name)) {
249
257
  return;
250
258
  }
251
259
  _idCtr[event.name] = _idCtr[event.name] || 0;
@@ -253,7 +261,7 @@ function createDebugger(hooks, _options = {}) {
253
261
  console.time(logPrefix(event));
254
262
  });
255
263
  const unsubscribeAfter = hooks.afterEach((event) => {
256
- if (!filter(event.name)) {
264
+ if (filter !== void 0 && !filter(event.name)) {
257
265
  return;
258
266
  }
259
267
  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>(): Hookable<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
@@ -41,25 +41,27 @@ function serial(tasks, function_) {
41
41
  const defaultTask = { run: (function_) => function_() };
42
42
  const _createTask = () => defaultTask;
43
43
  const createTask = typeof console.createTask !== "undefined" ? console.createTask : _createTask;
44
- function serialTaskCaller(hooks, name, ...args) {
44
+ function serialTaskCaller(hooks, args) {
45
+ const name = args.shift();
45
46
  const task = createTask(name);
46
47
  return hooks.reduce(
47
48
  (promise, hookFunction) => promise.then(() => task.run(() => hookFunction(...args))),
48
49
  Promise.resolve()
49
50
  );
50
51
  }
51
- function parallelTaskCaller(hooks, name, ...args) {
52
+ function parallelTaskCaller(hooks, args) {
53
+ const name = args.shift();
52
54
  const task = createTask(name);
53
55
  return Promise.all(hooks.map((hook) => task.run(() => hook(...args))));
54
56
  }
55
57
  function serialCaller(hooks, arguments_) {
56
58
  return hooks.reduce(
57
- (promise, hookFunction) => promise.then(() => hookFunction(...arguments_)),
59
+ (promise, hookFunction) => promise.then(() => hookFunction(...arguments_ || [])),
58
60
  Promise.resolve()
59
61
  );
60
62
  }
61
63
  function parallelCaller(hooks, args) {
62
- return Promise.all(hooks.map((hook) => hook(...args)));
64
+ return Promise.all(hooks.map((hook) => hook(...args || [])));
63
65
  }
64
66
  function callEachWith(callbacks, arg0) {
65
67
  for (const callback of callbacks) {
@@ -147,7 +149,7 @@ class Hookable {
147
149
  deprecateHook(name, deprecated) {
148
150
  this._deprecatedHooks[name] = typeof deprecated === "string" ? { to: deprecated } : deprecated;
149
151
  const _hooks = this._hooks[name] || [];
150
- this._hooks[name] = void 0;
152
+ delete this._hooks[name];
151
153
  for (const hook of _hooks) {
152
154
  this.hook(name, hook);
153
155
  }
@@ -181,9 +183,11 @@ class Hookable {
181
183
  }
182
184
  }
183
185
  callHook(name, ...arguments_) {
186
+ arguments_.unshift(name);
184
187
  return this.callHookWith(serialTaskCaller, name, ...arguments_);
185
188
  }
186
189
  callHookParallel(name, ...arguments_) {
190
+ arguments_.unshift(name);
187
191
  return this.callHookWith(parallelTaskCaller, name, ...arguments_);
188
192
  }
189
193
  callHookWith(caller, name, ...arguments_) {
@@ -208,9 +212,11 @@ class Hookable {
208
212
  this._before = this._before || [];
209
213
  this._before.push(function_);
210
214
  return () => {
211
- const index = this._before.indexOf(function_);
212
- if (index !== -1) {
213
- this._before.splice(index, 1);
215
+ if (this._before !== void 0) {
216
+ const index = this._before.indexOf(function_);
217
+ if (index !== -1) {
218
+ this._before.splice(index, 1);
219
+ }
214
220
  }
215
221
  };
216
222
  }
@@ -218,9 +224,11 @@ class Hookable {
218
224
  this._after = this._after || [];
219
225
  this._after.push(function_);
220
226
  return () => {
221
- const index = this._after.indexOf(function_);
222
- if (index !== -1) {
223
- this._after.splice(index, 1);
227
+ if (this._after !== void 0) {
228
+ const index = this._after.indexOf(function_);
229
+ if (index !== -1) {
230
+ this._after.splice(index, 1);
231
+ }
224
232
  }
225
233
  };
226
234
  }
@@ -243,7 +251,7 @@ function createDebugger(hooks, _options = {}) {
243
251
  const logPrefix = (event) => _tag + event.name + "".padEnd(event._id, "\0");
244
252
  const _idCtr = {};
245
253
  const unsubscribeBefore = hooks.beforeEach((event) => {
246
- if (!filter(event.name)) {
254
+ if (filter !== void 0 && !filter(event.name)) {
247
255
  return;
248
256
  }
249
257
  _idCtr[event.name] = _idCtr[event.name] || 0;
@@ -251,7 +259,7 @@ function createDebugger(hooks, _options = {}) {
251
259
  console.time(logPrefix(event));
252
260
  });
253
261
  const unsubscribeAfter = hooks.afterEach((event) => {
254
- if (!filter(event.name)) {
262
+ if (filter !== void 0 && !filter(event.name)) {
255
263
  return;
256
264
  }
257
265
  if (options.group) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hookable",
3
- "version": "5.5.0",
3
+ "version": "5.5.2",
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.0",
26
+ "@types/node": "^18.15.3",
27
27
  "@vitest/coverage-c8": "^0.29.2",
28
28
  "changelogen": "^0.5.1",
29
- "eslint": "^8.35.0",
29
+ "eslint": "^8.36.0",
30
30
  "eslint-config-unjs": "^0.1.0",
31
31
  "expect-type": "^0.15.0",
32
32
  "prettier": "^2.8.4",
33
33
  "typescript": "^4.9.5",
34
34
  "unbuild": "^1.1.2",
35
+ "vite": "^4.2.1",
35
36
  "vitest": "^0.29.2"
36
37
  },
37
- "packageManager": "pnpm@7.29.1",
38
+ "packageManager": "pnpm@7.29.2",
38
39
  "scripts": {
39
40
  "build": "unbuild",
40
41
  "dev": "vitest",