Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,11 @@ For sync hooks, `tap` is the only valid method to add a plugin. Async hooks also
```js
myCar.hooks.calculateRoutes.tapPromise(
"GoogleMapsPlugin",
(source, target, routesList) =>
(source, target, routesList) =>
// return a promise
google.maps.findRoute(source, target).then((route) => {
google.maps.findRoute(source, target).then((route) => {
routesList.add(route);
})

);
myCar.hooks.calculateRoutes.tapAsync(
"BingMapsPlugin",
Expand Down Expand Up @@ -117,9 +116,9 @@ class Car {
const routesList = new List();
return this.hooks.calculateRoutes
.promise(source, target, routesList)
.then((res) =>
.then((res) =>
// res is undefined for AsyncParallelHook
routesList.getRoutes()
routesList.getRoutes()
);
}

Expand Down Expand Up @@ -263,7 +262,11 @@ interface Hook {
tap: (name: string | Tap, fn: (context?, ...args) => Result) => void;
tapAsync: (
name: string | Tap,
fn: (context?, ...args, callback: (err, result: Result) => void) => void
fn: (
context?,
...args,
callback: (err: Error | null, result: Result) => void
) => void
) => void;
tapPromise: (
name: string | Tap,
Expand Down Expand Up @@ -306,7 +309,10 @@ interface Hook {
isUsed: () => boolean;
call: (...args) => Result;
promise: (...args) => Promise<Result>;
callAsync: (...args, callback: (err, result: Result) => void) => void;
callAsync: (
...args,
callback: (err: Error | null, result: Result) => void
) => void;
}

interface HookMap {
Expand Down
4 changes: 3 additions & 1 deletion lib/HookCodeFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ class HookCodeFactory {
}
code += `}), function(_err${tapIndex}) {\n`;
code += `if(_hasResult${tapIndex}) throw _err${tapIndex};\n`;
code += onError(`_err${tapIndex}`);
code += onError(
`!_err${tapIndex} ? new Error('Tap function (tapPromise) rejects "' + _err${tapIndex} + '" value') : _err${tapIndex}`
);
code += "});\n";
break;
}
Expand Down
54 changes: 54 additions & 0 deletions lib/__tests__/HookTester.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,60 @@ class HookTester {
);
}

{
const hook = this.createHook(["x"], `${type}SinglePromiseWithNullReject`);
// eslint-disable-next-line prefer-promise-reject-errors
hook.tapPromise("promise", (_arg) => Promise.reject(null));
result[`${type}SinglePromiseWithNullReject`] = await this.gainResult(
(cb) => hook[type](null, cb)
);
}

{
const hook = this.createHook(
["x"],
`${type}SinglePromiseWithUndefinedReject`
);
// eslint-disable-next-line prefer-promise-reject-errors
hook.tapPromise("promise", (_arg) => Promise.reject(undefined));
result[`${type}SinglePromiseWithUndefinedReject`] = await this.gainResult(
(cb) => hook[type](null, cb)
);
}

{
const hook = this.createHook(["x"], `${type}SinglePromiseWithZeroReject`);
// eslint-disable-next-line prefer-promise-reject-errors
hook.tapPromise("promise", (_arg) => Promise.reject(0));
result[`${type}SinglePromiseWithZeroReject`] = await this.gainResult(
(cb) => hook[type](null, cb)
);
}

{
const hook = this.createHook(
["x"],
`${type}SinglePromiseWithFalseReject`
);
// eslint-disable-next-line prefer-promise-reject-errors
hook.tapPromise("promise", (_arg) => Promise.reject(false));
result[`${type}SinglePromiseWithFalseReject`] = await this.gainResult(
(cb) => hook[type](null, cb)
);
}

{
const hook = this.createHook(
["x"],
`${type}SinglePromiseWithEmptyReject`
);
// eslint-disable-next-line prefer-promise-reject-errors
hook.tapPromise("promise", (_arg) => Promise.reject(""));
result[`${type}SinglePromiseWithEmptyReject`] = await this.gainResult(
(cb) => hook[type](null, cb)
);
}

{
const hook = this.createHook(["x"], `${type}MultiplePromiseWithArg`);
hook.tapPromise("promise1", (arg) => {
Expand Down
80 changes: 80 additions & 0 deletions lib/__tests__/__snapshots__/AsyncParallelHooks.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,26 @@ Object {
"value": 43,
},
"callAsyncSinglePromiseWithArgCalled1": 42,
"callAsyncSinglePromiseWithEmptyReject": Object {
"error": "Tap function (tapPromise) rejects \\"\\" value",
"type": "async",
},
"callAsyncSinglePromiseWithFalseReject": Object {
"error": "Tap function (tapPromise) rejects \\"false\\" value",
"type": "async",
},
"callAsyncSinglePromiseWithNullReject": Object {
"error": "Tap function (tapPromise) rejects \\"null\\" value",
"type": "async",
},
"callAsyncSinglePromiseWithUndefinedReject": Object {
"error": "Tap function (tapPromise) rejects \\"undefined\\" value",
"type": "async",
},
"callAsyncSinglePromiseWithZeroReject": Object {
"error": "Tap function (tapPromise) rejects \\"0\\" value",
"type": "async",
},
"callAsyncSingleSync": Object {
"type": "async",
"value": 42,
Expand Down Expand Up @@ -402,6 +422,26 @@ Object {
"value": 43,
},
"promiseSinglePromiseWithArgCalled1": 42,
"promiseSinglePromiseWithEmptyReject": Object {
"error": "Tap function (tapPromise) rejects \\"\\" value",
"type": "promise",
},
"promiseSinglePromiseWithFalseReject": Object {
"error": "Tap function (tapPromise) rejects \\"false\\" value",
"type": "promise",
},
"promiseSinglePromiseWithNullReject": Object {
"error": "Tap function (tapPromise) rejects \\"null\\" value",
"type": "promise",
},
"promiseSinglePromiseWithUndefinedReject": Object {
"error": "Tap function (tapPromise) rejects \\"undefined\\" value",
"type": "promise",
},
"promiseSinglePromiseWithZeroReject": Object {
"error": "Tap function (tapPromise) rejects \\"0\\" value",
"type": "promise",
},
"promiseSingleSync": Object {
"type": "promise",
"value": 42,
Expand Down Expand Up @@ -925,6 +965,26 @@ Object {
"value": undefined,
},
"callAsyncSinglePromiseWithArgCalled1": 42,
"callAsyncSinglePromiseWithEmptyReject": Object {
"error": "Tap function (tapPromise) rejects \\"\\" value",
"type": "async",
},
"callAsyncSinglePromiseWithFalseReject": Object {
"error": "Tap function (tapPromise) rejects \\"false\\" value",
"type": "async",
},
"callAsyncSinglePromiseWithNullReject": Object {
"error": "Tap function (tapPromise) rejects \\"null\\" value",
"type": "async",
},
"callAsyncSinglePromiseWithUndefinedReject": Object {
"error": "Tap function (tapPromise) rejects \\"undefined\\" value",
"type": "async",
},
"callAsyncSinglePromiseWithZeroReject": Object {
"error": "Tap function (tapPromise) rejects \\"0\\" value",
"type": "async",
},
"callAsyncSingleSync": Object {
"type": "async",
"value": undefined,
Expand Down Expand Up @@ -1145,6 +1205,26 @@ Object {
"value": undefined,
},
"promiseSinglePromiseWithArgCalled1": 42,
"promiseSinglePromiseWithEmptyReject": Object {
"error": "Tap function (tapPromise) rejects \\"\\" value",
"type": "promise",
},
"promiseSinglePromiseWithFalseReject": Object {
"error": "Tap function (tapPromise) rejects \\"false\\" value",
"type": "promise",
},
"promiseSinglePromiseWithNullReject": Object {
"error": "Tap function (tapPromise) rejects \\"null\\" value",
"type": "promise",
},
"promiseSinglePromiseWithUndefinedReject": Object {
"error": "Tap function (tapPromise) rejects \\"undefined\\" value",
"type": "promise",
},
"promiseSinglePromiseWithZeroReject": Object {
"error": "Tap function (tapPromise) rejects \\"0\\" value",
"type": "promise",
},
"promiseSingleSync": Object {
"type": "promise",
"value": undefined,
Expand Down
120 changes: 120 additions & 0 deletions lib/__tests__/__snapshots__/AsyncSeriesHooks.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,26 @@ Object {
"value": 43,
},
"callAsyncSinglePromiseWithArgCalled1": 42,
"callAsyncSinglePromiseWithEmptyReject": Object {
"error": "Tap function (tapPromise) rejects \\"\\" value",
"type": "async",
},
"callAsyncSinglePromiseWithFalseReject": Object {
"error": "Tap function (tapPromise) rejects \\"false\\" value",
"type": "async",
},
"callAsyncSinglePromiseWithNullReject": Object {
"error": "Tap function (tapPromise) rejects \\"null\\" value",
"type": "async",
},
"callAsyncSinglePromiseWithUndefinedReject": Object {
"error": "Tap function (tapPromise) rejects \\"undefined\\" value",
"type": "async",
},
"callAsyncSinglePromiseWithZeroReject": Object {
"error": "Tap function (tapPromise) rejects \\"0\\" value",
"type": "async",
},
"callAsyncSingleSync": Object {
"type": "async",
"value": 42,
Expand Down Expand Up @@ -380,6 +400,26 @@ Object {
"value": 43,
},
"promiseSinglePromiseWithArgCalled1": 42,
"promiseSinglePromiseWithEmptyReject": Object {
"error": "Tap function (tapPromise) rejects \\"\\" value",
"type": "promise",
},
"promiseSinglePromiseWithFalseReject": Object {
"error": "Tap function (tapPromise) rejects \\"false\\" value",
"type": "promise",
},
"promiseSinglePromiseWithNullReject": Object {
"error": "Tap function (tapPromise) rejects \\"null\\" value",
"type": "promise",
},
"promiseSinglePromiseWithUndefinedReject": Object {
"error": "Tap function (tapPromise) rejects \\"undefined\\" value",
"type": "promise",
},
"promiseSinglePromiseWithZeroReject": Object {
"error": "Tap function (tapPromise) rejects \\"0\\" value",
"type": "promise",
},
"promiseSingleSync": Object {
"type": "promise",
"value": 42,
Expand Down Expand Up @@ -894,6 +934,26 @@ Object {
"value": undefined,
},
"callAsyncSinglePromiseWithArgCalled1": 42,
"callAsyncSinglePromiseWithEmptyReject": Object {
"error": "Tap function (tapPromise) rejects \\"\\" value",
"type": "async",
},
"callAsyncSinglePromiseWithFalseReject": Object {
"error": "Tap function (tapPromise) rejects \\"false\\" value",
"type": "async",
},
"callAsyncSinglePromiseWithNullReject": Object {
"error": "Tap function (tapPromise) rejects \\"null\\" value",
"type": "async",
},
"callAsyncSinglePromiseWithUndefinedReject": Object {
"error": "Tap function (tapPromise) rejects \\"undefined\\" value",
"type": "async",
},
"callAsyncSinglePromiseWithZeroReject": Object {
"error": "Tap function (tapPromise) rejects \\"0\\" value",
"type": "async",
},
"callAsyncSingleSync": Object {
"type": "async",
"value": undefined,
Expand Down Expand Up @@ -1105,6 +1165,26 @@ Object {
"value": undefined,
},
"promiseSinglePromiseWithArgCalled1": 42,
"promiseSinglePromiseWithEmptyReject": Object {
"error": "Tap function (tapPromise) rejects \\"\\" value",
"type": "promise",
},
"promiseSinglePromiseWithFalseReject": Object {
"error": "Tap function (tapPromise) rejects \\"false\\" value",
"type": "promise",
},
"promiseSinglePromiseWithNullReject": Object {
"error": "Tap function (tapPromise) rejects \\"null\\" value",
"type": "promise",
},
"promiseSinglePromiseWithUndefinedReject": Object {
"error": "Tap function (tapPromise) rejects \\"undefined\\" value",
"type": "promise",
},
"promiseSinglePromiseWithZeroReject": Object {
"error": "Tap function (tapPromise) rejects \\"0\\" value",
"type": "promise",
},
"promiseSingleSync": Object {
"type": "promise",
"value": undefined,
Expand Down Expand Up @@ -1735,6 +1815,26 @@ Object {
"value": 43,
},
"callAsyncSinglePromiseWithArgCalled1": 42,
"callAsyncSinglePromiseWithEmptyReject": Object {
"error": "Tap function (tapPromise) rejects \\"\\" value",
"type": "async",
},
"callAsyncSinglePromiseWithFalseReject": Object {
"error": "Tap function (tapPromise) rejects \\"false\\" value",
"type": "async",
},
"callAsyncSinglePromiseWithNullReject": Object {
"error": "Tap function (tapPromise) rejects \\"null\\" value",
"type": "async",
},
"callAsyncSinglePromiseWithUndefinedReject": Object {
"error": "Tap function (tapPromise) rejects \\"undefined\\" value",
"type": "async",
},
"callAsyncSinglePromiseWithZeroReject": Object {
"error": "Tap function (tapPromise) rejects \\"0\\" value",
"type": "async",
},
"callAsyncSingleSync": Object {
"error": "Waterfall hooks must have at least one argument",
},
Expand Down Expand Up @@ -1914,6 +2014,26 @@ Object {
"value": 43,
},
"promiseSinglePromiseWithArgCalled1": 42,
"promiseSinglePromiseWithEmptyReject": Object {
"error": "Tap function (tapPromise) rejects \\"\\" value",
"type": "promise",
},
"promiseSinglePromiseWithFalseReject": Object {
"error": "Tap function (tapPromise) rejects \\"false\\" value",
"type": "promise",
},
"promiseSinglePromiseWithNullReject": Object {
"error": "Tap function (tapPromise) rejects \\"null\\" value",
"type": "promise",
},
"promiseSinglePromiseWithUndefinedReject": Object {
"error": "Tap function (tapPromise) rejects \\"undefined\\" value",
"type": "promise",
},
"promiseSinglePromiseWithZeroReject": Object {
"error": "Tap function (tapPromise) rejects \\"0\\" value",
"type": "promise",
},
"promiseSingleSync": Object {
"error": "Waterfall hooks must have at least one argument",
},
Expand Down
Loading
Loading