Skip to content

Commit 0bf8fdd

Browse files
committed
fix(core): only pass args
1 parent 60729df commit 0bf8fdd

File tree

4 files changed

+20
-32
lines changed

4 files changed

+20
-32
lines changed

src/hookable.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
parallelTaskCaller,
44
serialTaskCaller,
55
callEachWith,
6-
nextDispatch,
6+
callHooks,
77
} from "./utils.ts";
88

99
import type {
@@ -326,13 +326,6 @@ export class HookableCore<
326326
if (!hooks || hooks.length === 0) {
327327
return;
328328
}
329-
return nextDispatch(
330-
hooks,
331-
(args?.length ? [name, ...args] : [name]) as Parameters<
332-
InferCallback<HooksT, NameT>
333-
>,
334-
{ run: (fn) => fn() },
335-
0,
336-
);
329+
return callHooks(hooks, args, 0);
337330
}
338331
}

src/utils.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,19 @@ const createTask = /* @__PURE__ */ (() => {
7777
return () => defaultTask;
7878
})();
7979

80-
export function nextDispatch(
80+
export function callHooks(
8181
hooks: HookCallback[],
8282
args: any[],
83-
task: ReturnType<CreateTask>,
8483
startIndex: number,
84+
task?: ReturnType<CreateTask>,
8585
): Promise<any> | void {
8686
for (let i = startIndex; i < hooks.length; i += 1) {
8787
try {
88-
const result = task.run(() => hooks[i](...args));
89-
88+
const result = task
89+
? task.run(() => hooks[i](...args))
90+
: hooks[i](...args);
9091
if (result instanceof Promise) {
91-
return result.then(() => nextDispatch(hooks, args, task, i + 1));
92+
return result.then(() => callHooks(hooks, args, i + 1, task));
9293
}
9394
} catch (error) {
9495
return Promise.reject(error);
@@ -100,25 +101,19 @@ export function serialTaskCaller(
100101
hooks: HookCallback[],
101102
args: any[],
102103
): Promise<any> | void {
103-
if (hooks.length === 0) {
104-
return;
104+
if (hooks.length > 0) {
105+
return callHooks(hooks, args, 0, createTask(args.shift()));
105106
}
106-
107-
const task = createTask(args.shift());
108-
109-
return nextDispatch(hooks, args, task, 0);
110107
}
111108

112109
export function parallelTaskCaller(
113110
hooks: HookCallback[],
114111
args: any[],
115112
): Promise<any> | void {
116-
if (hooks.length === 0) {
117-
return;
113+
if (hooks.length > 0) {
114+
const task = createTask(args.shift());
115+
return Promise.all(hooks.map((hook) => task.run(() => hook(...args))));
118116
}
119-
120-
const task = createTask(args.shift());
121-
return Promise.all(hooks.map((hook) => task.run(() => hook(...args))));
122117
}
123118

124119
/** @deprecated */

test/bundle.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ describe("benchmark", () => {
2626
if (process.env.DEBUG) {
2727
console.log("new HookableCore():", { bytes, gzipSize });
2828
}
29-
expect(bytes).toBeLessThan(640);
30-
expect(gzipSize).toBeLessThan(380);
29+
expect(bytes).toBeLessThan(630);
30+
expect(gzipSize).toBeLessThan(370);
3131
});
3232
});
3333

test/hookable.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import { Hookable, HookableCore, flatHooks, mergeHooks } from "../src/index.ts";
55
describe("HookableCore", () => {
66
test("basic functionality", async () => {
77
const hookable = new HookableCore<{ test(): void }>();
8-
let called = false;
9-
hookable.hook("test", async () => {
10-
called = true;
8+
hookable.hook("test", async (obj: { called: boolean }) => {
9+
obj.called = true;
1110
});
12-
await hookable.callHook("test");
13-
expect(called).toBe(true);
11+
const obj = { called: false };
12+
await hookable.callHook("test", obj);
13+
expect(obj.called).toBe(true);
1414
});
1515
});
1616

0 commit comments

Comments
 (0)