Skip to content

Commit fc96bce

Browse files
pi0danielroe
andcommitted
perf: pass name arg to caller seperately
Co-authored-by: Daniel Roe <daniel@roe.dev>
1 parent e9f846f commit fc96bce

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,11 @@ Used by class itself to **sequentially** call handlers of a specific hook.
163163

164164
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.
165165

166-
`callerFn` if a callback function that accepts two arguments, `hooks` and `args`:
166+
`callerFn` if a callback function that accepts 3 arguments, `hooks`, `args` and `name`:
167167

168168
- `hooks`: Array of user hooks to be called
169169
- `args`: Array of arguments that should be passed each time calling a hook
170+
- `name`: Name of the hook
170171

171172
### `deprecateHook (old, name)`
172173

src/hookable.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ export class Hookable<
210210
CallFunction extends (
211211
hooks: HookCallback[],
212212
args: Parameters<InferCallback<HooksT, NameT>>,
213+
name: NameT,
213214
) => any,
214215
>(
215216
caller: CallFunction,
@@ -221,10 +222,10 @@ export class Hookable<
221222
if (this._before) {
222223
callEachWith(this._before, event);
223224
}
224-
const _args = args?.length ? [name, ...args] : [name];
225225
const result = caller(
226226
this._hooks[name] ? [...this._hooks[name]] : [],
227-
_args as Parameters<InferCallback<HooksT, NameT>>,
227+
args as Parameters<InferCallback<HooksT, NameT>>,
228+
name,
228229
);
229230
if ((result as any) instanceof Promise) {
230231
return result.finally(() => {

src/utils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,20 @@ export function callHooks(
100100
export function serialTaskCaller(
101101
hooks: HookCallback[],
102102
args: any[],
103+
name: string,
103104
): Promise<any> | void {
104105
if (hooks.length > 0) {
105-
return callHooks(hooks, args, 0, createTask(args.shift()));
106+
return callHooks(hooks, args, 0, createTask(name));
106107
}
107108
}
108109

109110
export function parallelTaskCaller(
110111
hooks: HookCallback[],
111112
args: any[],
113+
name: string,
112114
): Promise<any> | void {
113115
if (hooks.length > 0) {
114-
const task = createTask(args.shift());
116+
const task = createTask(name);
115117
return Promise.all(hooks.map((hook) => task.run(() => hook(...args))));
116118
}
117119
}

0 commit comments

Comments
 (0)