hookable 5.0.0-0 → 5.1.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/README.md CHANGED
@@ -5,9 +5,8 @@
5
5
  [![packagephobia][packagephobia-src]][packagephobia-href]
6
6
  [![Github Actions CI][github-actions-ci-src]][github-actions-ci-href]
7
7
  [![Codecov][codecov-src]][codecov-href]
8
- [![Dependencies][david-dm-src]][david-dm-href]
9
8
 
10
- > Awaitable hooks for Node.js and Browser
9
+ > Awaitable hook system
11
10
 
12
11
  ## Install
13
12
 
@@ -86,7 +85,7 @@ const hook2 = async () => { /* ... */ }
86
85
 
87
86
  // The hook() method returns an "unregister" function
88
87
  const unregisterHook0 = lib.hook('hook0', hook0)
89
- const unregisterHooks1and2 lib.addHooks({ hook1, hook2 })
88
+ const unregisterHooks1and2 = lib.addHooks({ hook1, hook2 })
90
89
 
91
90
  /* ... */
92
91
 
@@ -153,6 +152,14 @@ Returns an `unregister` function that, when called, will remove all the register
153
152
 
154
153
  Used by class itself to **sequentially** call handlers of a specific hook.
155
154
 
155
+ ### `callHookWith (name, callerFn)`
156
+
157
+ If you need custom control over how hooks are called, you can provide a custom function that will receive an array of handlers of a specific hook.
158
+
159
+ `callerFn` if a callback function that accepts two arguments, `hooks` and `args`:
160
+ - `hooks`: Array of user hooks to be called
161
+ - `args`: Array of arguments that should be passed each time calling a hook
162
+
156
163
  ### `deprecateHook (old, name)`
157
164
 
158
165
  Deprecate hook called `old` in favor of `name` hook.
@@ -187,17 +194,26 @@ hookable.removeHooks({
187
194
  })
