Skip to content

Commit 8b21f33

Browse files
committed
feat: multiHook support different types of hooks
1 parent f54ebea commit 8b21f33

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

lib/MultiHook.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,40 @@ class MultiHook {
1212

1313
tap(options, fn) {
1414
for (const hook of this.hooks) {
15-
hook.tap(options, fn);
15+
if (hook.tap) hook.tap(options, fn);
1616
}
1717
}
1818

1919
tapAsync(options, fn) {
2020
for (const hook of this.hooks) {
21-
hook.tapAsync(options, fn);
21+
if (hook.tapAsync) hook.tapAsync(options, fn);
2222
}
2323
}
2424

2525
tapPromise(options, fn) {
2626
for (const hook of this.hooks) {
27-
hook.tapPromise(options, fn);
27+
if (hook.tapPromise) hook.tapPromise(options, fn);
2828
}
2929
}
3030

3131
isUsed() {
3232
for (const hook of this.hooks) {
33-
if (hook.isUsed()) return true;
33+
if (hook.isUsed && hook.isUsed()) return true;
3434
}
3535
return false;
3636
}
3737

3838
intercept(interceptor) {
3939
for (const hook of this.hooks) {
40-
hook.intercept(interceptor);
40+
if (hook.intercept) hook.intercept(interceptor);
4141
}
4242
}
4343

4444
withOptions(options) {
4545
return new MultiHook(
46-
this.hooks.map((hook) => hook.withOptions(options)),
46+
this.hooks.map((hook) =>
47+
hook.withOptions ? hook.withOptions(options) : hook
48+
),
4749
this.name
4850
);
4951
}

lib/__tests__/MultiHook.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,27 @@ describe("MultiHook", () => {
6969
expect(new MultiHook([fakeHook2, fakeHook1]).isUsed()).toBe(true);
7070
expect(new MultiHook([fakeHook2, fakeHook2]).isUsed()).toBe(false);
7171
});
72+
73+
it("should support different types of hooks", () => {
74+
const calls = [];
75+
const fakeHook1 = {
76+
tap: (options, fn) => {
77+
calls.push({ options, fn });
78+
}
79+
};
80+
const fakeHook2 = {
81+
tapPromise: (options, fn) => {
82+
calls.push({ options, fn });
83+
}
84+
};
85+
const hook = new MultiHook([fakeHook1, fakeHook1, fakeHook2, fakeHook2]);
86+
hook.tap("options", "fn1");
87+
hook.tapPromise("options", "fn2");
88+
expect(calls).toEqual([
89+
{ options: "options", fn: "fn1" },
90+
{ options: "options", fn: "fn1" },
91+
{ options: "options", fn: "fn2" },
92+
{ options: "options", fn: "fn2" }
93+
]);
94+
});
7295
});

0 commit comments

Comments
 (0)