hookable 5.4.1 → 5.4.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/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2018 Pooya Parsa <pooya@pi0.ir>
3
+ Copyright (c) Pooya Parsa <pooya@pi0.io>
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/dist/index.cjs CHANGED
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
3
  function flatHooks(configHooks, hooks = {}, parentName) {
6
4
  for (const key in configHooks) {
7
5
  const subHook = configHooks[key];
@@ -28,42 +26,42 @@ function mergeHooks(...hooks) {
28
26
  }
29
27
  for (const key in finalHooks) {
30
28
  if (finalHooks[key].length > 1) {
31
- const arr = finalHooks[key];
32
- finalHooks[key] = (...args) => serial(arr, (fn) => fn(...args));
29
+ const array = finalHooks[key];
30
+ finalHooks[key] = (...arguments_) => serial(array, (function_) => function_(...arguments_));
33
31
  } else {
34
32
  finalHooks[key] = finalHooks[key][0];
35
33
  }
36
34
  }
37
35
  return finalHooks;
38
36
  }
39
- function serial(tasks, fn) {
40
- return tasks.reduce((promise, task) => promise.then(() => fn(task)), Promise.resolve(null));
37
+ function serial(tasks, function_) {
38
+ return tasks.reduce((promise, task) => promise.then(() => function_(task)), Promise.resolve());
41
39
  }
42
- function serialCaller(hooks, args) {
43
- return hooks.reduce((promise, hookFn) => promise.then(() => hookFn.apply(void 0, args)), Promise.resolve(null));
40
+ function serialCaller(hooks, arguments_) {
41
+ return hooks.reduce((promise, hookFunction) => promise.then(() => hookFunction.apply(void 0, arguments_)), Promise.resolve());
44
42
  }
45
- function parallelCaller(hooks, args) {
46
- return Promise.all(hooks.map((hook) => hook.apply(void 0, args)));
43
+ function parallelCaller(hooks, arguments_) {
44
+ return Promise.all(hooks.map((hook) => hook.apply(void 0, arguments_)));
47
45
  }
48
- function callEachWith(callbacks, arg0) {
49
- for (const cb of callbacks) {
50
- cb(arg0);
46
+ function callEachWith(callbacks, argument0) {
47
+ for (const callback of callbacks) {
48
+ callback(argument0);
51
49
  }
52
50
  }
53
51
 
54
52
  class Hookable {
55
53
  constructor() {
56
54
  this._hooks = {};
57
- this._before = null;
58
- this._after = null;
59
- this._deprecatedMessages = null;
55
+ this._before = void 0;
56
+ this._after = void 0;
57
+ this._deprecatedMessages = void 0;
60
58
  this._deprecatedHooks = {};
61
59
  this.hook = this.hook.bind(this);
62
60
  this.callHook = this.callHook.bind(this);
63
61
  this.callHookWith = this.callHookWith.bind(this);
64
62
  }
65
- hook(name, fn, opts = {}) {
66
- if (!name || typeof fn !== "function") {
63
+ hook(name, function_, options = {}) {
64
+ if (!name || typeof function_ !== "function") {
67
65
  return () => {
68
66
  };
69
67
  }
@@ -73,7 +71,7 @@ class Hookable {
73
71
  dep = this._deprecatedHooks[name];
74
72
  name = dep.to;
75
73
  }
76
- if (dep && !opts.allowDeprecated) {
74
+ if (dep && !options.allowDeprecated) {
77
75
  let message = dep.message;
78
76
  if (!message) {
79
77
  message = `${originalName} hook has been deprecated` + (dep.to ? `, please use ${dep.to}` : "");
@@ -87,30 +85,32 @@ class Hookable {
87
85
  }
88
86
  }
89
87
  this._hooks[name] = this._hooks[name] || [];
90
- this._hooks[name].push(fn);
88
+ this._hooks[name].push(function_);
91
89
  return () => {
92
- if (fn) {
93
- this.removeHook(name, fn);
94
- fn = null;
90
+ if (function_) {
91
+ this.removeHook(name, function_);
92
+ function_ = void 0;
95
93
  }
96
94
  };
97
95
  }
98
- hookOnce(name, fn) {
96
+ hookOnce(name, function_) {
99
97
  let _unreg;
100
- let _fn = (...args) => {
101
- _unreg();
102
- _unreg = null;
103
- _fn = null;
104
- return fn(...args);
98
+ let _function = (...arguments_) => {
99
+ if (typeof _unreg === "function") {
100
+ _unreg();
101
+ }
102
+ _unreg = void 0;
103
+ _function = void 0;
104
+ return function_(...arguments_);
105
105
  };
106
- _unreg = this.hook(name, _fn);
106
+ _unreg = this.hook(name, _function);
107
107
  return _unreg;
108
108
  }
109
- removeHook(name, fn) {
109
+ removeHook(name, function_) {
110
110
  if (this._hooks[name]) {
111
- const idx = this._hooks[name].indexOf(fn);
112
- if (idx !== -1) {
113
- this._hooks[name].splice(idx, 1);
111
+ const index = this._hooks[name].indexOf(function_);
112
+ if (index !== -1) {
113
+ this._hooks[name].splice(index, 1);
114
114
  }
115
115
  if (this._hooks[name].length === 0) {
116
116
  delete this._hooks[name];
@@ -135,7 +135,9 @@ class Hookable {
135
135
  const hooks = flatHooks(configHooks);
136
136
  const removeFns = Object.keys(hooks).map((key) => this.hook(key, hooks[key]));
137
137
  return () => {
138
- removeFns.splice(0, removeFns.length).forEach((unreg) => unreg());
138
+ for (const unreg of removeFns.splice(0, removeFns.length)) {
139
+ unreg();
140
+ }
139
141
  };
140
142
  }
141
143
  removeHooks(configHooks) {
@@ -144,18 +146,18 @@ class Hookable {
144
146
  this.removeHook(key, hooks[key]);
145
147
  }
146
148
  }
147
- callHook(name, ...args) {
148
- return this.callHookWith(serialCaller, name, ...args);
149
+ callHook(name, ...arguments_) {
150
+ return this.callHookWith(serialCaller, name, ...arguments_);
149
151
  }
150
- callHookParallel(name, ...args) {
151
- return this.callHookWith(parallelCaller, name, ...args);
152
+ callHookParallel(name, ...arguments_) {
153
+ return this.callHookWith(parallelCaller, name, ...arguments_);
152
154
  }
153
- callHookWith(caller, name, ...args) {
154
- const event = this._before || this._after ? { name, args, context: {} } : void 0;
155
+ callHookWith(caller, name, ...arguments_) {
156
+ const event = this._before || this._after ? { name, args: arguments_, context: {} } : void 0;
155
157
  if (this._before) {
156
158
  callEachWith(this._before, event);
157
159
  }
158
- const result = caller(this._hooks[name] || [], args);
160
+ const result = caller(this._hooks[name] || [], arguments_);
159
161
  if (result instanceof Promise) {
160
162
  return result.finally(() => {
161
163
  if (this._after && event) {
@@ -168,21 +170,21 @@ class Hookable {
168
170
  }
169
171
  return result;
170
172
  }
171
- beforeEach(fn) {
173
+ beforeEach(function_) {
172
174
  this._before = this._before || [];
173
- this._before.push(fn);
175
+ this._before.push(function_);
174
176
  return () => {
175
- const index = this._before.indexOf(fn);
177
+ const index = this._before.indexOf(function_);
176
178
  if (index !== -1) {
177
179
  this._before.splice(index, 1);
178
180
  }
179
181
  };
180
182
  }
181
- afterEach(fn) {
183
+ afterEach(function_) {
182
184
  this._after = this._after || [];
183
- this._after.push(fn);
185
+ this._after.push(function_);
184
186
  return () => {
185
- const index = this._after.indexOf(fn);
187
+ const index = this._after.indexOf(function_);
186
188
  if (index !== -1) {
187
189
  this._after.splice(index, 1);
188
190
  }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- declare type HookCallback = (...args: any) => Promise<void> | void;
1
+ declare type HookCallback = (...arguments_: any) => Promise<void> | void;
2
2
  interface Hooks {
3
3
  [key: string]: HookCallback;
4
4
  }
@@ -50,28 +50,28 @@ declare class Hookable<HooksT = Record<string, HookCallback>, HookNameT extends
50
50
  private _deprecatedHooks;
51
51
  private _deprecatedMessages;
52
52
  constructor();
53
- hook<NameT extends HookNameT>(name: NameT, fn: InferCallback<HooksT, NameT>, opts?: {
53
+ hook<NameT extends HookNameT>(name: NameT, function_: InferCallback<HooksT, NameT>, options?: {
54
54
  allowDeprecated?: boolean;
55
55
  }): () => void;
56
- hookOnce<NameT extends HookNameT>(name: NameT, fn: InferCallback<HooksT, NameT>): () => void;
57
- removeHook<NameT extends HookNameT>(name: NameT, fn: InferCallback<HooksT, NameT>): void;
56
+ hookOnce<NameT extends HookNameT>(name: NameT, function_: InferCallback<HooksT, NameT>): () => void;
57
+ removeHook<NameT extends HookNameT>(name: NameT, function_: InferCallback<HooksT, NameT>): void;
58
58
  deprecateHook<NameT extends HookNameT>(name: NameT, deprecated: HookKeys<HooksT> | DeprecatedHook<HooksT>): void;
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
- callHook<NameT extends HookNameT>(name: NameT, ...args: Parameters<InferCallback<HooksT, NameT>>): Promise<any>;
63
- callHookParallel<NameT extends HookNameT>(name: NameT, ...args: Parameters<InferCallback<HooksT, NameT>>): Promise<any[]>;
64
- callHookWith<NameT extends HookNameT, CallFunction extends (hooks: HookCallback[], args: Parameters<InferCallback<HooksT, NameT>>) => any>(caller: CallFunction, name: NameT, ...args: Parameters<InferCallback<HooksT, NameT>>): ReturnType<CallFunction>;
65
- beforeEach(fn: (event: InferSpyEvent<HooksT>) => void): () => void;
66
- afterEach(fn: (event: InferSpyEvent<HooksT>) => void): () => void;
62
+ callHook<NameT extends HookNameT>(name: NameT, ...arguments_: Parameters<InferCallback<HooksT, NameT>>): Promise<any>;
63
+ callHookParallel<NameT extends HookNameT>(name: NameT, ...arguments_: Parameters<InferCallback<HooksT, NameT>>): Promise<any[]>;
64
+ 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>;
65
+ beforeEach(function_: (event: InferSpyEvent<HooksT>) => void): () => void;
66
+ afterEach(function_: (event: InferSpyEvent<HooksT>) => void): () => void;
67
67
  }
68
68
  declare function createHooks<T>(): Hookable<T>;
69
69
 
70
70
  declare function flatHooks<T>(configHooks: NestedHooks<T>, hooks?: T, parentName?: string): T;
71
71
  declare function mergeHooks<T>(...hooks: NestedHooks<T>[]): T;
72
- declare function serial<T>(tasks: T[], fn: (task: T) => Promise<any> | any): Promise<any>;
73
- declare function serialCaller(hooks: HookCallback[], args?: any[]): Promise<any>;
74
- declare function parallelCaller(hooks: HookCallback[], args?: any[]): Promise<any[]>;
72
+ 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[]>;
75
75
 
76
76
  interface CreateDebuggerOptions {
77
77
  /** An optional tag to prefix console logs with */
package/dist/index.mjs CHANGED
@@ -24,42 +24,42 @@ function mergeHooks(...hooks) {
24
24
  }
25
25
  for (const key in finalHooks) {
26
26
  if (finalHooks[key].length > 1) {
27
- const arr = finalHooks[key];
28
- finalHooks[key] = (...args) => serial(arr, (fn) => fn(...args));
27
+ const array = finalHooks[key];
28
+ finalHooks[key] = (...arguments_) => serial(array, (function_) => function_(...arguments_));
29
29
  } else {
30
30
  finalHooks[key] = finalHooks[key][0];
31
31
  }
32
32
  }
33
33
  return finalHooks;
34
34
  }
35
- function serial(tasks, fn) {
36
- return tasks.reduce((promise, task) => promise.then(() => fn(task)), Promise.resolve(null));
35
+ function serial(tasks, function_) {
36
+ return tasks.reduce((promise, task) => promise.then(() => function_(task)), Promise.resolve());
37
37
  }
38
- function serialCaller(hooks, args) {
39
- return hooks.reduce((promise, hookFn) => promise.then(() => hookFn.apply(void 0, args)), Promise.resolve(null));
38
+ function serialCaller(hooks, arguments_) {
39
+ return hooks.reduce((promise, hookFunction) => promise.then(() => hookFunction.apply(void 0, arguments_)), Promise.resolve());
40
40
  }
41
- function parallelCaller(hooks, args) {
42
- return Promise.all(hooks.map((hook) => hook.apply(void 0, args)));
41
+ function parallelCaller(hooks, arguments_) {
42
+ return Promise.all(hooks.map((hook) => hook.apply(void 0, arguments_)));
43
43
  }
44
- function callEachWith(callbacks, arg0) {
45
- for (const cb of callbacks) {
46
- cb(arg0);
44
+ function callEachWith(callbacks, argument0) {
45
+ for (const callback of callbacks) {
46
+ callback(argument0);
47
47
  }
48
48
  }
49
49
 
50
50
  class Hookable {
51
51
  constructor() {
52
52
  this._hooks = {};
53
- this._before = null;
54
- this._after = null;
55
- this._deprecatedMessages = null;
53
+ this._before = void 0;
54
+ this._after = void 0;
55
+ this._deprecatedMessages = void 0;
56
56
  this._deprecatedHooks = {};
57
57
  this.hook = this.hook.bind(this);
58
58
  this.callHook = this.callHook.bind(this);
59
59
  this.callHookWith = this.callHookWith.bind(this);
60
60
  }
61
- hook(name, fn, opts = {}) {
62
- if (!name || typeof fn !== "function") {
61
+ hook(name, function_, options = {}) {
62
+ if (!name || typeof function_ !== "function") {
63
63
  return () => {
64
64
  };
65
65
  }
@@ -69,7 +69,7 @@ class Hookable {
69
69
  dep = this._deprecatedHooks[name];
70
70
  name = dep.to;
71
71
  }
72
- if (dep && !opts.allowDeprecated) {
72
+ if (dep && !options.allowDeprecated) {
73
73
  let message = dep.message;
74
74
  if (!message) {
75
75
  message = `${originalName} hook has been deprecated` + (dep.to ? `, please use ${dep.to}` : "");
@@ -83,30 +83,32 @@ class Hookable {
83
83
  }
84
84
  }
85
85
  this._hooks[name] = this._hooks[name] || [];
86
- this._hooks[name].push(fn);
86
+ this._hooks[name].push(function_);
87
87
  return () => {
88
- if (fn) {
89
- this.removeHook(name, fn);
90
- fn = null;
88
+ if (function_) {
89
+ this.removeHook(name, function_);
90
+ function_ = void 0;
91
91
  }
92
92
  };
93
93
  }
94
- hookOnce(name, fn) {
94
+ hookOnce(name, function_) {
95
95
  let _unreg;
96
- let _fn = (...args) => {
97
- _unreg();
98
- _unreg = null;
99
- _fn = null;
100
- return fn(...args);
96
+ let _function = (...arguments_) => {
97
+ if (typeof _unreg === "function") {
98
+ _unreg();
99
+ }
100
+ _unreg = void 0;
101
+ _function = void 0;
102
+ return function_(...arguments_);
101
103
  };
102
- _unreg = this.hook(name, _fn);
104
+ _unreg = this.hook(name, _function);
103
105
  return _unreg;
104
106
  }
105
- removeHook(name, fn) {
107
+ removeHook(name, function_) {
106
108
  if (this._hooks[name]) {
107
- const idx = this._hooks[name].indexOf(fn);
108
- if (idx !== -1) {
109
- this._hooks[name].splice(idx, 1);
109
+ const index = this._hooks[name].indexOf(function_);
110
+ if (index !== -1) {
111
+ this._hooks[name].splice(index, 1);
110
112
  }
111
113
  if (this._hooks[name].length === 0) {
112
114
  delete this._hooks[name];
@@ -131,7 +133,9 @@ class Hookable {
131
133
  const hooks = flatHooks(configHooks);
132
134
  const removeFns = Object.keys(hooks).map((key) => this.hook(key, hooks[key]));
133
135
  return () => {
134
- removeFns.splice(0, removeFns.length).forEach((unreg) => unreg());
136
+ for (const unreg of removeFns.splice(0, removeFns.length)) {
137
+ unreg();
138
+ }
135
139
  };
136
140
  }
137
141
  removeHooks(configHooks) {
@@ -140,18 +144,18 @@ class Hookable {
140
144
  this.removeHook(key, hooks[key]);
141
145
  }
142
146
  }
143
- callHook(name, ...args) {
144
- return this.callHookWith(serialCaller, name, ...args);
147
+ callHook(name, ...arguments_) {
148
+ return this.callHookWith(serialCaller, name, ...arguments_);
145
149
  }
146
- callHookParallel(name, ...args) {
147
- return this.callHookWith(parallelCaller, name, ...args);
150
+ callHookParallel(name, ...arguments_) {
151
+ return this.callHookWith(parallelCaller, name, ...arguments_);
148
152
  }
149
- callHookWith(caller, name, ...args) {
150
- const event = this._before || this._after ? { name, args, context: {} } : void 0;
153
+ callHookWith(caller, name, ...arguments_) {
154
+ const event = this._before || this._after ? { name, args: arguments_, context: {} } : void 0;
151
155
  if (this._before) {
152
156
  callEachWith(this._before, event);
153
157
  }
154
- const result = caller(this._hooks[name] || [], args);
158
+ const result = caller(this._hooks[name] || [], arguments_);
155
159
  if (result instanceof Promise) {
156
160
  return result.finally(() => {
157
161
  if (this._after && event) {
@@ -164,21 +168,21 @@ class Hookable {
164
168
  }
165
169
  return result;
166
170
  }
167
- beforeEach(fn) {
171
+ beforeEach(function_) {
168
172
  this._before = this._before || [];
169
- this._before.push(fn);
173
+ this._before.push(function_);
170
174
  return () => {
171
- const index = this._before.indexOf(fn);
175
+ const index = this._before.indexOf(function_);
172
176
  if (index !== -1) {
173
177
  this._before.splice(index, 1);
174
178
  }
175
179
  };
176
180
  }
177
- afterEach(fn) {
181
+ afterEach(function_) {
178
182
  this._after = this._after || [];
179
- this._after.push(fn);
183
+ this._after.push(function_);
180
184
  return () => {
181
- const index = this._after.indexOf(fn);
185
+ const index = this._after.indexOf(function_);
182
186
  if (index !== -1) {
183
187
  this._after.splice(index, 1);
184
188
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hookable",
3
- "version": "5.4.1",
3
+ "version": "5.4.2",
4
4
  "description": "Awaitable hook system",
5
5
  "keywords": [
6
6
  "hook",
@@ -13,6 +13,7 @@
13
13
  "license": "MIT",
14
14
  "exports": {
15
15
  "import": "./dist/index.mjs",
16
+ "types": "./dist/index.d.ts",
16
17
  "require": "./dist/index.cjs"
17
18
  },
18
19
  "main": "./dist/index.cjs",
@@ -22,16 +23,16 @@
22
23
  "dist"
23
24
  ],
24
25
  "devDependencies": {
25
- "@nuxtjs/eslint-config-typescript": "^11.0.0",
26
- "@vitest/coverage-c8": "^0.24.3",
27
- "eslint": "^8.25.0",
28
- "expect-type": "^0.14.2",
26
+ "@vitest/coverage-c8": "^0.25.2",
27
+ "eslint": "^8.27.0",
28
+ "eslint-config-unjs": "^0.0.2",
29
+ "expect-type": "^0.15.0",
29
30
  "standard-version": "^9.5.0",
30
- "typescript": "latest",
31
- "unbuild": "^0.8.2",
32
- "vitest": "^0.24.3"
31
+ "typescript": "^4.8.4",
32
+ "unbuild": "^0.9.4",
33
+ "vitest": "^0.25.2"
33
34
  },
34
- "packageManager": "pnpm@7.13.4",
35
+ "packageManager": "pnpm@7.16.0",
35
36
  "scripts": {
36
37
  "build": "unbuild",
37
38
  "dev": "vitest",