188
195
  ```
189
196
 
190
- ## Credits
197
+ ## Migration
191
198
 
192
- Extracted from [Nuxt.js](https://github.com/nuxt/nuxt.js) hooks system
199
+ ### From `4.x` to `5.x`
193
200
 
194
- Original author [Sébastien Chopin](https://github.com/Atinux)
201
+ - Type checking improved. You can use `Hookable<T>` or `createHooks<T>()` to provide types interface **([c2e1e22](https://github.com/unjs/hookable/commit/c2e1e223d16e7bf87117cd8d72ad3ba211a333d8))**
202
+ - We no longer provide an IE11 compatible umd build. Instead, you should use an ESM-aware bundler such as webpack or rollup to transpile if needed.
203
+ - Logger param is dropped. We use `console.warn` by default for deprecated hooks.
204
+ - Package now uses named exports. You should import `{ Hookable }` instead of `Hookable` or use new `createHooks` util
205
+ - `mergeHooks` util is exported standalone. You should replace `Hookable.mergeHooks` and `this.mergeHooks` with new `{ mergeHooks }` export
206
+ - In versions < 5.0.0 when using `callHook` if an error happened by one of the hook callbacks, we was handling errors globally and call global `error` hook + `console.error` instead and resolve `callHook` promise! This sometimes makes confusing behavior when we think code worked but it didn't. v5 introduced a breaking change that when a hook throws an error, `callHook` also rejects instead of a global `error` event. This means you should be careful to handle all errors when using `callHook` now.
195
207
 
196
- Thanks to [Joe Paice](https://github.com/RGBboy) for donating [hookable](https://www.npmjs.com/package/hookable) package name
208
+ ## Credits
209
+
210
+ Extracted from [Nuxt](https://github.com/nuxt/nuxt.js) hooks system originally introduced by [Sébastien Chopin](https://github.com/Atinux)
211
+
212
+ Thanks to [Joe Paice](https://github.com/RGBboy) for donating [hookable](https://www.npmjs.com/package/hookable) package name.
197
213
 
198
214
  ## License
199
215
 
200
- MIT - Made with 💖 by Nuxt.js team!
216
+ MIT - Made with 💖
201
217
 
202
218
  <!-- Badges -->
203
219
  [npm-version-src]: https://flat.badgen.net/npm/dt/hookable
@@ -206,14 +222,11 @@ MIT - Made with 💖 by Nuxt.js team!
206
222
  [npm-downloads-src]: https://flat.badgen.net/npm/v/hookable
207
223
  [npm-downloads-href]: https://npmjs.com/package/hookable
208
224
 
209
- [github-actions-ci-src]: https://github.com/unjs/hookable/workflows/ci/badge.svg
210
- [github-actions-ci-href]: https://github.com/unjs/hookable/actions?query=workflow%3Aci
225
+ [github-actions-ci-src]: https://flat.badgen.net/github/checks/unjs/hookable/main
226
+ [github-actions-ci-href]: https://github.com/unjs/hookable/actions
211
227
 
212
228
  [codecov-src]: https://flat.badgen.net/codecov/c/github/unjs/hookable
213
229
  [codecov-href]: https://codecov.io/gh/unjs/hookable
214
230
 
215
- [david-dm-src]: https://flat.badgen.net/david/dep/unjs/hookable
216
- [david-dm-href]: https://david-dm.org/unjs/hookable
217
-
218
231
  [packagephobia-src]: https://flat.badgen.net/packagephobia/install/hookable
219
232
  [packagephobia-href]: https://packagephobia.now.sh/result?p=hookable
package/dist/index.cjs CHANGED
@@ -39,6 +39,12 @@ function mergeHooks(...hooks) {
39
39
  function serial(tasks, fn) {
40
40
  return tasks.reduce((promise, task) => promise.then(() => fn(task)), Promise.resolve(null));
41
41
  }
42
+ function serialCaller(hooks, args) {
43
+ return hooks.reduce((promise, hookFn) => promise.then(() => hookFn.apply(void 0, args)), Promise.resolve(null));
44
+ }
45
+ function parallelCaller(hooks, args) {
46
+ return Promise.all(hooks.map((hook) => hook.apply(void 0, args)));
47
+ }
42
48
 
43
49
  class Hookable {
44
50
  constructor() {
@@ -46,6 +52,7 @@ class Hookable {
46
52
  this._deprecatedHooks = {};
47
53
  this.hook = this.hook.bind(this);
48
54
  this.callHook = this.callHook.bind(this);
55
+ this.callHookWith = this.callHookWith.bind(this);
49
56
  }
50
57
  hook(name, fn) {
51
58
  if (!name || typeof fn !== "function") {
@@ -53,19 +60,21 @@ class Hookable {
53
60
  };
54
61
  }
55
62
  const originalName = name;
56
- let deprecatedHook;
63
+ let deprecatedHookObj;
57
64
  while (this._deprecatedHooks[name]) {
58
- deprecatedHook = this._deprecatedHooks[name];
65
+ const deprecatedHook = this._deprecatedHooks[name];
59
66
  if (typeof deprecatedHook === "string") {
60
- deprecatedHook = { to: deprecatedHook };
67
+ deprecatedHookObj = { to: deprecatedHook };
68
+ } else {
69
+ deprecatedHookObj = deprecatedHook;
61
70
  }
62
- name = deprecatedHook.to;
71
+ name = deprecatedHookObj.to;
63
72
  }
64
- if (deprecatedHook) {
65
- if (!deprecatedHook.message) {
66
- console.warn(`${originalName} hook has been deprecated` + (deprecatedHook.to ? `, please use ${deprecatedHook.to}` : ""));
73
+ if (deprecatedHookObj) {
74
+ if (!deprecatedHookObj.message) {
75
+ console.warn(`${originalName} hook has been deprecated` + (deprecatedHookObj.to ? `, please use ${deprecatedHookObj.to}` : ""));
67
76
  } else {
68
- console.warn(deprecatedHook.message);
77
+ console.warn(deprecatedHookObj.message);
69
78
  }
70
79
  }
71
80
  this._hooks[name] = this._hooks[name] || [];
@@ -119,10 +128,19 @@ class Hookable {
119
128
  }
120
129
  }
121
130
  callHook(name, ...args) {
122
- if (!this._hooks[name]) {
123
- return;
131
+ if (this._hooks[name]) {
132
+ return serialCaller(this._hooks[name], args);
133
+ }
134
+ }
135
+ callHookParallel(name, ...args) {
136
+ if (this._hooks[name]) {
137
+ return parallelCaller(this._hooks[name], args);
138
+ }
139
+ }
140
+ callHookWith(caller, name, ...args) {
141
+ if (this._hooks[name]) {
142
+ return caller(this._hooks[name], args);
124
143
  }
125
- return serial(this._hooks[name], (fn) => fn(...args));
126
144
  }
127
145
  }
128
146
  function createHooks() {
@@ -133,4 +151,6 @@ exports.Hookable = Hookable;
133
151
  exports.createHooks = createHooks;
134
152
  exports.flatHooks = flatHooks;
135
153
  exports.mergeHooks = mergeHooks;
154
+ exports.parallelCaller = parallelCaller;
136
155
  exports.serial = serial;
156
+ exports.serialCaller = serialCaller;
@@ -0,0 +1,62 @@
1
+ declare type HookCallback = (...args: any) => Promise<void> | void;
2
+ interface Hooks {
3
+ [key: string]: HookCallback;
4
+ }
5
+ declare type HookKeys<T> = keyof T & string;
6
+ declare type DeprecatedHook<T> = string | {
7
+ message?: string;
8
+ to: HookKeys<T>;
9
+ };
10
+ declare type DeprecatedHooks<T> = {
11
+ [name in HookKeys<T>]: DeprecatedHook<T>;
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 {
16
+ [K in keyof T as string extends K ? never : number extends K ? never : K]: never;
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<{
21
+ [key in Strings<T>]: key extends `${infer Namespace}:${string}` ? Namespace : never;
22
+ }>;
23
+ declare type BareHooks<T> = ValueOf<{
24
+ [key in Strings<T>]: key extends `${string}:${string}` ? never : key;
25
+ }>;
26
+ declare type HooksInNamespace<T, Namespace extends string> = ValueOf<{
27
+ [key in Strings<T>]: key extends `${Namespace}:${infer HookName}` ? HookName : never;
28
+ }>;
29
+ declare type WithoutNamespace<T, Namespace extends string> = {
30
+ [key in HooksInNamespace<T, Namespace>]: `${Namespace}:${key}` extends keyof T ? T[`${Namespace}:${key}`] : never;
31
+ };
32
+ declare type NestedHooks<T> = (Partial<StripGeneric<T>> | Partial<OnlyGeneric<T>>) & Partial<{
33
+ [key in Namespaces<StripGeneric<T>>]: NestedHooks<WithoutNamespace<T, key>>;
34
+ }> & Partial<{
35
+ [key in BareHooks<StripGeneric<T>>]: T[key];
36
+ }>;
37
+
38
+ declare type InferCallback<HT, HN extends keyof HT> = HT[HN] extends HookCallback ? HT[HN] : never;
39
+ declare class Hookable<HooksT = Record<string, HookCallback>, HookNameT extends HookKeys<HooksT> = HookKeys<HooksT>> {
40
+ private _hooks;
41
+ private _deprecatedHooks;
42
+ constructor();
43
+ hook<NameT extends HookNameT>(name: NameT, fn: InferCallback<HooksT, NameT>): () => void;
44
+ hookOnce<NameT extends HookNameT>(name: NameT, fn: InferCallback<HooksT, NameT>): () => void;
45
+ removeHook<NameT extends HookNameT>(name: NameT, fn: InferCallback<HooksT, NameT>): void;
46
+ deprecateHook<NameT extends HookNameT>(name: NameT, deprecated: DeprecatedHook<HooksT>): void;
47
+ deprecateHooks(deprecatedHooks: Record<HookNameT, DeprecatedHook<HooksT>>): void;
48
+ addHooks(configHooks: NestedHooks<HooksT>): () => void;
49
+ removeHooks(configHooks: NestedHooks<HooksT>): void;
50
+ callHook<NameT extends HookNameT>(name: NameT, ...args: Parameters<InferCallback<HooksT, NameT>>): Promise<any>;
51
+ callHookParallel<NameT extends HookNameT>(name: NameT, ...args: Parameters<InferCallback<HooksT, NameT>>): Promise<any[]>;
52
+ callHookWith<NameT extends HookNameT, CallFunction extends (hooks: HookCallback[], args: Parameters<InferCallback<HooksT, NameT>>) => any>(caller: CallFunction, name: NameT, ...args: Parameters<InferCallback<HooksT, NameT>>): void | ReturnType<CallFunction>;
53
+ }
54
+ declare function createHooks<T>(): Hookable<T>;
55
+
56
+ declare function flatHooks<T>(configHooks: NestedHooks<T>, hooks?: T, parentName?: string): T;
57
+ declare function mergeHooks<T>(...hooks: NestedHooks<T>[]): T;
58
+ declare function serial<T>(tasks: T[], fn: (task: T) => Promise<any> | any): Promise<any>;
59
+ declare function serialCaller(hooks: HookCallback[], args?: any[]): Promise<any>;
60
+ declare function parallelCaller(hooks: HookCallback[], args?: any[]): Promise<any[]>;
61
+
62
+ export { DeprecatedHook, DeprecatedHooks, HookCallback, HookKeys, Hookable, Hooks, NestedHooks, createHooks, flatHooks, mergeHooks, parallelCaller, serial, serialCaller };
package/dist/index.mjs CHANGED
@@ -35,6 +35,12 @@ function mergeHooks(...hooks) {
35
35
  function serial(tasks, fn) {
36
36
  return tasks.reduce((promise, task) => promise.then(() => fn(task)), Promise.resolve(null));
37
37
  }
38
+ function serialCaller(hooks, args) {
39
+ return hooks.reduce((promise, hookFn) => promise.then(() => hookFn.apply(void 0, args)), Promise.resolve(null));
40
+ }
41
+ function parallelCaller(hooks, args) {
42
+ return Promise.all(hooks.map((hook) => hook.apply(void 0, args)));
43
+ }
38
44
 
39
45
  class Hookable {
40
46
  constructor() {
@@ -42,6 +48,7 @@ class Hookable {
42
48
  this._deprecatedHooks = {};
43
49
  this.hook = this.hook.bind(this);
44
50
  this.callHook = this.callHook.bind(this);
51
+ this.callHookWith = this.callHookWith.bind(this);
45
52
  }
46
53
  hook(name, fn) {
47
54
  if (!name || typeof fn !== "function") {
@@ -49,19 +56,21 @@ class Hookable {
49
56
  };
50
57
  }
51
58
  const originalName = name;
52
- let deprecatedHook;
59
+ let deprecatedHookObj;
53
60
  while (this._deprecatedHooks[name]) {
54
- deprecatedHook = this._deprecatedHooks[name];
61
+ const deprecatedHook = this._deprecatedHooks[name];
55
62
  if (typeof deprecatedHook === "string") {
56
- deprecatedHook = { to: deprecatedHook };
63
+ deprecatedHookObj = { to: deprecatedHook };
64
+ } else {
65
+ deprecatedHookObj = deprecatedHook;
57
66
  }
58
- name = deprecatedHook.to;
67
+ name = deprecatedHookObj.to;
59
68
  }
60
- if (deprecatedHook) {
61
- if (!deprecatedHook.message) {
62
- console.warn(`${originalName} hook has been deprecated` + (deprecatedHook.to ? `, please use ${deprecatedHook.to}` : ""));
69
+ if (deprecatedHookObj) {
70
+ if (!deprecatedHookObj.message) {
71
+ console.warn(`${originalName} hook has been deprecated` + (deprecatedHookObj.to ? `, please use ${deprecatedHookObj.to}` : ""));
63
72
  } else {
64
- console.warn(deprecatedHook.message);
73
+ console.warn(deprecatedHookObj.message);
65
74
  }
66
75
  }
67
76
  this._hooks[name] = this._hooks[name] || [];
@@ -115,14 +124,23 @@ class Hookable {
115
124
  }
116
125
  }
117
126
  callHook(name, ...args) {
118
- if (!this._hooks[name]) {
119
- return;
127
+ if (this._hooks[name]) {
128
+ return serialCaller(this._hooks[name], args);
129
+ }
130
+ }
131
+ callHookParallel(name, ...args) {
132
+ if (this._hooks[name]) {
133
+ return parallelCaller(this._hooks[name], args);
134
+ }
135
+ }
136
+ callHookWith(caller, name, ...args) {
137
+ if (this._hooks[name]) {
138
+ return caller(this._hooks[name], args);
120
139
  }
121
- return serial(this._hooks[name], (fn) => fn(...args));
122
140
  }
123
141
  }
124
142
  function createHooks() {
125
143
  return new Hookable();
126
144
  }
127
145
 
128
- export { Hookable, createHooks, flatHooks, mergeHooks, serial };
146
+ export { Hookable, createHooks, flatHooks, mergeHooks, parallelCaller, serial, serialCaller };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "hookable",
3
- "version": "5.0.0-0",
4
- "description": "Awaitable hooks for Node.js",
3
+ "version": "5.1.0",
4
+ "description": "Awaitable hook system",
5
5
  "keywords": [
6
6
  "hook",
7
7
  "hookable",
@@ -17,13 +17,12 @@
17
17
  },
18
18
  "main": "./dist/index.cjs",
19
19
  "module": "./dist/index.mjs",
20
- "types": "./types/index.d.ts",
20
+ "types": "./dist/index.d.ts",
21
21
  "files": [
22
- "dist",
23
- "types"
22
+ "dist"
24
23
  ],
25
24
  "scripts": {
26
- "build": "siroc build",
25
+ "build": "unbuild",
27
26
  "lint": "eslint --ext .ts src",
28
27
  "prepublish": "yarn build",
29
28
  "release": "yarn test && yarn build && standard-version && git push --follow-tags && npm publish",
@@ -35,11 +34,11 @@
35
34
  "babel-jest": "latest",
36
35
  "codecov": "latest",
37
36
  "eslint": "latest",
37
+ "expect-type": "^0.12.0",
38
38
  "jest": "latest",
39
- "rollup-plugin-typescript2": "latest",
40
- "siroc": "latest",
41
39
  "standard-version": "latest",
42
40
  "ts-jest": "latest",
43
- "typescript": "latest"
41
+ "typescript": "latest",
42
+ "unbuild": "latest"
44
43
  }
45
44
  }
package/CHANGELOG.md DELETED
@@ -1,247 +0,0 @@
1
- # Changelog
2
-
3
- All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
-
5
- ## [5.0.0-0](https://github.com/unjs/hookable/compare/v4.4.1...v5.0.0-0) (2021-08-26)
6
-
7
-
8
- ### ⚠ BREAKING CHANGES
9
-
10
- * You should directly handle errors with callHook
11
- * use named exports and expose createHooks
12
- * remove mergehooks from Hookable prototype
13
- * drop browser build and use exports field
14
- * improve type checking
15
-
16
- ### Features
17
-
18
- * drop browser build and use exports field ([b626770](https://github.com/unjs/hookable/commit/b626770192a62b23acc1be16b4e4eac2383e9136))
19
- * drop logger and global error handler ([ee6ea87](https://github.com/unjs/hookable/commit/ee6ea87ad0e15a52252389b9b3112060bd411330))
20
- * improve type checking ([c2e1e22](https://github.com/unjs/hookable/commit/c2e1e223d16e7bf87117cd8d72ad3ba211a333d8))
21
- * use named exports and expose createHooks ([fadfcbd](https://github.com/unjs/hookable/commit/fadfcbd07c3e2fa6905d0e31deabc37fc55d8317))
22
-
23
-
24
- * remove mergehooks from Hookable prototype ([d50af59](https://github.com/unjs/hookable/commit/d50af595d3cb05be8fb5060374f4e28b3d6259fa))
25
-
26
- ### [4.4.1](https://github.com/unjs/hookable/compare/v4.4.0...v4.4.1) (2021-02-26)
27
-
28
-
29
- ### Bug Fixes
30
-
31
- * avoid creating extra wrapper when merging hooks ([790c1c4](https://github.com/unjs/hookable/commit/790c1c4dbbd1f7b9cb1995b40baeecead5346ed0))
32
-
33
- ## [4.4.0](https://github.com/unjs/hookable/compare/v4.3.1...v4.4.0) (2021-01-21)
34
-
35
-
36
- ### Features
37
-
38
- * **pkg:** expose module format ([2987b09](https://github.com/unjs/hookable/commit/2987b0901e292eb4570f8141ca51cfd9d2d3f94f))
39
-
40
- ### [4.3.1](https://github.com/unjs/hookable/compare/v4.3.0...v4.3.1) (2020-11-06)
41
-
42
-
43
- ### Bug Fixes
44
-
45
- * expose types ([0ffbaff](https://github.com/unjs/hookable/commit/0ffbaffa91d38e333e0818cd73a084cf1e8657c8))
46
-
47
- ## [4.3.0](https://github.com/unjs/hookable/compare/v4.2.0...v4.3.0) (2020-11-06)
48
-
49
-
50
- ### Features
51
-
52
- * `mergeHooks` helper ([#26](https://github.com/unjs/hookable/issues/26)) ([8c52d03](https://github.com/unjs/hookable/commit/8c52d034aa1a40bafb13d0b847c72593c51fcba5))
53
-
54
- ## [4.2.0](https://github.com/unjs/hookable/compare/v4.1.2...v4.2.0) (2020-10-23)
55
-
56
-
57
- ### Features
58
-
59
- * hookOnce ([225fa8a](https://github.com/unjs/hookable/commit/225fa8af85e1a504916c357f57b047143b6bc5ab))
60
-
61
-
62
- ### Bug Fixes
63
-
64
- * typecheck for flatHooks ([7800190](https://github.com/unjs/hookable/commit/7800190feb82134be5dd089ca184a18a6644da19))
65
-
66
- ### [4.1.2](https://github.com/unjs/hookable/compare/v4.1.1...v4.1.2) (2020-08-24)
67
-
68
-
69
- ### Bug Fixes
70
-
71
- * **build:** exclude regenerator and update target to ie 11 ([48acfc5](https://github.com/unjs/hookable/commit/48acfc5c0a4b0fb8edc6e9790b37ad336c966215))
72
-
73
- ### [4.1.1](https://github.com/unjs/hookable/compare/v4.1.0...v4.1.1) (2020-04-28)
74
-
75
-
76
- ### Bug Fixes
77
-
78
- * **pkg:** typo in types entry name (fixes [#19](https://github.com/unjs/hookable/issues/19)) ([b9ba90f](https://github.com/unjs/hookable/commit/b9ba90fbc725097e41c430d7df4205985c2faaec))
79
-
80
- ## [4.1.0](https://github.com/unjs/hookable/compare/v4.0.0...v4.1.0) (2020-04-17)
81
-
82
-
83
- ### Features
84
-
85
- * **types:** implement strict types ([823cdca](https://github.com/unjs/hookable/commit/823cdcac728d189b802f75faa9a361ac5ea4883d))
86
-
87
- ## [4.0.0](https://github.com/unjs/hookable/compare/v3.0.0...v4.0.0) (2020-04-17)
88
-
89
-
90
- ### ⚠ BREAKING CHANGES
91
-
92
- * only dist and types getting published
93
-
94
- ### Features
95
-
96
- * allow disabling logger ([f8fb742](https://github.com/unjs/hookable/commit/f8fb74224f1277ec7f7d5a37bd312af7514fc962))
97
- * allow removing registered hooks ([#16](https://github.com/unjs/hookable/issues/16)) ([4134c31](https://github.com/unjs/hookable/commit/4134c31c44256cc82cac3a7a3610ece9252431dc))
98
- * migrate to typescript ([d63ea3e](https://github.com/unjs/hookable/commit/d63ea3e408ebea74ea3855af0c6e51880ebf9cac))
99
-
100
- ## [3.0.0](https://github.com/unjs/hookable/compare/v2.3.0...v3.0.0) (2020-02-25)
101
-
102
-
103
- ### Bug Fixes
104
-
105
- * revert back hooks ([07f52dc](https://github.com/unjs/hookable/commit/07f52dc))
106
-
107
-
108
- ### Features
109
-
110
- * advanced deprecation ([5b88628](https://github.com/unjs/hookable/commit/5b88628))
111
- * bundle package ([53a2a0e](https://github.com/unjs/hookable/commit/53a2a0e))
112
-
113
- # [2.3.0](https://github.com/unjs/hookable/compare/v2.2.1...v2.3.0) (2019-09-01)
114
-
115
-
116
- ### Features
117
-
118
- * hide deprecate warnings on production builds ([0861df3](https://github.com/unjs/hookable/commit/0861df3))
119
-
120
-
121
-
122
- ## [2.2.1](https://github.com/unjs/hookable/compare/v2.2.0...v2.2.1) (2019-08-21)
123
-
124
-
125
-
126
- # [2.2.0](https://github.com/unjs/hookable/compare/v2.1.0...v2.2.0) (2019-08-21)
127
-
128
-
129
- ### Features
130
-
131
- * deprecateHooks ([62f2d38](https://github.com/unjs/hookable/commit/62f2d38))
132
-
133
-
134
-
135
- # [2.1.0](https://github.com/unjs/hookable/compare/v2.0.1...v2.1.0) (2019-08-21)
136
-
137
-
138
- ### Features
139
-
140
- * optional fatal support for logger ([7c7355d](https://github.com/unjs/hookable/commit/7c7355d))
141
-
142
-
143
-
144
- ## [2.0.1](https://github.com/unjs/hookable/compare/v2.0.0...v2.0.1) (2019-08-21)
145
-
146
-
147
-
148
- # [2.0.0](https://github.com/unjs/hookable/compare/v1.0.1...v2.0.0) (2019-08-21)
149
-
150
-
151
- ### Features
152
-
153
- * custom logger ([ada6e37](https://github.com/unjs/hookable/commit/ada6e37))
154
-
155
-
156
- ### BREAKING CHANGES
157
-
158
- * console is replaced by consola by default
159
-
160
-
161
-
162
- ## [1.0.1](https://github.com/unjs/hookable/compare/v1.0.0...v1.0.1) (2019-03-16)
163
-
164
-
165
- ### Bug Fixes
166
-
167
- * fix package.json (2) ([7ff4ce9](https://github.com/unjs/hookable/commit/7ff4ce9))
168
-
169
-
170
-
171
- <a name="1.0.0"></a>
172
- # [1.0.0](https://github.com/unjs/hookable/compare/v0.0.7...v1.0.0) (2019-02-11)
173
-
174
-
175
- ### Features
176
-
177
- * rewrite for 1.0.0 ([88decae](https://github.com/unjs/hookable/commit/88decae))
178
-
179
-
180
- ### BREAKING CHANGES
181
-
182
- * api change
183
-
184
-
185
-
186
- <a name="0.0.7"></a>
187
- ## [0.0.7](https://github.com/pi0/hookable/compare/v0.0.6...v0.0.7) (2018-01-28)
188
-
189
-
190
- ### Bug Fixes
191
-
192
- * hook with array or falsy key ([7e90de1](https://github.com/pi0/hookable/commit/7e90de1))
193
-
194
-
195
- ### Performance Improvements
196
-
197
- * use for in for hookObj ([3c8e2e7](https://github.com/pi0/hookable/commit/3c8e2e7))
198
-
199
-
200
-
201
- <a name="0.0.6"></a>
202
- ## [0.0.6](https://github.com/pi0/hookable/compare/v0.0.5...v0.0.6) (2018-01-26)
203
-
204
-
205
- ### Performance Improvements
206
-
207
- * reduce transpiled dist size ([df607cf](https://github.com/pi0/hookable/commit/df607cf))
208
-
209
-
210
-
211
- <a name="0.0.5"></a>
212
- ## [0.0.5](https://github.com/pi0/hookable/compare/v0.0.4...v0.0.5) (2018-01-26)
213
-
214
-
215
- ### Bug Fixes
216
-
217
- * **package:** lib ~> dist ([34a8d5c](https://github.com/pi0/hookable/commit/34a8d5c))
218
-
219
-
220
-
221
- <a name="0.0.4"></a>
222
- ## [0.0.4](https://github.com/pi0/hookable/compare/v0.0.3...v0.0.4) (2018-01-26)
223
-
224
-
225
- ### Performance Improvements
226
-
227
- * handle fn as array faster ([ec35edc](https://github.com/pi0/hookable/commit/ec35edc))
228
-
229
-
230
-
231
- <a name="0.0.3"></a>
232
- ## [0.0.3](https://github.com/pi0/hookable/compare/v0.0.2...v0.0.3) (2018-01-26)
233
-
234
-
235
- ### Bug Fixes
236
-
237
- * bind hookObj to this context ([6f6f7bc](https://github.com/pi0/hookable/commit/6f6f7bc))
238
-
239
-
240
- ### Performance Improvements
241
-
242
- * minor refactor ([e4083aa](https://github.com/pi0/hookable/commit/e4083aa))
243
-
244
-
245
-
246
- <a name="0.0.2"></a>
247
- ## 0.0.2 (2018-01-26)
@@ -1,43 +0,0 @@
1
- declare type HookCallback = (...args: any) => Promise<void> | void;
2
- interface Hooks {
3
- [key: string]: HookCallback;
4
- }
5
- declare type HookKeys<T> = keyof T & string;
6
- declare type NestedHooks<T> = {
7
- [name in HookKeys<T>]: NestedHooks<T> | HookCallback;
8
- };
9
- declare type DeprecatedHook<T> = string | {
10
- message: string;
11
- to: HookKeys<T>;
12
- };
13
- declare type DeprecatedHooks<T> = {
14
- [name in HookKeys<T>]: DeprecatedHook<T>;
15
- };
16
- interface LoggerT {
17
- error(...args: any): void;
18
- fatal?(...args: any): void;
19
- warn(...args: any): void;
20
- }
21
-
22
- declare function mergeHooks<T>(...hooks: NestedHooks<T>[]): T;
23
-
24
- declare class Hookable<_HooksT = Record<string, HookCallback>, HooksT = _HooksT & {
25
- error: (error: Error | any) => void;
26
- }, HookNameT extends HookKeys<HooksT> = HookKeys<HooksT>> {
27
- private _hooks;
28
- private _deprecatedHooks;
29
- private _logger;
30
- static mergeHooks: typeof mergeHooks;
31
- mergeHooks: typeof mergeHooks;
32
- constructor(logger?: LoggerT | false);
33
- hook<NameT extends HookNameT>(name: NameT, fn: HooksT[NameT] & HookCallback): () => void;
34
- hookOnce<NameT extends HookNameT>(name: NameT, fn: HooksT[NameT] & HookCallback): any;
35
- removeHook<NameT extends HookNameT>(name: NameT, fn: HooksT[NameT] & HookCallback): void;
36
- deprecateHook<NameT extends HookNameT>(name: NameT, deprecated: DeprecatedHook<HooksT>): void;
37
- deprecateHooks(deprecatedHooks: Record<HookNameT, DeprecatedHook<HooksT>>): void;
38
- addHooks(configHooks: NestedHooks<HooksT>): () => void;
39
- removeHooks(configHooks: NestedHooks<HooksT>): void;
40
- callHook<NameT extends HookNameT>(name: NameT, ...args: Parameters<HooksT[NameT]>): Promise<void>;
41
- }
42
-
43
- export { DeprecatedHook, DeprecatedHooks, HookCallback, HookKeys, Hooks, LoggerT, NestedHooks, Hookable as default };
package/types/index.d.ts DELETED
@@ -1,36 +0,0 @@
1
- declare type HookCallback = (...args: any) => Promise<void> | void;
2
- interface Hooks {
3
- [key: string]: HookCallback;
4
- }
5
- declare type HookKeys<T> = keyof T & string;
6
- declare type NestedHooks<T> = {
7
- [name in HookKeys<T>]: NestedHooks<T> | HookCallback;
8
- };
9
- declare type DeprecatedHook<T> = string | {
10
- message: string;
11
- to: HookKeys<T>;
12
- };
13
- declare type DeprecatedHooks<T> = {
14
- [name in HookKeys<T>]: DeprecatedHook<T>;
15
- };
16
-
17
- declare class Hookable<HooksT = Record<string, HookCallback>, HookNameT extends HookKeys<HooksT> = HookKeys<HooksT>> {
18
- private _hooks;
19
- private _deprecatedHooks;
20
- constructor();
21
- hook<NameT extends HookNameT>(name: NameT, fn: HooksT[NameT] & HookCallback): () => void;
22
- hookOnce<NameT extends HookNameT>(name: NameT, fn: HooksT[NameT] & HookCallback): any;
23
- removeHook<NameT extends HookNameT>(name: NameT, fn: HooksT[NameT] & HookCallback): void;
24
- deprecateHook<NameT extends HookNameT>(name: NameT, deprecated: DeprecatedHook<HooksT>): void;
25
- deprecateHooks(deprecatedHooks: Record<HookNameT, DeprecatedHook<HooksT>>): void;
26
- addHooks(configHooks: NestedHooks<HooksT>): () => void;
27
- removeHooks(configHooks: NestedHooks<HooksT>): void;
28
- callHook<NameT extends HookNameT>(name: NameT, ...args: Parameters<HooksT[NameT]>): Promise<any>;
29
- }
30
- declare function createHooks<T>(): Hookable<T>;
31
-
32
- declare function flatHooks<T>(configHooks: NestedHooks<T>, hooks?: T, parentName?: string): T;
33
- declare function mergeHooks<T>(...hooks: NestedHooks<T>[]): T;
34
- declare function serial<T>(tasks: T[], fn: (task: T) => Promise<any> | any): Promise<any>;
35
-
36
- export { DeprecatedHook, DeprecatedHooks, HookCallback, HookKeys, Hookable, Hooks, NestedHooks, createHooks, flatHooks, mergeHooks, serial };