Comparing version 1.0.0 to 1.1.0
@@ -12,19 +12,27 @@ /* | ||
describe("AsyncParallelHook", () => { | ||
it("should have to correct behavior", async () => { | ||
const tester = new HookTester((args) => new AsyncParallelHook(args)); | ||
it( | ||
"should have to correct behavior", | ||
async () => { | ||
const tester = new HookTester(args => new AsyncParallelHook(args)); | ||
const result = await tester.run(); | ||
const result = await tester.run(); | ||
expect(result).toMatchSnapshot(); | ||
}, 15000) | ||
}) | ||
expect(result).toMatchSnapshot(); | ||
}, | ||
15000 | ||
); | ||
}); | ||
describe("AsyncParallelBailHook", () => { | ||
it("should have to correct behavior", async () => { | ||
const tester = new HookTester((args) => new AsyncParallelBailHook(args)); | ||
it( | ||
"should have to correct behavior", | ||
async () => { | ||
const tester = new HookTester(args => new AsyncParallelBailHook(args)); | ||
const result = await tester.run(); | ||
const result = await tester.run(); | ||
expect(result).toMatchSnapshot(); | ||
}, 15000) | ||
}) | ||
expect(result).toMatchSnapshot(); | ||
}, | ||
15000 | ||
); | ||
}); |
@@ -15,3 +15,3 @@ /* | ||
it("should have to correct behavior", async () => { | ||
const tester = new HookTester((args) => new AsyncSeriesHook(args)); | ||
const tester = new HookTester(args => new AsyncSeriesHook(args)); | ||
@@ -21,8 +21,8 @@ const result = await tester.run(); | ||
expect(result).toMatchSnapshot(); | ||
}) | ||
}) | ||
}); | ||
}); | ||
describe("AsyncSeriesBailHook", () => { | ||
it("should have to correct behavior", async () => { | ||
const tester = new HookTester((args) => new AsyncSeriesBailHook(args)); | ||
const tester = new HookTester(args => new AsyncSeriesBailHook(args)); | ||
@@ -32,8 +32,8 @@ const result = await tester.run(); | ||
expect(result).toMatchSnapshot(); | ||
}) | ||
}) | ||
}); | ||
}); | ||
describe("AsyncSeriesWaterfallHook", () => { | ||
it("should have to correct behavior", async () => { | ||
const tester = new HookTester((args) => new AsyncSeriesWaterfallHook(args)); | ||
const tester = new HookTester(args => new AsyncSeriesWaterfallHook(args)); | ||
@@ -43,8 +43,8 @@ const result = await tester.run(); | ||
expect(result).toMatchSnapshot(); | ||
}) | ||
}) | ||
}); | ||
}); | ||
describe("AsyncSeriesLoopHook", () => { | ||
it("should have to correct behavior", async () => { | ||
const tester = new HookTester((args) => new AsyncSeriesLoopHook(args)); | ||
const tester = new HookTester(args => new AsyncSeriesLoopHook(args)); | ||
@@ -54,3 +54,3 @@ const result = await tester.runForLoop(); | ||
expect(result).toMatchSnapshot(); | ||
}) | ||
}) | ||
}); | ||
}); |
@@ -15,6 +15,9 @@ /* | ||
hook.tap("A", () => calls.push("A")); | ||
hook.tap({ | ||
name: "B", | ||
before: "A" | ||
}, () => calls.push("B")); | ||
hook.tap( | ||
{ | ||
name: "B", | ||
before: "A" | ||
}, | ||
() => calls.push("B") | ||
); | ||
@@ -25,6 +28,9 @@ calls.length = 0; | ||
hook.tap({ | ||
name: "C", | ||
before: ["A", "B"] | ||
}, () => calls.push("C")); | ||
hook.tap( | ||
{ | ||
name: "C", | ||
before: ["A", "B"] | ||
}, | ||
() => calls.push("C") | ||
); | ||
@@ -35,6 +41,9 @@ calls.length = 0; | ||
hook.tap({ | ||
name: "D", | ||
before: "B" | ||
}, () => calls.push("D")); | ||
hook.tap( | ||
{ | ||
name: "D", | ||
before: "B" | ||
}, | ||
() => calls.push("D") | ||
); | ||
@@ -45,10 +54,16 @@ calls.length = 0; | ||
hook.tap({ | ||
name: "E", | ||
stage: -5 | ||
}, () => calls.push("E")); | ||
hook.tap({ | ||
name: "F", | ||
stage: -3 | ||
}, () => calls.push("F")); | ||
hook.tap( | ||
{ | ||
name: "E", | ||
stage: -5 | ||
}, | ||
() => calls.push("E") | ||
); | ||
hook.tap( | ||
{ | ||
name: "F", | ||
stage: -3 | ||
}, | ||
() => calls.push("F") | ||
); | ||
@@ -55,0 +70,0 @@ calls.length = 0; |
@@ -9,3 +9,3 @@ /* | ||
const expectNoSyntaxError = (code) => { | ||
const expectNoSyntaxError = code => { | ||
new Function("a, b, c", code); | ||
@@ -70,7 +70,7 @@ }; | ||
call: () => {} | ||
}, | ||
} | ||
] | ||
} | ||
} | ||
for(const configurationName in factoryConfigurations) { | ||
}; | ||
for (const configurationName in factoryConfigurations) { | ||
describe(`(${configurationName})`, () => { | ||
@@ -135,3 +135,3 @@ let factory; | ||
const factoryConfigurations = { | ||
"none": { | ||
none: { | ||
args: ["a", "b", "c"], | ||
@@ -183,3 +183,3 @@ taps: [], | ||
}, | ||
"mixed": { | ||
mixed: { | ||
args: ["a", "b", "c"], | ||
@@ -199,3 +199,3 @@ taps: [ | ||
}, | ||
"mixed2": { | ||
mixed2: { | ||
args: ["a", "b", "c"], | ||
@@ -211,8 +211,8 @@ taps: [ | ||
type: "sync" | ||
}, | ||
} | ||
], | ||
interceptors: [] | ||
}, | ||
} | ||
for(const configurationName in factoryConfigurations) { | ||
} | ||
}; | ||
for (const configurationName in factoryConfigurations) { | ||
describe(`(${configurationName})`, () => { | ||
@@ -227,3 +227,4 @@ let factory; | ||
onError: (i, err) => `onError(${i}, ${err});\n`, | ||
onResult: (i, result, next, doneBreak) => `onResult(${i}, ${result}, () => {\n${next()}}, () => {\n${doneBreak()}});\n`, | ||
onResult: (i, result, next, doneBreak) => | ||
`onResult(${i}, ${result}, () => {\n${next()}}, () => {\n${doneBreak()}});\n`, | ||
onDone: () => "onDone();\n", | ||
@@ -238,3 +239,4 @@ rethrowIfPossible: true | ||
onError: (i, err) => `onError(${i}, ${err});\n`, | ||
onResult: (i, result, done, doneBreak) => `onResult(${i}, ${result}, () => {\n${done()}}, () => {\n${doneBreak()}});\n`, | ||
onResult: (i, result, done, doneBreak) => | ||
`onResult(${i}, ${result}, () => {\n${done()}}, () => {\n${doneBreak()}});\n`, | ||
onDone: () => "onDone();\n", | ||
@@ -241,0 +243,0 @@ rethrowIfPossible: true |
@@ -28,8 +28,5 @@ /* | ||
if(syncOnly) { | ||
if (syncOnly) { | ||
await this.runSync(result.sync, "call"); | ||
} else { | ||
await this.runAsync(result.async, "callAsync"); | ||
@@ -40,3 +37,2 @@ await this.runAsync(result.async, "promise"); | ||
await this.runIntercept(result.intercept, "promise"); | ||
} | ||
@@ -56,11 +52,7 @@ | ||
if(syncOnly) { | ||
if (syncOnly) { | ||
await this.runForLoopSync(result.sync, "call"); | ||
} else { | ||
await this.runForLoopAsync(result.async, "callAsync"); | ||
await this.runForLoopAsync(result.async, "promise"); | ||
} | ||
@@ -76,10 +68,20 @@ | ||
{ | ||
const hook = this.createHook([], `${method}BrokenPromise`); | ||
hook.tapPromise("promise", () => "this is not a promise"); | ||
result[`${method}BrokenPromise`] = await this.gainResult(cb => | ||
hook[method](cb) | ||
); | ||
} | ||
{ | ||
const hook = this.createHook([], `${method}SinglePromise`); | ||
hook.tapPromise("promise", () => { | ||
result[`${method}SinglePromiseCalled`] = (result[`${method}SinglePromiseCalled`] || 0) + 1; | ||
if(result[`${method}SinglePromiseCalled`] < 42) | ||
result[`${method}SinglePromiseCalled`] = | ||
(result[`${method}SinglePromiseCalled`] || 0) + 1; | ||
if (result[`${method}SinglePromiseCalled`] < 42) | ||
return Promise.resolve().then(() => true); | ||
return Promise.resolve().then(() => {}); | ||
}); | ||
result[`${method}SinglePromise`] = await this.gainResult(cb => hook[method](cb)); | ||
result[`${method}SinglePromise`] = await this.gainResult(cb => | ||
hook[method](cb) | ||
); | ||
} | ||
@@ -90,4 +92,5 @@ | ||
hook.tapPromise("promise1", () => { | ||
result[`${method}MultiplePromiseCalled1`] = (result[`${method}MultiplePromiseCalled1`] || 0) + 1; | ||
if(result[`${method}MultiplePromiseCalled1`] < 42) | ||
result[`${method}MultiplePromiseCalled1`] = | ||
(result[`${method}MultiplePromiseCalled1`] || 0) + 1; | ||
if (result[`${method}MultiplePromiseCalled1`] < 42) | ||
return Promise.resolve().then(() => true); | ||
@@ -97,8 +100,11 @@ return Promise.resolve().then(() => {}); | ||
hook.tapPromise("promise2", () => { | ||
result[`${method}MultiplePromiseCalled2`] = (result[`${method}MultiplePromiseCalled2`] || 0) + 1; | ||
if(result[`${method}MultiplePromiseCalled2`] < 42) | ||
result[`${method}MultiplePromiseCalled2`] = | ||
(result[`${method}MultiplePromiseCalled2`] || 0) + 1; | ||
if (result[`${method}MultiplePromiseCalled2`] < 42) | ||
return Promise.resolve().then(() => true); | ||
return Promise.resolve().then(() => {}); | ||
}); | ||
result[`${method}MultiplePromise`] = await this.gainResult(cb => hook[method](cb)); | ||
result[`${method}MultiplePromise`] = await this.gainResult(cb => | ||
hook[method](cb) | ||
); | ||
} | ||
@@ -108,9 +114,12 @@ | ||
const hook = this.createHook([], `${method}SingleAsync`); | ||
hook.tapAsync("async", (callback) => { | ||
result[`${method}SingleAsyncCalled`] = (result[`${method}SingleAsyncCalled`] || 0) + 1; | ||
if(result[`${method}SingleAsyncCalled`] < 42) | ||
hook.tapAsync("async", callback => { | ||
result[`${method}SingleAsyncCalled`] = | ||
(result[`${method}SingleAsyncCalled`] || 0) + 1; | ||
if (result[`${method}SingleAsyncCalled`] < 42) | ||
return Promise.resolve().then(() => callback(null, true)); | ||
return Promise.resolve().then(() => callback()); | ||
}); | ||
result[`${method}SingleAsync`] = await this.gainResult(cb => hook[method](cb)); | ||
result[`${method}SingleAsync`] = await this.gainResult(cb => | ||
hook[method](cb) | ||
); | ||
} | ||
@@ -120,15 +129,19 @@ | ||
const hook = this.createHook([], `${method}MultipleAsync`); | ||
hook.tapAsync("async1", (callback) => { | ||
result[`${method}MultipleAsyncCalled1`] = (result[`${method}MultipleAsyncCalled1`] || 0) + 1; | ||
if(result[`${method}MultipleAsyncCalled1`] < 42) | ||
hook.tapAsync("async1", callback => { | ||
result[`${method}MultipleAsyncCalled1`] = | ||
(result[`${method}MultipleAsyncCalled1`] || 0) + 1; | ||
if (result[`${method}MultipleAsyncCalled1`] < 42) | ||
return Promise.resolve().then(() => callback(null, true)); | ||
return Promise.resolve().then(() => callback()); | ||
}); | ||
hook.tapAsync("async2", (callback) => { | ||
result[`${method}MultipleAsyncCalled2`] = (result[`${method}MultipleAsyncCalled2`] || 0) + 1; | ||
if(result[`${method}MultipleAsyncCalled2`] < 42) | ||
hook.tapAsync("async2", callback => { | ||
result[`${method}MultipleAsyncCalled2`] = | ||
(result[`${method}MultipleAsyncCalled2`] || 0) + 1; | ||
if (result[`${method}MultipleAsyncCalled2`] < 42) | ||
return Promise.resolve().then(() => callback(null, true)); | ||
return Promise.resolve().then(() => callback()); | ||
}); | ||
result[`${method}MultipleAsync`] = await this.gainResult(cb => hook[method](cb)); | ||
result[`${method}MultipleAsync`] = await this.gainResult(cb => | ||
hook[method](cb) | ||
); | ||
} | ||
@@ -138,5 +151,6 @@ | ||
const hook = this.createHook([], `${method}Mixed`); | ||
hook.tapAsync("async1", (callback) => { | ||
result[`${method}MixedCalled1`] = (result[`${method}MixedCalled1`] || 0) + 1; | ||
if(result[`${method}MixedCalled1`] < 42) | ||
hook.tapAsync("async1", callback => { | ||
result[`${method}MixedCalled1`] = | ||
(result[`${method}MixedCalled1`] || 0) + 1; | ||
if (result[`${method}MixedCalled1`] < 42) | ||
return Promise.resolve().then(() => callback(null, true)); | ||
@@ -146,9 +160,10 @@ return Promise.resolve().then(() => callback()); | ||
hook.tap("sync2", () => { | ||
result[`${method}MixedCalled2`] = (result[`${method}MixedCalled2`] || 0) + 1; | ||
if(result[`${method}MixedCalled2`] < 42) | ||
return true; | ||
result[`${method}MixedCalled2`] = | ||
(result[`${method}MixedCalled2`] || 0) + 1; | ||
if (result[`${method}MixedCalled2`] < 42) return true; | ||
}); | ||
hook.tapPromise("promise3", () => { | ||
result[`${method}MixedCalled3`] = (result[`${method}MixedCalled3`] || 0) + 1; | ||
if(result[`${method}MixedCalled3`] < 42) | ||
result[`${method}MixedCalled3`] = | ||
(result[`${method}MixedCalled3`] || 0) + 1; | ||
if (result[`${method}MixedCalled3`] < 42) | ||
return Promise.resolve().then(() => true); | ||
@@ -169,3 +184,5 @@ return Promise.resolve().then(() => {}); | ||
const hook = this.createHook(["arg"], `${method}NoneWithArg`); | ||
result[`${method}NoneWithArg`] = await this.gainResult(cb => hook[method](42, cb)); | ||
result[`${method}NoneWithArg`] = await this.gainResult(cb => | ||
hook[method](42, cb) | ||
); | ||
} | ||
@@ -176,7 +193,9 @@ | ||
hook.tap("sync", () => { | ||
result[`${method}SingleSyncCalled`] = (result[`${method}SingleSyncCalled`] || 0) + 1; | ||
if(result[`${method}SingleSyncCalled`] < 42) | ||
return true; | ||
result[`${method}SingleSyncCalled`] = | ||
(result[`${method}SingleSyncCalled`] || 0) + 1; | ||
if (result[`${method}SingleSyncCalled`] < 42) return true; | ||
}); | ||
result[`${method}SingleSync`] = await this.gainResult(cb => hook[method](cb)); | ||
result[`${method}SingleSync`] = await this.gainResult(cb => | ||
hook[method](cb) | ||
); | ||
} | ||
@@ -187,12 +206,14 @@ | ||
hook.tap("sync1", () => { | ||
result[`${method}MultipleSyncCalled1`] = (result[`${method}MultipleSyncCalled1`] || 0) + 1; | ||
if(result[`${method}MultipleSyncCalled1`] < 42) | ||
return true; | ||
result[`${method}MultipleSyncCalled1`] = | ||
(result[`${method}MultipleSyncCalled1`] || 0) + 1; | ||
if (result[`${method}MultipleSyncCalled1`] < 42) return true; | ||
}); | ||
hook.tap("sync2", () => { | ||
result[`${method}MultipleSyncCalled2`] = (result[`${method}MultipleSyncCalled2`] || 0) + 1; | ||
if(result[`${method}MultipleSyncCalled2`] < 42) | ||
return true; | ||
result[`${method}MultipleSyncCalled2`] = | ||
(result[`${method}MultipleSyncCalled2`] || 0) + 1; | ||
if (result[`${method}MultipleSyncCalled2`] < 42) return true; | ||
}); | ||
result[`${method}MultipleSync`] = await this.gainResult(cb => hook[method](cb)); | ||
result[`${method}MultipleSync`] = await this.gainResult(cb => | ||
hook[method](cb) | ||
); | ||
} | ||
@@ -203,19 +224,26 @@ | ||
hook.tap("sync1", () => { | ||
result[`${method}InterceptedSyncCalled1`] = (result[`${method}InterceptedSyncCalled1`] || 0) + 1; | ||
if(result[`${method}InterceptedSyncCalled1`] < 42) | ||
return true; | ||
result[`${method}InterceptedSyncCalled1`] = | ||
(result[`${method}InterceptedSyncCalled1`] || 0) + 1; | ||
if (result[`${method}InterceptedSyncCalled1`] < 42) return true; | ||
}); | ||
hook.tap("sync2", () => { | ||
result[`${method}InterceptedSyncCalled2`] = (result[`${method}InterceptedSyncCalled2`] || 0) + 1; | ||
if(result[`${method}InterceptedSyncCalled2`] < 42) | ||
return true; | ||
result[`${method}InterceptedSyncCalled2`] = | ||
(result[`${method}InterceptedSyncCalled2`] || 0) + 1; | ||
if (result[`${method}InterceptedSyncCalled2`] < 42) return true; | ||
}); | ||
hook.intercept({ | ||
call: (a) => result[`${method}InterceptedSyncCalledCall`] = (result[`${method}InterceptedSyncCalledCall`] || 0) + 1, | ||
loop: (a) => result[`${method}InterceptedSyncCalledLoop`] = (result[`${method}InterceptedSyncCalledLoop`] || 0) + 1, | ||
tap: (tap) => { | ||
result[`${method}InterceptedSyncCalledTap`] = (result[`${method}InterceptedSyncCalledTap`] || 0) + 1 | ||
}, | ||
}) | ||
result[`${method}InterceptedSync`] = await this.gainResult(cb => hook[method](cb)); | ||
call: a => | ||
(result[`${method}InterceptedSyncCalledCall`] = | ||
(result[`${method}InterceptedSyncCalledCall`] || 0) + 1), | ||
loop: a => | ||
(result[`${method}InterceptedSyncCalledLoop`] = | ||
(result[`${method}InterceptedSyncCalledLoop`] || 0) + 1), | ||
tap: tap => { | ||
result[`${method}InterceptedSyncCalledTap`] = | ||
(result[`${method}InterceptedSyncCalledTap`] || 0) + 1; | ||
} | ||
}); | ||
result[`${method}InterceptedSync`] = await this.gainResult(cb => | ||
hook[method](cb) | ||
); | ||
} | ||
@@ -232,3 +260,5 @@ } | ||
const hook = this.createHook(["arg"], `${method}NoneWithArg`); | ||
result[`${method}NoneWithArg`] = await this.gainResult(cb => hook[method](42, cb)); | ||
result[`${method}NoneWithArg`] = await this.gainResult(cb => | ||
hook[method](42, cb) | ||
); | ||
} | ||
@@ -242,3 +272,5 @@ | ||
}); | ||
result[`${method}SingleSync`] = await this.gainResult(cb => hook[method](cb)); | ||
result[`${method}SingleSync`] = await this.gainResult(cb => | ||
hook[method](cb) | ||
); | ||
} | ||
@@ -248,7 +280,9 @@ | ||
const hook = this.createHook(["myArg"], `${method}SingleSyncWithArg`); | ||
hook.tap("sync", (nr) => { | ||
hook.tap("sync", nr => { | ||
result[`${method}SingleSyncWithArgCalled`] = nr; | ||
return nr; | ||
}); | ||
result[`${method}SingleSyncWithArg`] = await this.gainResult(cb => hook[method](42, cb)); | ||
result[`${method}SingleSyncWithArg`] = await this.gainResult(cb => | ||
hook[method](42, cb) | ||
); | ||
} | ||
@@ -266,3 +300,5 @@ | ||
}); | ||
result[`${method}MultipleSync`] = await this.gainResult(cb => hook[method](cb)); | ||
result[`${method}MultipleSync`] = await this.gainResult(cb => | ||
hook[method](cb) | ||
); | ||
} | ||
@@ -272,50 +308,70 @@ | ||
const hook = this.createHook(["a"], `${method}MultipleSyncWithArg`); | ||
hook.tap("sync1", (a) => { | ||
hook.tap("sync1", a => { | ||
result[`${method}MultipleSyncWithArgCalled1`] = a; | ||
return 42 + a; | ||
}); | ||
hook.tap("sync2", (a) => { | ||
hook.tap("sync2", a => { | ||
result[`${method}MultipleSyncWithArgCalled2`] = a; | ||
return 43 + a; | ||
}); | ||
result[`${method}MultipleSyncWithArg`] = await this.gainResult(cb => hook[method](42, cb)); | ||
result[`${method}MultipleSyncWithArg`] = await this.gainResult(cb => | ||
hook[method](42, cb) | ||
); | ||
} | ||
{ | ||
const hook = this.createHook(["a"], `${method}MultipleSyncWithArgNoReturn`); | ||
hook.tap("sync1", (a) => { | ||
const hook = this.createHook( | ||
["a"], | ||
`${method}MultipleSyncWithArgNoReturn` | ||
); | ||
hook.tap("sync1", a => { | ||
result[`${method}MultipleSyncWithArgNoReturnCalled1`] = a; | ||
}); | ||
hook.tap("sync2", (a) => { | ||
hook.tap("sync2", a => { | ||
result[`${method}MultipleSyncWithArgNoReturnCalled2`] = a; | ||
}); | ||
result[`${method}MultipleSyncWithArgNoReturn`] = await this.gainResult(cb => hook[method](42, cb)); | ||
result[`${method}MultipleSyncWithArgNoReturn`] = await this.gainResult( | ||
cb => hook[method](42, cb) | ||
); | ||
} | ||
{ | ||
const hook = this.createHook(["a"], `${method}MultipleSyncWithArgFirstReturn`); | ||
hook.tap("sync1", (a) => { | ||
const hook = this.createHook( | ||
["a"], | ||
`${method}MultipleSyncWithArgFirstReturn` | ||
); | ||
hook.tap("sync1", a => { | ||
result[`${method}MultipleSyncWithArgFirstReturnCalled1`] = a; | ||
return 42 + a; | ||
}); | ||
hook.tap("sync2", (a) => { | ||
hook.tap("sync2", a => { | ||
result[`${method}MultipleSyncWithArgFirstReturnCalled2`] = a; | ||
}); | ||
result[`${method}MultipleSyncWithArgFirstReturn`] = await this.gainResult(cb => hook[method](42, cb)); | ||
result[`${method}MultipleSyncWithArgFirstReturn`] = await this.gainResult( | ||
cb => hook[method](42, cb) | ||
); | ||
} | ||
{ | ||
const hook = this.createHook(["a"], `${method}MultipleSyncWithArgLastReturn`); | ||
hook.tap("sync1", (a) => { | ||
const hook = this.createHook( | ||
["a"], | ||
`${method}MultipleSyncWithArgLastReturn` | ||
); | ||
hook.tap("sync1", a => { | ||
result[`${method}MultipleSyncWithArgLastReturnCalled1`] = a; | ||
}); | ||
hook.tap("sync2", (a) => { | ||
hook.tap("sync2", a => { | ||
result[`${method}MultipleSyncWithArgLastReturnCalled2`] = a; | ||
return 43 + a; | ||
}); | ||
result[`${method}MultipleSyncWithArgLastReturn`] = await this.gainResult(cb => hook[method](42, cb)); | ||
result[`${method}MultipleSyncWithArgLastReturn`] = await this.gainResult( | ||
cb => hook[method](42, cb) | ||
); | ||
} | ||
{ | ||
const hook = this.createHook(["a", "b", "c"], `${method}MultipleSyncWithArgs`); | ||
const hook = this.createHook( | ||
["a", "b", "c"], | ||
`${method}MultipleSyncWithArgs` | ||
); | ||
hook.tap("sync1", (a, b, c) => { | ||
@@ -329,3 +385,5 @@ result[`${method}MultipleSyncWithArgsCalled1`] = [a, b, c]; | ||
}); | ||
result[`${method}MultipleSyncWithArgs`] = await this.gainResult(cb => hook[method](42, 43, 44, cb)); | ||
result[`${method}MultipleSyncWithArgs`] = await this.gainResult(cb => | ||
hook[method](42, 43, 44, cb) | ||
); | ||
} | ||
@@ -340,3 +398,3 @@ | ||
result[`${method}MultipleSyncErrorCalled2`] = true; | ||
throw new Error("Error in sync2") | ||
throw new Error("Error in sync2"); | ||
}); | ||
@@ -346,3 +404,5 @@ hook.tap("sync3", () => { | ||
}); | ||
result[`${method}MultipleSyncError`] = await this.gainResult(cb => hook[method](cb)); | ||
result[`${method}MultipleSyncError`] = await this.gainResult(cb => | ||
hook[method](cb) | ||
); | ||
} | ||
@@ -356,4 +416,6 @@ | ||
}, | ||
tap: (tap) => { | ||
result[`${method}InterceptedTap1`] = Object.assign({}, tap, { fn: tap.fn.length }); | ||
tap: tap => { | ||
result[`${method}InterceptedTap1`] = Object.assign({}, tap, { | ||
fn: tap.fn.length | ||
}); | ||
} | ||
@@ -365,5 +427,7 @@ }); | ||
}, | ||
tap: (tap) => { | ||
if(!result[`${method}InterceptedTap2`]) | ||
result[`${method}InterceptedTap2`] = Object.assign({}, tap, { fn: tap.fn.length }); | ||
tap: tap => { | ||
if (!result[`${method}InterceptedTap2`]) | ||
result[`${method}InterceptedTap2`] = Object.assign({}, tap, { | ||
fn: tap.fn.length | ||
}); | ||
} | ||
@@ -373,3 +437,5 @@ }); | ||
hook.tap("sync2", (a, b) => a + b + 1); | ||
result[`${method}Intercepted`] = await this.gainResult((cb) => hook[method](1, 2, 3, cb)); | ||
result[`${method}Intercepted`] = await this.gainResult(cb => | ||
hook[method](1, 2, 3, cb) | ||
); | ||
} | ||
@@ -381,3 +447,3 @@ } | ||
const hook = this.createHook([], `${type}None`); | ||
result[`${type}None`] = await this.gainResult((cb) => hook[type](cb)); | ||
result[`${type}None`] = await this.gainResult(cb => hook[type](cb)); | ||
} | ||
@@ -387,3 +453,5 @@ | ||
const hook = this.createHook(["arg"], `${type}NoneWithArg`); | ||
result[`${type}NoneWithArg`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[`${type}NoneWithArg`] = await this.gainResult(cb => | ||
hook[type](42, cb) | ||
); | ||
} | ||
@@ -397,3 +465,3 @@ | ||
}); | ||
result[`${type}SingleSync`] = await this.gainResult((cb) => hook[type](cb)); | ||
result[`${type}SingleSync`] = await this.gainResult(cb => hook[type](cb)); | ||
} | ||
@@ -407,3 +475,5 @@ | ||
}); | ||
result[`${type}SingleSyncWithArg`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[`${type}SingleSyncWithArg`] = await this.gainResult(cb => | ||
hook[type](42, cb) | ||
); | ||
} | ||
@@ -416,3 +486,5 @@ | ||
}); | ||
result[`${type}SingleSyncWithArgNoReturn`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[`${type}SingleSyncWithArgNoReturn`] = await this.gainResult(cb => | ||
hook[type](42, cb) | ||
); | ||
} | ||
@@ -430,3 +502,5 @@ | ||
}); | ||
result[`${type}MultipleSync`] = await this.gainResult((cb) => hook[type](cb)); | ||
result[`${type}MultipleSync`] = await this.gainResult(cb => | ||
hook[type](cb) | ||
); | ||
} | ||
@@ -443,3 +517,5 @@ | ||
}); | ||
result[`${type}MultipleSyncLastReturn`] = await this.gainResult((cb) => hook[type](cb)); | ||
result[`${type}MultipleSyncLastReturn`] = await this.gainResult(cb => | ||
hook[type](cb) | ||
); | ||
} | ||
@@ -455,3 +531,5 @@ | ||
}); | ||
result[`${type}MultipleSyncNoReturn`] = await this.gainResult((cb) => hook[type](cb)); | ||
result[`${type}MultipleSyncNoReturn`] = await this.gainResult(cb => | ||
hook[type](cb) | ||
); | ||
} | ||
@@ -469,7 +547,12 @@ | ||
}); | ||
result[`${type}MultipleSyncWithArg`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[`${type}MultipleSyncWithArg`] = await this.gainResult(cb => | ||
hook[type](42, cb) | ||
); | ||
} | ||
{ | ||
const hook = this.createHook(["arg"], `${type}MultipleSyncWithArgNoReturn`); | ||
const hook = this.createHook( | ||
["arg"], | ||
`${type}MultipleSyncWithArgNoReturn` | ||
); | ||
hook.tap("sync1", arg => { | ||
@@ -481,7 +564,12 @@ result[`${type}MultipleSyncWithArgNoReturnCalled1`] = arg; | ||
}); | ||
result[`${type}MultipleSyncWithArgNoReturn`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[`${type}MultipleSyncWithArgNoReturn`] = await this.gainResult(cb => | ||
hook[type](42, cb) | ||
); | ||
} | ||
{ | ||
const hook = this.createHook(["arg"], `${type}MultipleSyncWithArgLastReturn`); | ||
const hook = this.createHook( | ||
["arg"], | ||
`${type}MultipleSyncWithArgLastReturn` | ||
); | ||
hook.tap("sync1", arg => { | ||
@@ -494,7 +582,12 @@ result[`${type}MultipleSyncWithArgLastReturnCalled1`] = arg; | ||
}); | ||
result[`${type}MultipleSyncWithArgLastReturn`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[`${type}MultipleSyncWithArgLastReturn`] = await this.gainResult( | ||
cb => hook[type](42, cb) | ||
); | ||
} | ||
{ | ||
const hook = this.createHook(["arg"], `${type}MultipleSyncWithArgFirstReturn`); | ||
const hook = this.createHook( | ||
["arg"], | ||
`${type}MultipleSyncWithArgFirstReturn` | ||
); | ||
hook.tap("sync1", arg => { | ||
@@ -507,3 +600,5 @@ result[`${type}MultipleSyncWithArgFirstReturnCalled1`] = arg; | ||
}); | ||
result[`${type}MultipleSyncWithArgFirstReturn`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[`${type}MultipleSyncWithArgFirstReturn`] = await this.gainResult( | ||
cb => hook[type](42, cb) | ||
); | ||
} | ||
@@ -517,3 +612,5 @@ | ||
}); | ||
result[`${type}SingleAsyncWithArg`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[`${type}SingleAsyncWithArg`] = await this.gainResult(cb => | ||
hook[type](42, cb) | ||
); | ||
} | ||
@@ -531,7 +628,12 @@ | ||
}); | ||
result[`${type}MultipleAsyncWithArg`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[`${type}MultipleAsyncWithArg`] = await this.gainResult(cb => | ||
hook[type](42, cb) | ||
); | ||
} | ||
{ | ||
const hook = this.createHook(["x"], `${type}MultipleAsyncWithArgNoReturn`); | ||
const hook = this.createHook( | ||
["x"], | ||
`${type}MultipleAsyncWithArgNoReturn` | ||
); | ||
hook.tapAsync("async1", (arg, callback) => { | ||
@@ -545,7 +647,12 @@ result[`${type}MultipleAsyncWithArgNoReturnCalled1`] = arg; | ||
}); | ||
result[`${type}MultipleAsyncWithArgNoReturn`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[`${type}MultipleAsyncWithArgNoReturn`] = await this.gainResult( | ||
cb => hook[type](42, cb) | ||
); | ||
} | ||
{ | ||
const hook = this.createHook(["x"], `${type}MultipleAsyncWithArgFirstReturn`); | ||
const hook = this.createHook( | ||
["x"], | ||
`${type}MultipleAsyncWithArgFirstReturn` | ||
); | ||
hook.tapAsync("async1", (arg, callback) => { | ||
@@ -559,7 +666,12 @@ result[`${type}MultipleAsyncWithArgFirstReturnCalled1`] = arg; | ||
}); | ||
result[`${type}MultipleAsyncWithArgFirstReturn`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[`${type}MultipleAsyncWithArgFirstReturn`] = await this.gainResult( | ||
cb => hook[type](42, cb) | ||
); | ||
} | ||
{ | ||
const hook = this.createHook(["x"], `${type}MultipleAsyncWithArgLastReturn`); | ||
const hook = this.createHook( | ||
["x"], | ||
`${type}MultipleAsyncWithArgLastReturn` | ||
); | ||
hook.tapAsync("async1", (arg, callback) => { | ||
@@ -573,3 +685,5 @@ result[`${type}MultipleAsyncWithArgLastReturnCalled1`] = arg; | ||
}); | ||
result[`${type}MultipleAsyncWithArgLastReturn`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[`${type}MultipleAsyncWithArgLastReturn`] = await this.gainResult( | ||
cb => hook[type](42, cb) | ||
); | ||
} | ||
@@ -583,3 +697,5 @@ | ||
}); | ||
result[`${type}SinglePromiseWithArg`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[`${type}SinglePromiseWithArg`] = await this.gainResult(cb => | ||
hook[type](42, cb) | ||
); | ||
} | ||
@@ -597,7 +713,12 @@ | ||
}); | ||
result[`${type}MultiplePromiseWithArg`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[`${type}MultiplePromiseWithArg`] = await this.gainResult(cb => | ||
hook[type](42, cb) | ||
); | ||
} | ||
{ | ||
const hook = this.createHook(["x"], `${type}MultiplePromiseWithArgNoReturn`); | ||
const hook = this.createHook( | ||
["x"], | ||
`${type}MultiplePromiseWithArgNoReturn` | ||
); | ||
hook.tapPromise("promise1", arg => { | ||
@@ -611,7 +732,12 @@ result[`${type}MultiplePromiseWithArgNoReturnCalled1`] = arg; | ||
}); | ||
result[`${type}MultiplePromiseWithArgNoReturn`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[`${type}MultiplePromiseWithArgNoReturn`] = await this.gainResult( | ||
cb => hook[type](42, cb) | ||
); | ||
} | ||
{ | ||
const hook = this.createHook(["x"], `${type}MultiplePromiseWithArgFirstReturn`); | ||
const hook = this.createHook( | ||
["x"], | ||
`${type}MultiplePromiseWithArgFirstReturn` | ||
); | ||
hook.tapPromise("promise1", arg => { | ||
@@ -625,7 +751,12 @@ result[`${type}MultiplePromiseWithArgFirstReturnCalled1`] = arg; | ||
}); | ||
result[`${type}MultiplePromiseWithArgFirstReturn`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[ | ||
`${type}MultiplePromiseWithArgFirstReturn` | ||
] = await this.gainResult(cb => hook[type](42, cb)); | ||
} | ||
{ | ||
const hook = this.createHook(["x"], `${type}MultiplePromiseWithArgLastReturn`); | ||
const hook = this.createHook( | ||
["x"], | ||
`${type}MultiplePromiseWithArgLastReturn` | ||
); | ||
hook.tapPromise("promise1", arg => { | ||
@@ -639,3 +770,5 @@ result[`${type}MultiplePromiseWithArgLastReturnCalled1`] = arg; | ||
}); | ||
result[`${type}MultiplePromiseWithArgLastReturn`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[`${type}MultiplePromiseWithArgLastReturn`] = await this.gainResult( | ||
cb => hook[type](42, cb) | ||
); | ||
} | ||
@@ -657,3 +790,5 @@ | ||
}); | ||
result[`${type}MultipleMixed1WithArg`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[`${type}MultipleMixed1WithArg`] = await this.gainResult(cb => | ||
hook[type](42, cb) | ||
); | ||
} | ||
@@ -671,3 +806,5 @@ | ||
}); | ||
result[`${type}MultipleMixed2WithArg`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[`${type}MultipleMixed2WithArg`] = await this.gainResult(cb => | ||
hook[type](42, cb) | ||
); | ||
} | ||
@@ -689,3 +826,5 @@ | ||
}); | ||
result[`${type}MultipleMixed3WithArg`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[`${type}MultipleMixed3WithArg`] = await this.gainResult(cb => | ||
hook[type](42, cb) | ||
); | ||
} | ||
@@ -699,3 +838,3 @@ | ||
hook.tap("sync2", () => { | ||
throw new Error("Error in sync2") | ||
throw new Error("Error in sync2"); | ||
}); | ||
@@ -705,3 +844,5 @@ hook.tap("sync3", () => { | ||
}); | ||
result[`${type}MultipleSyncError`] = await this.gainResult((cb) => hook[type](cb)); | ||
result[`${type}MultipleSyncError`] = await this.gainResult(cb => | ||
hook[type](cb) | ||
); | ||
} | ||
@@ -711,14 +852,16 @@ | ||
const hook = this.createHook([], `${type}MultipleAsyncError`); | ||
hook.tapAsync("async1", (callback) => { | ||
hook.tapAsync("async1", callback => { | ||
result[`${type}MultipleAsyncErrorCalled1`] = true; | ||
callback(); | ||
}); | ||
hook.tapAsync("async2", (callback) => { | ||
hook.tapAsync("async2", callback => { | ||
callback(new Error("Error in async2")); | ||
}); | ||
hook.tapAsync("async3", (callback) => { | ||
hook.tapAsync("async3", callback => { | ||
result[`${type}MultipleAsyncErrorCalled3`] = true; | ||
callback(); | ||
}); | ||
result[`${type}MultipleAsyncError`] = await this.gainResult((cb) => hook[type](cb)); | ||
result[`${type}MultipleAsyncError`] = await this.gainResult(cb => | ||
hook[type](cb) | ||
); | ||
} | ||
@@ -728,46 +871,58 @@ | ||
const hook = this.createHook([], `${type}MultipleAsyncLateError`); | ||
hook.tapAsync("async1", (callback) => { | ||
hook.tapAsync("async1", callback => { | ||
result[`${type}MultipleAsyncLateErrorCalled1`] = true; | ||
callback(); | ||
}); | ||
hook.tapAsync("async2", (callback) => { | ||
hook.tapAsync("async2", callback => { | ||
setTimeout(() => callback(new Error("Error in async2")), 100); | ||
}); | ||
hook.tapAsync("async3", (callback) => { | ||
hook.tapAsync("async3", callback => { | ||
result[`${type}MultipleAsyncLateErrorCalled3`] = true; | ||
callback(); | ||
}); | ||
result[`${type}MultipleAsyncLateError`] = await this.gainResult((cb) => hook[type](cb)); | ||
result[`${type}MultipleAsyncLateError`] = await this.gainResult(cb => | ||
hook[type](cb) | ||
); | ||
} | ||
{ | ||
const hook = this.createHook([], `${type}MultipleAsyncLateErrorEarlyResult1`); | ||
hook.tapAsync("async1", (callback) => { | ||
const hook = this.createHook( | ||
[], | ||
`${type}MultipleAsyncLateErrorEarlyResult1` | ||
); | ||
hook.tapAsync("async1", callback => { | ||
result[`${type}MultipleAsyncLateErrorEarlyResult1Called1`] = true; | ||
callback(); | ||
}); | ||
hook.tapAsync("async2", (callback) => { | ||
hook.tapAsync("async2", callback => { | ||
setTimeout(() => callback(new Error("Error in async2")), 100); | ||
}); | ||
hook.tapAsync("async3", (callback) => { | ||
hook.tapAsync("async3", callback => { | ||
result[`${type}MultipleAsyncLateErrorEarlyResult1Called3`] = true; | ||
callback(null, 7); | ||
}); | ||
result[`${type}MultipleAsyncLateErrorEarlyResult1`] = await this.gainResult((cb) => hook[type](cb)); | ||
result[ | ||
`${type}MultipleAsyncLateErrorEarlyResult1` | ||
] = await this.gainResult(cb => hook[type](cb)); | ||
} | ||
{ | ||
const hook = this.createHook([], `${type}MultipleAsyncLateErrorEarlyResult2`); | ||
hook.tapAsync("async1", (callback) => { | ||
const hook = this.createHook( | ||
[], | ||
`${type}MultipleAsyncLateErrorEarlyResult2` | ||
); | ||
hook.tapAsync("async1", callback => { | ||
result[`${type}MultipleAsyncLateErrorEarlyResult2Called1`] = true; | ||
setTimeout(() => callback(null, 42), 200); | ||
}); | ||
hook.tapAsync("async2", (callback) => { | ||
hook.tapAsync("async2", callback => { | ||
setTimeout(() => callback(new Error("Error in async2")), 100); | ||
}); | ||
hook.tapAsync("async3", (callback) => { | ||
hook.tapAsync("async3", callback => { | ||
result[`${type}MultipleAsyncLateErrorEarlyResult2Called3`] = true; | ||
callback(null, 7); | ||
}); | ||
result[`${type}MultipleAsyncLateErrorEarlyResult2`] = await this.gainResult((cb) => hook[type](cb)); | ||
result[ | ||
`${type}MultipleAsyncLateErrorEarlyResult2` | ||
] = await this.gainResult(cb => hook[type](cb)); | ||
} | ||
@@ -777,14 +932,16 @@ | ||
const hook = this.createHook([], `${type}MultipleAsyncEarlyError`); | ||
hook.tapAsync("async1", (callback) => { | ||
hook.tapAsync("async1", callback => { | ||
result[`${type}MultipleAsyncEarlyErrorCalled1`] = true; | ||
setTimeout(() => callback(), 100); | ||
}); | ||
hook.tapAsync("async2", (callback) => { | ||
hook.tapAsync("async2", callback => { | ||
callback(new Error("Error in async2")); | ||
}); | ||
hook.tapAsync("async3", (callback) => { | ||
hook.tapAsync("async3", callback => { | ||
result[`${type}MultipleAsyncEarlyErrorCalled3`] = true; | ||
setTimeout(() => callback(), 100); | ||
}); | ||
result[`${type}MultipleAsyncEarlyError`] = await this.gainResult((cb) => hook[type](cb)); | ||
result[`${type}MultipleAsyncEarlyError`] = await this.gainResult(cb => | ||
hook[type](cb) | ||
); | ||
} | ||
@@ -799,3 +956,5 @@ | ||
hook.tapPromise("promise2", () => { | ||
return Promise.resolve().then(() => { throw new Error("Error in async2"); }); | ||
return Promise.resolve().then(() => { | ||
throw new Error("Error in async2"); | ||
}); | ||
}); | ||
@@ -806,3 +965,5 @@ hook.tapPromise("promise3", () => { | ||
}); | ||
result[`${type}MultiplePromiseError`] = await this.gainResult((cb) => hook[type](cb)); | ||
result[`${type}MultiplePromiseError`] = await this.gainResult(cb => | ||
hook[type](cb) | ||
); | ||
} | ||
@@ -825,3 +986,5 @@ | ||
}); | ||
result[`${type}MultiplePromiseLateError`] = await this.gainResult((cb) => hook[type](cb)); | ||
result[`${type}MultiplePromiseLateError`] = await this.gainResult(cb => | ||
hook[type](cb) | ||
); | ||
} | ||
@@ -836,3 +999,5 @@ | ||
hook.tapPromise("promise2", () => { | ||
return Promise.resolve().then(() => { throw new Error("Error in async2"); }); | ||
return Promise.resolve().then(() => { | ||
throw new Error("Error in async2"); | ||
}); | ||
}); | ||
@@ -843,3 +1008,5 @@ hook.tapPromise("promise3", () => { | ||
}); | ||
result[`${type}MultiplePromiseEarlyError`] = await this.gainResult((cb) => hook[type](cb)); | ||
result[`${type}MultiplePromiseEarlyError`] = await this.gainResult(cb => | ||
hook[type](cb) | ||
); | ||
} | ||
@@ -861,3 +1028,5 @@ | ||
}); | ||
result[`${type}MultipleMixedError1WithArg`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[`${type}MultipleMixedError1WithArg`] = await this.gainResult(cb => | ||
hook[type](42, cb) | ||
); | ||
} | ||
@@ -873,3 +1042,5 @@ | ||
result[`${type}MultipleMixedError2WithArgCalled2`] = arg; | ||
return Promise.resolve().then(() => { throw new Error("Error in promise"); }); | ||
return Promise.resolve().then(() => { | ||
throw new Error("Error in promise"); | ||
}); | ||
}); | ||
@@ -880,3 +1051,5 @@ hook.tap("sync", arg => { | ||
}); | ||
result[`${type}MultipleMixedError2WithArg`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[`${type}MultipleMixedError2WithArg`] = await this.gainResult(cb => | ||
hook[type](42, cb) | ||
); | ||
} | ||
@@ -898,3 +1071,5 @@ | ||
}); | ||
result[`${type}MultipleMixedError3WithArg`] = await this.gainResult((cb) => hook[type](42, cb)); | ||
result[`${type}MultipleMixedError3WithArg`] = await this.gainResult(cb => | ||
hook[type](42, cb) | ||
); | ||
} | ||
@@ -904,3 +1079,3 @@ | ||
const hook = this.createHook([], `${type}MultipleMixedLateError`); | ||
hook.tapAsync("async", (callback) => { | ||
hook.tapAsync("async", callback => { | ||
result[`${type}MultipleMixedLateErrorCalled1`] = true; | ||
@@ -917,3 +1092,5 @@ setTimeout(() => callback(new Error("Error in async")), 100); | ||
}); | ||
result[`${type}MultipleMixedLateError`] = await this.gainResult((cb) => hook[type](cb)); | ||
result[`${type}MultipleMixedLateError`] = await this.gainResult(cb => | ||
hook[type](cb) | ||
); | ||
} | ||
@@ -929,4 +1106,6 @@ } | ||
}, | ||
tap: (tap) => { | ||
result[`${type}InterceptedTap1`] = Object.assign({}, tap, { fn: tap.fn.length }); | ||
tap: tap => { | ||
result[`${type}InterceptedTap1`] = Object.assign({}, tap, { | ||
fn: tap.fn.length | ||
}); | ||
} | ||
@@ -938,5 +1117,7 @@ }); | ||
}, | ||
tap: (tap) => { | ||
if(!result[`${type}InterceptedTap2`]) | ||
result[`${type}InterceptedTap2`] = Object.assign({}, tap, { fn: tap.fn.length }); | ||
tap: tap => { | ||
if (!result[`${type}InterceptedTap2`]) | ||
result[`${type}InterceptedTap2`] = Object.assign({}, tap, { | ||
fn: tap.fn.length | ||
}); | ||
} | ||
@@ -946,7 +1127,12 @@ }); | ||
hook.tapPromise("promise", (a, b) => Promise.resolve(a + b + 1)); | ||
result[`${type}Intercepted`] = await this.gainResult((cb) => hook[type](1, 2, 3, cb)); | ||
result[`${type}Intercepted`] = await this.gainResult(cb => | ||
hook[type](1, 2, 3, cb) | ||
); | ||
} | ||
{ | ||
const hook = this.createHook(["a", "b", "c"], `${type}ContextIntercepted`); | ||
const hook = this.createHook( | ||
["a", "b", "c"], | ||
`${type}ContextIntercepted` | ||
); | ||
hook.intercept({ | ||
@@ -971,11 +1157,19 @@ call: (context, a, b, c) => { | ||
}); | ||
hook.tap({ | ||
name: "sync", | ||
context: true | ||
}, (context, a, b, c) => context.number + a + b + c); | ||
result[`${type}ContextIntercepted`] = await this.gainResult((cb) => hook[type](1, 2, 3, cb)); | ||
hook.tap( | ||
{ | ||
name: "sync", | ||
context: true | ||
}, | ||
(context, a, b, c) => context.number + a + b + c | ||
); | ||
result[`${type}ContextIntercepted`] = await this.gainResult(cb => | ||
hook[type](1, 2, 3, cb) | ||
); | ||
} | ||
{ | ||
const hook = this.createHook(["a", "b", "c"], `${type}UnusedContextIntercepted`); | ||
const hook = this.createHook( | ||
["a", "b", "c"], | ||
`${type}UnusedContextIntercepted` | ||
); | ||
hook.intercept({ | ||
@@ -996,3 +1190,5 @@ call: (context, a, b, c) => { | ||
hook.tap("sync", (a, b, c) => a + b + c); | ||
result[`${type}UnusedContextIntercepted`] = await this.gainResult((cb) => hook[type](1, 2, 3, cb)); | ||
result[`${type}UnusedContextIntercepted`] = await this.gainResult(cb => | ||
hook[type](1, 2, 3, cb) | ||
); | ||
} | ||
@@ -1002,41 +1198,53 @@ } | ||
gainResult(fn) { | ||
return Promise.race([new Promise(resolve => { | ||
try { | ||
const ret = fn((err, result) => { | ||
if(err) { | ||
return Promise.race([ | ||
new Promise(resolve => { | ||
try { | ||
const ret = fn((err, result) => { | ||
if (err) { | ||
resolve({ | ||
type: "async", | ||
error: err.message | ||
}); | ||
} else { | ||
resolve({ | ||
type: "async", | ||
value: result | ||
}); | ||
} | ||
}); | ||
if (ret instanceof Promise) { | ||
resolve( | ||
ret.then( | ||
res => ({ | ||
type: "promise", | ||
value: res | ||
}), | ||
err => ({ | ||
type: "promise", | ||
error: err.message | ||
}) | ||
) | ||
); | ||
} else if (ret !== undefined) { | ||
resolve({ | ||
type: "async", | ||
error: err.message | ||
type: "return", | ||
value: ret | ||
}); | ||
} else { | ||
resolve({ | ||
type: "async", | ||
value: result | ||
}); | ||
} | ||
}); | ||
if(ret instanceof Promise) { | ||
resolve(ret.then(res => ({ | ||
type: "promise", | ||
value: res | ||
}), err => ({ | ||
type: "promise", | ||
error: err.message | ||
}))); | ||
} else if(ret !== undefined) { | ||
} catch (e) { | ||
resolve({ | ||
type: "return", | ||
value: ret | ||
}) | ||
error: e.message | ||
}); | ||
} | ||
} catch(e) { | ||
resolve({ | ||
error: e.message | ||
}); | ||
} | ||
}), new Promise(resolve => { | ||
setTimeout(() => resolve({ | ||
type: "no result" | ||
}), 1000); | ||
})]); | ||
}), | ||
new Promise(resolve => { | ||
setTimeout( | ||
() => | ||
resolve({ | ||
type: "no result" | ||
}), | ||
1000 | ||
); | ||
}) | ||
]); | ||
} | ||
@@ -1047,3 +1255,3 @@ | ||
return this.hookCreator(args, name); | ||
} catch(err) { | ||
} catch (err) { | ||
return { | ||
@@ -1054,6 +1262,12 @@ tap: () => {}, | ||
intercept: () => {}, | ||
call: () => { throw err; }, | ||
callAsync: () => { throw err; }, | ||
promise: () => { throw err; }, | ||
} | ||
call: () => { | ||
throw err; | ||
}, | ||
callAsync: () => { | ||
throw err; | ||
}, | ||
promise: () => { | ||
throw err; | ||
} | ||
}; | ||
} | ||
@@ -1060,0 +1274,0 @@ } |
@@ -13,3 +13,3 @@ /* | ||
const redirectedMethods = ["tap", "tapAsync", "tapPromise"]; | ||
for(const name of redirectedMethods) { | ||
for (const name of redirectedMethods) { | ||
it(`should redirect ${name}`, () => { | ||
@@ -32,3 +32,3 @@ const calls = []; | ||
const fakeHook = { | ||
intercept: (interceptor) => { | ||
intercept: interceptor => { | ||
calls.push(interceptor); | ||
@@ -38,6 +38,3 @@ } | ||
new MultiHook([fakeHook, fakeHook]).intercept("interceptor"); | ||
expect(calls).toEqual([ | ||
"interceptor", | ||
"interceptor" | ||
]); | ||
expect(calls).toEqual(["interceptor", "interceptor"]); | ||
}); | ||
@@ -47,3 +44,3 @@ it("should redirect withOptions", () => { | ||
const fakeHook = { | ||
withOptions: (options) => { | ||
withOptions: options => { | ||
calls.push(options); | ||
@@ -54,7 +51,7 @@ return { | ||
} | ||
} | ||
}; | ||
} | ||
}; | ||
const newHook = new MultiHook([fakeHook, fakeHook]).withOptions("options"); | ||
newHook.tap("options", "fn") | ||
newHook.tap("options", "fn"); | ||
expect(calls).toEqual([ | ||
@@ -61,0 +58,0 @@ "options", |
@@ -20,3 +20,3 @@ /* | ||
h1.tap("A", (a) => undefined); | ||
h1.tap("A", a => undefined); | ||
h2.tap("A", (a, b) => [a, b]); | ||
@@ -31,3 +31,3 @@ | ||
h1.tap("B", (a) => "ok" + a); | ||
h1.tap("B", a => "ok" + a); | ||
h2.tap("B", (a, b) => "wrong"); | ||
@@ -71,8 +71,6 @@ | ||
fn((err, result) => { | ||
if(err) | ||
reject(err); | ||
else | ||
resolve(result); | ||
if (err) reject(err); | ||
else resolve(result); | ||
}); | ||
}); | ||
} |
@@ -30,3 +30,3 @@ /* | ||
const mock1 = jest.fn(); | ||
h0.tap("B", mock1) | ||
h0.tap("B", mock1); | ||
@@ -71,3 +71,3 @@ h0.call(); | ||
it("should allow to intercept calls", () =>{ | ||
it("should allow to intercept calls", () => { | ||
const hook = new SyncHook(["arg1", "arg2"]); | ||
@@ -105,3 +105,3 @@ | ||
expect(mock0).toHaveBeenLastCalledWith(1, 2); | ||
}) | ||
}); | ||
}); |
@@ -14,39 +14,55 @@ /* | ||
describe("SyncHook", () => { | ||
it("should have to correct behavior", async () => { | ||
const tester = new HookTester((args) => new SyncHook(args)); | ||
it( | ||
"should have to correct behavior", | ||
async () => { | ||
const tester = new HookTester(args => new SyncHook(args)); | ||
const result = await tester.run(true); | ||
const result = await tester.run(true); | ||
expect(result).toMatchSnapshot(); | ||
}, 15000) | ||
}) | ||
expect(result).toMatchSnapshot(); | ||
}, | ||
15000 | ||
); | ||
}); | ||
describe("SyncBailHook", () => { | ||
it("should have to correct behavior", async () => { | ||
const tester = new HookTester((args) => new SyncBailHook(args)); | ||
it( | ||
"should have to correct behavior", | ||
async () => { | ||
const tester = new HookTester(args => new SyncBailHook(args)); | ||
const result = await tester.run(true); | ||
const result = await tester.run(true); | ||
expect(result).toMatchSnapshot(); | ||
}, 15000) | ||
}) | ||
expect(result).toMatchSnapshot(); | ||
}, | ||
15000 | ||
); | ||
}); | ||
describe("SyncWaterfallHook", () => { | ||
it("should have to correct behavior", async () => { | ||
const tester = new HookTester((args) => new SyncWaterfallHook(args)); | ||
it( | ||
"should have to correct behavior", | ||
async () => { | ||
const tester = new HookTester(args => new SyncWaterfallHook(args)); | ||
const result = await tester.run(true); | ||
const result = await tester.run(true); | ||
expect(result).toMatchSnapshot(); | ||
}, 15000) | ||
}) | ||
expect(result).toMatchSnapshot(); | ||
}, | ||
15000 | ||
); | ||
}); | ||
describe("SyncLoopHook", () => { | ||
it("should have to correct behavior", async () => { | ||
const tester = new HookTester((args) => new SyncLoopHook(args)); | ||
it( | ||
"should have to correct behavior", | ||
async () => { | ||
const tester = new HookTester(args => new SyncLoopHook(args)); | ||
const result = await tester.runForLoop(true); | ||
const result = await tester.runForLoop(true); | ||
expect(result).toMatchSnapshot(); | ||
}, 15000) | ||
}) | ||
expect(result).toMatchSnapshot(); | ||
}, | ||
15000 | ||
); | ||
}); |
@@ -20,3 +20,3 @@ /* | ||
t.hooks.myHook.call(); | ||
t.plugin("myHook", () => called += 10); | ||
t.plugin("myHook", () => (called += 10)); | ||
t.hooks.myHook.call(); | ||
@@ -46,9 +46,13 @@ expect(called).toEqual(12); | ||
const match = /^hookMap (.+)$/.exec(options.name); | ||
if(match) { | ||
t.hooks.hookMap.tap(match[1], options.fn.name || "unnamed compat plugin", options.fn); | ||
if (match) { | ||
t.hooks.hookMap.tap( | ||
match[1], | ||
options.fn.name || "unnamed compat plugin", | ||
options.fn | ||
); | ||
return true; | ||
} | ||
}) | ||
}); | ||
t.plugin("my-hook", () => called++); | ||
t.plugin("hookMap test", () => called += 10); | ||
t.plugin("hookMap test", () => (called += 10)); | ||
t.hooks.myHook.call(); | ||
@@ -61,2 +65,2 @@ expect(called).toEqual(1); | ||
}); | ||
}) | ||
}); |
@@ -32,3 +32,4 @@ /* | ||
let code = ""; | ||
code += `if(${i} < _results.length && ((_results.length = ${i + 1}), (_results[${i}] = { error: ${err} }), _checkDone())) {\n`; | ||
code += `if(${i} < _results.length && ((_results.length = ${i + | ||
1}), (_results[${i}] = { error: ${err} }), _checkDone())) {\n`; | ||
code += doneBreak(true); | ||
@@ -42,3 +43,4 @@ code += "} else {\n"; | ||
let code = ""; | ||
code += `if(${i} < _results.length && (${result} !== undefined && (_results.length = ${i + 1}), (_results[${i}] = { result: ${result} }), _checkDone())) {\n`; | ||
code += `if(${i} < _results.length && (${result} !== undefined && (_results.length = ${i + | ||
1}), (_results[${i}] = { result: ${result} }), _checkDone())) {\n`; | ||
code += doneBreak(true); | ||
@@ -52,3 +54,3 @@ code += "} else {\n"; | ||
let code = ""; | ||
if(i > 0) { | ||
if (i > 0) { | ||
code += `if(${i} >= _results.length) {\n`; | ||
@@ -59,3 +61,3 @@ code += done(); | ||
code += run(); | ||
if(i > 0) code += "}\n"; | ||
if (i > 0) code += "}\n"; | ||
return code; | ||
@@ -72,7 +74,2 @@ }, | ||
class AsyncParallelBailHook extends Hook { | ||
constructor(args) { | ||
super(args); | ||
this.call = this._call = undefined; | ||
} | ||
compile(options) { | ||
@@ -84,2 +81,6 @@ factory.setup(this, options); | ||
Object.defineProperties(AsyncParallelBailHook.prototype, { | ||
_call: { value: undefined, configurable: true, writable: true } | ||
}); | ||
module.exports = AsyncParallelBailHook; |
@@ -22,7 +22,2 @@ /* | ||
class AsyncParallelHook extends Hook { | ||
constructor(args) { | ||
super(args); | ||
this.call = this._call = undefined; | ||
} | ||
compile(options) { | ||
@@ -34,2 +29,6 @@ factory.setup(this, options); | ||
Object.defineProperties(AsyncParallelHook.prototype, { | ||
_call: { value: undefined, configurable: true, writable: true } | ||
}); | ||
module.exports = AsyncParallelHook; |
@@ -14,3 +14,6 @@ /* | ||
onError: (i, err, next, doneBreak) => onError(err) + doneBreak(true), | ||
onResult: (i, result, next) => `if(${result} !== undefined) {\n${onResult(result)};\n} else {\n${next()}}\n`, | ||
onResult: (i, result, next) => | ||
`if(${result} !== undefined) {\n${onResult( | ||
result | ||
)};\n} else {\n${next()}}\n`, | ||
onDone | ||
@@ -24,8 +27,2 @@ }); | ||
class AsyncSeriesBailHook extends Hook { | ||
constructor(args) { | ||
super(args); | ||
this.call = this._call = undefined; | ||
} | ||
compile(options) { | ||
@@ -37,2 +34,6 @@ factory.setup(this, options); | ||
Object.defineProperties(AsyncSeriesBailHook.prototype, { | ||
_call: { value: undefined, configurable: true, writable: true } | ||
}); | ||
module.exports = AsyncSeriesBailHook; |
@@ -22,7 +22,2 @@ /* | ||
class AsyncSeriesHook extends Hook { | ||
constructor(args) { | ||
super(args); | ||
this.call = this._call = undefined; | ||
} | ||
compile(options) { | ||
@@ -34,2 +29,6 @@ factory.setup(this, options); | ||
Object.defineProperties(AsyncSeriesHook.prototype, { | ||
_call: { value: undefined, configurable: true, writable: true } | ||
}); | ||
module.exports = AsyncSeriesHook; |
@@ -22,7 +22,2 @@ /* | ||
class AsyncSeriesLoopHook extends Hook { | ||
constructor(args) { | ||
super(args); | ||
this.call = this._call = undefined; | ||
} | ||
compile(options) { | ||
@@ -34,2 +29,6 @@ factory.setup(this, options); | ||
Object.defineProperties(AsyncSeriesLoopHook.prototype, { | ||
_call: { value: undefined, configurable: true, writable: true } | ||
}); | ||
module.exports = AsyncSeriesLoopHook; |
@@ -32,4 +32,4 @@ /* | ||
super(args); | ||
if(args.length < 1) throw new Error("Waterfall hooks must have at least one argument"); | ||
this.call = this._call = undefined; | ||
if (args.length < 1) | ||
throw new Error("Waterfall hooks must have at least one argument"); | ||
} | ||
@@ -43,2 +43,6 @@ | ||
Object.defineProperties(AsyncSeriesWaterfallHook.prototype, { | ||
_call: { value: undefined, configurable: true, writable: true } | ||
}); | ||
module.exports = AsyncSeriesWaterfallHook; |
112
lib/Hook.js
@@ -9,9 +9,9 @@ /* | ||
constructor(args) { | ||
if(!Array.isArray(args)) args = []; | ||
if (!Array.isArray(args)) args = []; | ||
this._args = args; | ||
this.taps = []; | ||
this.interceptors = []; | ||
this.call = this._call = this._createCompileDelegate("call", "sync"); | ||
this.promise = this._promise = this._createCompileDelegate("promise", "promise"); | ||
this.callAsync = this._callAsync = this._createCompileDelegate("callAsync", "async"); | ||
this.call = this._call; | ||
this.promise = this._promise; | ||
this.callAsync = this._callAsync; | ||
this._x = undefined; | ||
@@ -33,17 +33,10 @@ } | ||
_createCompileDelegate(name, type) { | ||
const lazyCompileHook = (...args) => { | ||
this[name] = this._createCall(type); | ||
return this[name](...args); | ||
}; | ||
return lazyCompileHook; | ||
} | ||
tap(options, fn) { | ||
if(typeof options === "string") | ||
options = { name: options }; | ||
if(typeof options !== "object" || options === null) | ||
throw new Error("Invalid arguments to tap(options: Object, fn: function)"); | ||
if (typeof options === "string") options = { name: options }; | ||
if (typeof options !== "object" || options === null) | ||
throw new Error( | ||
"Invalid arguments to tap(options: Object, fn: function)" | ||
); | ||
options = Object.assign({ type: "sync", fn: fn }, options); | ||
if(typeof options.name !== "string" || options.name === "") | ||
if (typeof options.name !== "string" || options.name === "") | ||
throw new Error("Missing name for tap"); | ||
@@ -55,8 +48,9 @@ options = this._runRegisterInterceptors(options); | ||
tapAsync(options, fn) { | ||
if(typeof options === "string") | ||
options = { name: options }; | ||
if(typeof options !== "object" || options === null) | ||
throw new Error("Invalid arguments to tapAsync(options: Object, fn: function)"); | ||
if (typeof options === "string") options = { name: options }; | ||
if (typeof options !== "object" || options === null) | ||
throw new Error( | ||
"Invalid arguments to tapAsync(options: Object, fn: function)" | ||
); | ||
options = Object.assign({ type: "async", fn: fn }, options); | ||
if(typeof options.name !== "string" || options.name === "") | ||
if (typeof options.name !== "string" || options.name === "") | ||
throw new Error("Missing name for tapAsync"); | ||
@@ -68,8 +62,9 @@ options = this._runRegisterInterceptors(options); | ||
tapPromise(options, fn) { | ||
if(typeof options === "string") | ||
options = { name: options }; | ||
if(typeof options !== "object" || options === null) | ||
throw new Error("Invalid arguments to tapPromise(options: Object, fn: function)"); | ||
if (typeof options === "string") options = { name: options }; | ||
if (typeof options !== "object" || options === null) | ||
throw new Error( | ||
"Invalid arguments to tapPromise(options: Object, fn: function)" | ||
); | ||
options = Object.assign({ type: "promise", fn: fn }, options); | ||
if(typeof options.name !== "string" || options.name === "") | ||
if (typeof options.name !== "string" || options.name === "") | ||
throw new Error("Missing name for tapPromise"); | ||
@@ -81,7 +76,6 @@ options = this._runRegisterInterceptors(options); | ||
_runRegisterInterceptors(options) { | ||
for(const interceptor of this.interceptors) { | ||
if(interceptor.register) { | ||
for (const interceptor of this.interceptors) { | ||
if (interceptor.register) { | ||
const newOptions = interceptor.register(options); | ||
if(newOptions !== undefined) | ||
options = newOptions; | ||
if (newOptions !== undefined) options = newOptions; | ||
} | ||
@@ -93,3 +87,4 @@ } | ||
withOptions(options) { | ||
const mergeOptions = opt => Object.assign({}, options, typeof opt === "string" ? { name: opt } : opt); | ||
const mergeOptions = opt => | ||
Object.assign({}, options, typeof opt === "string" ? { name: opt } : opt); | ||
@@ -101,4 +96,4 @@ // Prevent creating endless prototype chains | ||
newHook.tapAsync = (opt, fn) => base.tapAsync(mergeOptions(opt), fn), | ||
newHook.tap = (opt, fn) => base.tap(mergeOptions(opt), fn); | ||
(newHook.tapAsync = (opt, fn) => base.tapAsync(mergeOptions(opt), fn)), | ||
(newHook.tap = (opt, fn) => base.tap(mergeOptions(opt), fn)); | ||
newHook.tapPromise = (opt, fn) => base.tapPromise(mergeOptions(opt), fn); | ||
@@ -117,4 +112,4 @@ newHook._withOptions = options; | ||
this.interceptors.push(Object.assign({}, interceptor)); | ||
if(interceptor.register) { | ||
for(let i = 0; i < this.taps.length; i++) | ||
if (interceptor.register) { | ||
for (let i = 0; i < this.taps.length; i++) | ||
this.taps[i] = interceptor.register(this.taps[i]); | ||
@@ -133,26 +128,24 @@ } | ||
let before; | ||
if(typeof item.before === "string") | ||
before = new Set([item.before]); | ||
else if(Array.isArray(item.before)) { | ||
if (typeof item.before === "string") before = new Set([item.before]); | ||
else if (Array.isArray(item.before)) { | ||
before = new Set(item.before); | ||
} | ||
let stage = 0; | ||
if(typeof item.stage === "number") | ||
stage = item.stage; | ||
if (typeof item.stage === "number") stage = item.stage; | ||
let i = this.taps.length; | ||
while(i > 0) { | ||
while (i > 0) { | ||
i--; | ||
const x = this.taps[i]; | ||
this.taps[i+1] = x; | ||
this.taps[i + 1] = x; | ||
const xStage = x.stage || 0; | ||
if(before) { | ||
if(before.has(x.name)) { | ||
if (before) { | ||
if (before.has(x.name)) { | ||
before.delete(x.name); | ||
continue; | ||
} | ||
if(before.size > 0) { | ||
if (before.size > 0) { | ||
continue; | ||
} | ||
} | ||
if(xStage > stage) { | ||
if (xStage > stage) { | ||
continue; | ||
@@ -167,2 +160,27 @@ } | ||
function createCompileDelegate(name, type) { | ||
return function lazyCompileHook(...args) { | ||
this[name] = this._createCall(type); | ||
return this[name](...args); | ||
}; | ||
} | ||
Object.defineProperties(Hook.prototype, { | ||
_call: { | ||
value: createCompileDelegate("call", "sync"), | ||
configurable: true, | ||
writable: true | ||
}, | ||
_promise: { | ||
value: createCompileDelegate("promise", "promise"), | ||
configurable: true, | ||
writable: true | ||
}, | ||
_callAsync: { | ||
value: createCompileDelegate("callAsync", "async"), | ||
configurable: true, | ||
writable: true | ||
} | ||
}); | ||
module.exports = Hook; |
@@ -11,2 +11,3 @@ /* | ||
this.options = undefined; | ||
this._args = undefined; | ||
} | ||
@@ -16,21 +17,34 @@ | ||
this.init(options); | ||
switch(this.options.type) { | ||
let fn; | ||
switch (this.options.type) { | ||
case "sync": | ||
return new Function(this.args(), "\"use strict\";\n" + this.header() + this.content({ | ||
onError: err => `throw ${err};\n`, | ||
onResult: result => `return ${result};\n`, | ||
onDone: () => "", | ||
rethrowIfPossible: true | ||
})); | ||
fn = new Function( | ||
this.args(), | ||
'"use strict";\n' + | ||
this.header() + | ||
this.content({ | ||
onError: err => `throw ${err};\n`, | ||
onResult: result => `return ${result};\n`, | ||
onDone: () => "", | ||
rethrowIfPossible: true | ||
}) | ||
); | ||
break; | ||
case "async": | ||
return new Function(this.args({ | ||
after: "_callback" | ||
}), "\"use strict\";\n" + this.header() + this.content({ | ||
onError: err => `_callback(${err});\n`, | ||
onResult: result => `_callback(null, ${result});\n`, | ||
onDone: () => "_callback();\n" | ||
})); | ||
fn = new Function( | ||
this.args({ | ||
after: "_callback" | ||
}), | ||
'"use strict";\n' + | ||
this.header() + | ||
this.content({ | ||
onError: err => `_callback(${err});\n`, | ||
onResult: result => `_callback(null, ${result});\n`, | ||
onDone: () => "_callback();\n" | ||
}) | ||
); | ||
break; | ||
case "promise": | ||
let code = ""; | ||
code += "\"use strict\";\n"; | ||
code += '"use strict";\n'; | ||
code += "return new Promise((_resolve, _reject) => {\n"; | ||
@@ -53,4 +67,7 @@ code += "var _sync = true;\n"; | ||
code += "});\n"; | ||
return new Function(this.args(), code); | ||
fn = new Function(this.args(), code); | ||
break; | ||
} | ||
this.deinit(); | ||
return fn; | ||
} | ||
@@ -70,5 +87,10 @@ | ||
deinit() { | ||
this.options = undefined; | ||
this._args = undefined; | ||
} | ||
header() { | ||
let code = ""; | ||
if(this.needContext()) { | ||
if (this.needContext()) { | ||
code += "var _context = {};\n"; | ||
@@ -79,9 +101,9 @@ } else { | ||
code += "var _x = this._x;\n"; | ||
if(this.options.interceptors.length > 0) { | ||
if (this.options.interceptors.length > 0) { | ||
code += "var _taps = this.taps;\n"; | ||
code += "var _interceptors = this.interceptors;\n"; | ||
} | ||
for(let i = 0; i < this.options.interceptors.length; i++) { | ||
for (let i = 0; i < this.options.interceptors.length; i++) { | ||
const interceptor = this.options.interceptors[i]; | ||
if(interceptor.call) { | ||
if (interceptor.call) { | ||
code += `${this.getInterceptor(i)}.call(${this.args({ | ||
@@ -96,4 +118,3 @@ before: interceptor.context ? "_context" : undefined | ||
needContext() { | ||
for(const tap of this.options.taps) | ||
if(tap.context) return true; | ||
for (const tap of this.options.taps) if (tap.context) return true; | ||
return false; | ||
@@ -105,10 +126,12 @@ } | ||
let hasTapCached = false; | ||
for(let i = 0; i < this.options.interceptors.length; i++) { | ||
for (let i = 0; i < this.options.interceptors.length; i++) { | ||
const interceptor = this.options.interceptors[i]; | ||
if(interceptor.tap) { | ||
if(!hasTapCached) { | ||
if (interceptor.tap) { | ||
if (!hasTapCached) { | ||
code += `var _tap${tapIndex} = ${this.getTap(tapIndex)};\n`; | ||
hasTapCached = true; | ||
} | ||
code += `${this.getInterceptor(i)}.tap(${interceptor.context ? "_context, " : ""}_tap${tapIndex});\n`; | ||
code += `${this.getInterceptor(i)}.tap(${ | ||
interceptor.context ? "_context, " : "" | ||
}_tap${tapIndex});\n`; | ||
} | ||
@@ -118,9 +141,9 @@ } | ||
const tap = this.options.taps[tapIndex]; | ||
switch(tap.type) { | ||
switch (tap.type) { | ||
case "sync": | ||
if(!rethrowIfPossible) { | ||
if (!rethrowIfPossible) { | ||
code += `var _hasError${tapIndex} = false;\n`; | ||
code += "try {\n"; | ||
} | ||
if(onResult) { | ||
if (onResult) { | ||
code += `var _result${tapIndex} = _fn${tapIndex}(${this.args({ | ||
@@ -134,3 +157,3 @@ before: tap.context ? "_context" : undefined | ||
} | ||
if(!rethrowIfPossible) { | ||
if (!rethrowIfPossible) { | ||
code += "} catch(_err) {\n"; | ||
@@ -142,9 +165,9 @@ code += `_hasError${tapIndex} = true;\n`; | ||
} | ||
if(onResult) { | ||
if (onResult) { | ||
code += onResult(`_result${tapIndex}`); | ||
} | ||
if(onDone) { | ||
if (onDone) { | ||
code += onDone(); | ||
} | ||
if(!rethrowIfPossible) { | ||
if (!rethrowIfPossible) { | ||
code += "}\n"; | ||
@@ -155,13 +178,11 @@ } | ||
let cbCode = ""; | ||
if(onResult) | ||
cbCode += `(_err${tapIndex}, _result${tapIndex}) => {\n`; | ||
else | ||
cbCode += `_err${tapIndex} => {\n`; | ||
if (onResult) cbCode += `(_err${tapIndex}, _result${tapIndex}) => {\n`; | ||
else cbCode += `_err${tapIndex} => {\n`; | ||
cbCode += `if(_err${tapIndex}) {\n`; | ||
cbCode += onError(`_err${tapIndex}`); | ||
cbCode += "} else {\n"; | ||
if(onResult) { | ||
if (onResult) { | ||
cbCode += onResult(`_result${tapIndex}`); | ||
} | ||
if(onDone) { | ||
if (onDone) { | ||
cbCode += onDone(); | ||
@@ -178,10 +199,13 @@ } | ||
code += `var _hasResult${tapIndex} = false;\n`; | ||
code += `_fn${tapIndex}(${this.args({ | ||
code += `var _promise${tapIndex} = _fn${tapIndex}(${this.args({ | ||
before: tap.context ? "_context" : undefined | ||
})}).then(_result${tapIndex} => {\n`; | ||
})});\n`; | ||
code += `if (!_promise${tapIndex} || !_promise${tapIndex}.then)\n`; | ||
code += ` throw new Error('Tap function (tapPromise) did not return promise (returned ' + _promise${tapIndex} + ')');\n`; | ||
code += `_promise${tapIndex}.then(_result${tapIndex} => {\n`; | ||
code += `_hasResult${tapIndex} = true;\n`; | ||
if(onResult) { | ||
if (onResult) { | ||
code += onResult(`_result${tapIndex}`); | ||
} | ||
if(onDone) { | ||
if (onDone) { | ||
code += onDone(); | ||
@@ -199,23 +223,27 @@ } | ||
callTapsSeries({ onError, onResult, onDone, rethrowIfPossible }) { | ||
if(this.options.taps.length === 0) | ||
return onDone(); | ||
if (this.options.taps.length === 0) return onDone(); | ||
const firstAsync = this.options.taps.findIndex(t => t.type !== "sync"); | ||
const next = i => { | ||
if(i >= this.options.taps.length) { | ||
if (i >= this.options.taps.length) { | ||
return onDone(); | ||
} | ||
const done = () => next(i + 1); | ||
const doneBreak = (skipDone) => { | ||
if(skipDone) return ""; | ||
const doneBreak = skipDone => { | ||
if (skipDone) return ""; | ||
return onDone(); | ||
} | ||
}; | ||
return this.callTap(i, { | ||
onError: error => onError(i, error, done, doneBreak), | ||
onResult: onResult && ((result) => { | ||
return onResult(i, result, done, doneBreak); | ||
}), | ||
onDone: !onResult && (() => { | ||
return done(); | ||
}), | ||
rethrowIfPossible: rethrowIfPossible && (firstAsync < 0 || i < firstAsync) | ||
onResult: | ||
onResult && | ||
(result => { | ||
return onResult(i, result, done, doneBreak); | ||
}), | ||
onDone: | ||
!onResult && | ||
(() => { | ||
return done(); | ||
}), | ||
rethrowIfPossible: | ||
rethrowIfPossible && (firstAsync < 0 || i < firstAsync) | ||
}); | ||
@@ -227,7 +255,6 @@ }; | ||
callTapsLooping({ onError, onDone, rethrowIfPossible }) { | ||
if(this.options.taps.length === 0) | ||
return onDone(); | ||
if (this.options.taps.length === 0) return onDone(); | ||
const syncOnly = this.options.taps.every(t => t.type === "sync"); | ||
let code = ""; | ||
if(!syncOnly) { | ||
if (!syncOnly) { | ||
code += "var _looper = () => {\n"; | ||
@@ -239,5 +266,5 @@ code += "var _loopAsync = false;\n"; | ||
code += "_loop = false;\n"; | ||
for(let i = 0; i < this.options.interceptors.length; i++) { | ||
for (let i = 0; i < this.options.interceptors.length; i++) { | ||
const interceptor = this.options.interceptors[i]; | ||
if(interceptor.loop) { | ||
if (interceptor.loop) { | ||
code += `${this.getInterceptor(i)}.loop(${this.args({ | ||
@@ -254,4 +281,3 @@ before: interceptor.context ? "_context" : undefined | ||
code += "_loop = true;\n"; | ||
if(!syncOnly) | ||
code += "if(_loopAsync) _looper();\n"; | ||
if (!syncOnly) code += "if(_loopAsync) _looper();\n"; | ||
code += doneBreak(true); | ||
@@ -263,13 +289,15 @@ code += `} else {\n`; | ||
}, | ||
onDone: onDone && (() => { | ||
let code = ""; | ||
code += "if(!_loop) {\n"; | ||
code += onDone(); | ||
code += "}\n"; | ||
return code; | ||
}), | ||
onDone: | ||
onDone && | ||
(() => { | ||
let code = ""; | ||
code += "if(!_loop) {\n"; | ||
code += onDone(); | ||
code += "}\n"; | ||
return code; | ||
}), | ||
rethrowIfPossible: rethrowIfPossible && syncOnly | ||
}) | ||
}); | ||
code += "} while(_loop);\n"; | ||
if(!syncOnly) { | ||
if (!syncOnly) { | ||
code += "_loopAsync = true;\n"; | ||
@@ -282,5 +310,16 @@ code += "};\n"; | ||
callTapsParallel({ onError, onResult, onDone, rethrowIfPossible, onTap = (i, run) => run() }) { | ||
if(this.options.taps.length <= 1) { | ||
return this.callTapsSeries({ onError, onResult, onDone, rethrowIfPossible }) | ||
callTapsParallel({ | ||
onError, | ||
onResult, | ||
onDone, | ||
rethrowIfPossible, | ||
onTap = (i, run) => run() | ||
}) { | ||
if (this.options.taps.length <= 1) { | ||
return this.callTapsSeries({ | ||
onError, | ||
onResult, | ||
onDone, | ||
rethrowIfPossible | ||
}); | ||
} | ||
@@ -290,3 +329,3 @@ let code = ""; | ||
code += `var _counter = ${this.options.taps.length};\n`; | ||
if(onDone) { | ||
if (onDone) { | ||
code += "var _done = () => {\n"; | ||
@@ -296,36 +335,42 @@ code += onDone(); | ||
} | ||
for(let i = 0; i < this.options.taps.length; i++) { | ||
for (let i = 0; i < this.options.taps.length; i++) { | ||
const done = () => { | ||
if(onDone) | ||
return "if(--_counter === 0) _done();\n"; | ||
else | ||
return "--_counter;"; | ||
if (onDone) return "if(--_counter === 0) _done();\n"; | ||
else return "--_counter;"; | ||
}; | ||
const doneBreak = (skipDone) => { | ||
if(skipDone || !onDone) | ||
return "_counter = 0;\n"; | ||
else | ||
return "_counter = 0;\n_done();\n"; | ||
} | ||
const doneBreak = skipDone => { | ||
if (skipDone || !onDone) return "_counter = 0;\n"; | ||
else return "_counter = 0;\n_done();\n"; | ||
}; | ||
code += "if(_counter <= 0) break;\n"; | ||
code += onTap(i, () => this.callTap(i, { | ||
onError: error => { | ||
let code = ""; | ||
code += "if(_counter > 0) {\n"; | ||
code += onError(i, error, done, doneBreak); | ||
code += "}\n"; | ||
return code; | ||
}, | ||
onResult: onResult && ((result) => { | ||
let code = ""; | ||
code += "if(_counter > 0) {\n"; | ||
code += onResult(i, result, done, doneBreak); | ||
code += "}\n"; | ||
return code; | ||
}), | ||
onDone: !onResult && (() => { | ||
return done(); | ||
}), | ||
rethrowIfPossible | ||
}), done, doneBreak); | ||
code += onTap( | ||
i, | ||
() => | ||
this.callTap(i, { | ||
onError: error => { | ||
let code = ""; | ||
code += "if(_counter > 0) {\n"; | ||
code += onError(i, error, done, doneBreak); | ||
code += "}\n"; | ||
return code; | ||
}, | ||
onResult: | ||
onResult && | ||
(result => { | ||
let code = ""; | ||
code += "if(_counter > 0) {\n"; | ||
code += onResult(i, result, done, doneBreak); | ||
code += "}\n"; | ||
return code; | ||
}), | ||
onDone: | ||
!onResult && | ||
(() => { | ||
return done(); | ||
}), | ||
rethrowIfPossible | ||
}), | ||
done, | ||
doneBreak | ||
); | ||
} | ||
@@ -338,5 +383,5 @@ code += "} while(false);\n"; | ||
let allArgs = this._args; | ||
if(before) allArgs = [before].concat(allArgs); | ||
if(after) allArgs = allArgs.concat(after); | ||
if(allArgs.length === 0) { | ||
if (before) allArgs = [before].concat(allArgs); | ||
if (after) allArgs = allArgs.concat(after); | ||
if (allArgs.length === 0) { | ||
return ""; | ||
@@ -343,0 +388,0 @@ } else { |
@@ -20,3 +20,3 @@ /* | ||
const hook = this.get(key); | ||
if(hook !== undefined) { | ||
if (hook !== undefined) { | ||
return hook; | ||
@@ -26,3 +26,3 @@ } | ||
const interceptors = this._interceptors; | ||
for(let i = 0; i < interceptors.length; i++) { | ||
for (let i = 0; i < interceptors.length; i++) { | ||
newHook = interceptors[i].factory(key, newHook); | ||
@@ -35,5 +35,10 @@ } | ||
intercept(interceptor) { | ||
this._interceptors.push(Object.assign({ | ||
factory: (key, hook) => hook | ||
}, interceptor)); | ||
this._interceptors.push( | ||
Object.assign( | ||
{ | ||
factory: (key, hook) => hook | ||
}, | ||
interceptor | ||
) | ||
); | ||
} | ||
@@ -40,0 +45,0 @@ |
@@ -15,3 +15,3 @@ /* | ||
tap(options, fn) { | ||
for(const hook of this.hooks) { | ||
for (const hook of this.hooks) { | ||
hook.tap(options, fn); | ||
@@ -22,3 +22,3 @@ } | ||
tapAsync(options, fn) { | ||
for(const hook of this.hooks) { | ||
for (const hook of this.hooks) { | ||
hook.tapAsync(options, fn); | ||
@@ -29,3 +29,3 @@ } | ||
tapPromise(options, fn) { | ||
for(const hook of this.hooks) { | ||
for (const hook of this.hooks) { | ||
hook.tapPromise(options, fn); | ||
@@ -36,5 +36,4 @@ } | ||
isUsed() { | ||
for(const hook of this.hooks) { | ||
if(hook.isUsed()) | ||
return true; | ||
for (const hook of this.hooks) { | ||
if (hook.isUsed()) return true; | ||
} | ||
@@ -45,3 +44,3 @@ return false; | ||
intercept(interceptor) { | ||
for(const hook of this.hooks) { | ||
for (const hook of this.hooks) { | ||
hook.intercept(interceptor); | ||
@@ -48,0 +47,0 @@ } |
@@ -14,3 +14,6 @@ /* | ||
onError: (i, err) => onError(err), | ||
onResult: (i, result, next) => `if(${result} !== undefined) {\n${onResult(result)};\n} else {\n${next()}}\n`, | ||
onResult: (i, result, next) => | ||
`if(${result} !== undefined) {\n${onResult( | ||
result | ||
)};\n} else {\n${next()}}\n`, | ||
onDone, | ||
@@ -17,0 +20,0 @@ rethrowIfPossible |
@@ -0,0 +0,0 @@ /* |
@@ -0,0 +0,0 @@ /* |
@@ -33,3 +33,4 @@ /* | ||
super(args); | ||
if(args.length < 1) throw new Error("Waterfall hooks must have at least one argument"); | ||
if (args.length < 1) | ||
throw new Error("Waterfall hooks must have at least one argument"); | ||
} | ||
@@ -36,0 +37,0 @@ |
@@ -12,31 +12,37 @@ /* | ||
this._pluginCompat = new SyncBailHook(["options"]); | ||
this._pluginCompat.tap({ | ||
name: "Tapable camelCase", | ||
stage: 100 | ||
}, options => { | ||
options.names.add(options.name.replace(/[- ]([a-z])/g, str => str.substr(1).toUpperCase())); | ||
}); | ||
this._pluginCompat.tap({ | ||
name: "Tapable this.hooks", | ||
stage: 200 | ||
}, options => { | ||
let hook; | ||
for(const name of options.names) { | ||
hook = this.hooks[name]; | ||
if(hook !== undefined) { | ||
break; | ||
this._pluginCompat.tap( | ||
{ | ||
name: "Tapable camelCase", | ||
stage: 100 | ||
}, | ||
options => { | ||
options.names.add( | ||
options.name.replace(/[- ]([a-z])/g, (str, ch) => ch.toUpperCase()) | ||
); | ||
} | ||
); | ||
this._pluginCompat.tap( | ||
{ | ||
name: "Tapable this.hooks", | ||
stage: 200 | ||
}, | ||
options => { | ||
let hook; | ||
for (const name of options.names) { | ||
hook = this.hooks[name]; | ||
if (hook !== undefined) { | ||
break; | ||
} | ||
} | ||
if (hook !== undefined) { | ||
const tapOpt = { | ||
name: options.fn.name || "unnamed compat plugin", | ||
stage: options.stage || 0 | ||
}; | ||
if (options.async) hook.tapAsync(tapOpt, options.fn); | ||
else hook.tap(tapOpt, options.fn); | ||
return true; | ||
} | ||
} | ||
if(hook !== undefined) { | ||
const tapOpt = { | ||
name: options.fn.name || "unnamed compat plugin", | ||
stage: options.stage || 0 | ||
}; | ||
if(options.async) | ||
hook.tapAsync(tapOpt, options.fn); | ||
else | ||
hook.tap(tapOpt, options.fn); | ||
return true; | ||
} | ||
}); | ||
); | ||
} | ||
@@ -52,3 +58,3 @@ module.exports = Tapable; | ||
Tapable.prototype.plugin = util.deprecate(function plugin(name, fn) { | ||
if(Array.isArray(name)) { | ||
if (Array.isArray(name)) { | ||
name.forEach(function(name) { | ||
@@ -64,6 +70,8 @@ this.plugin(name, fn); | ||
}); | ||
if(!result) { | ||
throw new Error(`Plugin could not be registered at '${name}'. Hook was not found.\n` + | ||
"BREAKING CHANGE: There need to exist a hook at 'this.hooks'. " + | ||
"To create a compatiblity layer for this hook, hook into 'this._pluginCompat'."); | ||
if (!result) { | ||
throw new Error( | ||
`Plugin could not be registered at '${name}'. Hook was not found.\n` + | ||
"BREAKING CHANGE: There need to exist a hook at 'this.hooks'. " + | ||
"To create a compatibility layer for this hook, hook into 'this._pluginCompat'." | ||
); | ||
} | ||
@@ -73,5 +81,5 @@ }, "Tapable.plugin is deprecated. Use new API on `.hooks` instead"); | ||
Tapable.prototype.apply = util.deprecate(function apply() { | ||
for(var i = 0; i < arguments.length; i++) { | ||
for (var i = 0; i < arguments.length; i++) { | ||
arguments[i].apply(this); | ||
} | ||
}, "Tapable.apply is deprecated. Call apply on the plugin directly instead"); |
{ | ||
"name": "tapable", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"author": "Tobias Koppers @sokra", | ||
@@ -17,3 +17,4 @@ "description": "Just a little module for plugins.", | ||
"codecov": "^2.3.0", | ||
"jest": "^21.0.4" | ||
"jest": "^21.0.4", | ||
"prettier": "^1.13.2" | ||
}, | ||
@@ -30,3 +31,4 @@ "engines": { | ||
"test": "jest", | ||
"travis": "jest --coverage && codecov" | ||
"travis": "jest --coverage && codecov", | ||
"pretty": "prettier --write lib/*.js lib/__tests__/*.js" | ||
}, | ||
@@ -33,0 +35,0 @@ "jest": { |
@@ -122,2 +122,25 @@ # Tapable | ||
## Hook types | ||
Each hook can be tapped with one or several functions. How they are executed depends on the hook type: | ||
* Basic hook (without “Waterfall”, “Bail” or “Loop” in its name). This hook simply calls every function it’s tapped with in a row. | ||
* __Waterfall__. A waterfall hook also calls each tapped function in a row. Unlike the basic hook, it passes a return value from each function to the next function. | ||
* __Bail__. A bail hook allows exitting early. When any of the tapped function returns anything, the bail hook will stop executing the remaining ones. | ||
* __Loop__. TODO | ||
Additionally, hooks can be synchronous or asynchronous. To reflect this, there’re “Sync”, “AsyncSeries” and “AsyncParallel” hook classes: | ||
* __Sync__. A sync hooks can only be tapped with synchronous functions (using `myHook.tap()`). | ||
* __AsyncSeries__. Async-series hooks can be tapped with synchronous, callback-based and promise-based functions (using `myHook.tap()`, `myHook.tapAsync()` and `myHook.tapPromise()`). They call each async method in a row. | ||
* __AsyncParallel__. Async-parallel hooks can also be tapped with synchronous, callback-based and promise-based functions (using `myHook.tap()`, `myHook.tapAsync()` and `myHook.tapPromise()`). However, they run each async method in parallel. | ||
The hook type is reflected in its class name. E.g., `AsyncSeriesWaterfallHook` allows asynchronous functions and runs them in series passing each function’s return value into the next function. | ||
## Interception | ||
@@ -132,3 +155,3 @@ | ||
}, | ||
tap: (tapInfo) => { | ||
register: (tapInfo) => { | ||
// tapInfo = { type: "promise", name: "GoogleMapsPlugin", fn: ... } | ||
@@ -149,3 +172,34 @@ console.log(`${tapInfo.name} is doing it's job`); | ||
## Context | ||
Plugins and interceptors can opt-in to access an optional `context` object, which can be used to pass arbitrary values to subsequent plugins and interceptors. | ||
``` js | ||
myCar.hooks.accelerate.intercept({ | ||
context: true, | ||
tap: (context, tapInfo) => { | ||
// tapInfo = { type: "sync", name: "NoisePlugin", fn: ... } | ||
console.log(`${tapInfo.name} is doing it's job`); | ||
// `context` starts as an empty object if at least one plugin uses `context: true`. | ||
// If no plugins use `context: true`, then `context` is undefined. | ||
if (context) { | ||
// Arbitrary properties can be added to `context`, which plugins can then access. | ||
context.hasMuffler = true; | ||
} | ||
} | ||
}); | ||
myCar.hooks.accelerate.tap({ | ||
name: "NoisePlugin", | ||
context: true | ||
}, (context, newSpeed) => { | ||
if (context && context.hasMuffler) { | ||
console.log("Silence..."); | ||
} else { | ||
console.log("Vroom!"); | ||
} | ||
}); | ||
``` | ||
## HookMap | ||
@@ -152,0 +206,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2891
289
239470
7