You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A synchronous hook that allows exiting early. Every tapped function is called in order until one returns a non-`undefined` value; that value becomes the result of `call` and the remaining taps are skipped. If all taps return `undefined`, `call` returns `undefined`.
202
211
203
212
- Tap methods: `tap`
204
213
- Call methods: `call`
205
214
215
+
```js
216
+
consthook=newSyncBailHook(["value"]);
217
+
hook.tap("Negative", (v) => (v <0?"negative":undefined));
218
+
hook.tap("Zero", (v) => (v ===0?"zero":undefined));
219
+
hook.tap("Positive", (v) =>"positive");
220
+
221
+
hook.call(-1); // "negative" (later taps skipped)
222
+
hook.call(5); // "positive"
223
+
```
224
+
206
225
### SyncWaterfallHook
207
226
208
227
A synchronous hook that threads a value through its tapped functions. The first argument passed to `call` is forwarded to the first tap. If a tap returns a non-`undefined` value it replaces that argument for the next tap; otherwise the previous value is kept. `call` returns the value after the last tap has run. Additional arguments (if any) are passed through unchanged.
209
228
210
229
- Tap methods: `tap`
211
230
- Call methods: `call`
212
231
232
+
```js
233
+
consthook=newSyncWaterfallHook(["value"]);
234
+
hook.tap("Double", (v) => v *2);
235
+
hook.tap("PlusOne", (v) => v +1);
236
+
237
+
hook.call(3); // 7 -> (3 * 2) + 1
238
+
```
239
+
213
240
### SyncLoopHook
214
241
215
242
A synchronous hook that keeps re-running its taps until all of them return `undefined` for a full pass. Whenever a tap returns a non-`undefined` value the hook restarts from the first tap. `call` returns `undefined`.
216
243
217
244
- Tap methods: `tap`
218
245
- Call methods: `call`
219
246
247
+
```js
248
+
consthook=newSyncLoopHook(["state"]);
249
+
let retries =3;
250
+
hook.tap("Retry", () => {
251
+
if (retries-->0) returntrue; // non-undefined restarts the loop
252
+
});
253
+
hook.tap("Log", () =>console.log("pass"));
254
+
255
+
hook.call({});
256
+
// pass (runs once all taps return undefined)
257
+
```
258
+
220
259
### AsyncParallelHook
221
260
222
261
An asynchronous hook that runs all of its tapped functions in parallel. It completes when every tap has signalled completion (sync return, callback, or promise resolution). Return values and resolution values are ignored; `callAsync`'s callback is invoked with no result and `promise()` resolves to `undefined`. If any tap errors, the error is forwarded and remaining taps still complete but their results are discarded.
223
262
224
263
- Tap methods: `tap`, `tapAsync`, `tapPromise`
225
264
- Call methods: `callAsync`, `promise`
226
265
266
+
```js
267
+
consthook=newAsyncParallelHook(["source"]);
268
+
hook.tapPromise("Fetch", (src) =>fetch(src));
269
+
hook.tapAsync("Log", (src, cb) => {
270
+
console.log("fetching", src);
271
+
cb();
272
+
});
273
+
274
+
awaithook.promise("https://example.com");
275
+
```
276
+
227
277
### AsyncParallelBailHook
228
278
229
279
Like `AsyncParallelHook`, but designed to bail out with a result. All tapped functions start in parallel; the first tap to produce a non-`undefined` value (synchronously, via its callback, or by resolving its promise) determines the hook’s result. The remaining taps continue to run but their results are ignored. Order is determined by tap registration order: an earlier tap’s value takes precedence over a later one’s, even if the later one finishes first.
// First non-undefined result (by registration order) wins.
291
+
```
292
+
234
293
### AsyncSeriesHook
235
294
236
295
An asynchronous hook that runs tapped functions one after another, waiting for each to finish before starting the next. Results are ignored; `callAsync`'s callback is invoked with no result and `promise()` resolves to `undefined`. The first error aborts the series.
An asynchronous series hook that allows exiting early. Tapped functions run one after another; as soon as one produces a non-`undefined` value, that value becomes the hook’s result and the remaining taps are skipped.
An asynchronous series hook that loops. Tapped functions run one after another; whenever a tap produces a non-`undefined` value the hook restarts from the first tap. The hook completes once a full pass yields `undefined` from every tap. The result is always `undefined`.
251
327
252
328
- Tap methods: `tap`, `tapAsync`, `tapPromise`
253
329
- Call methods: `callAsync`, `promise`
254
330
331
+
```js
332
+
consthook=newAsyncSeriesLoopHook(["job"]);
333
+
hook.tapPromise("Process", async (job) => {
334
+
constmore=awaitjob.step();
335
+
if (more) returntrue; // restart the loop
336
+
});
337
+
338
+
awaithook.promise(job);
339
+
```
340
+
255
341
### AsyncSeriesWaterfallHook
256
342
257
343
An asynchronous series hook that threads a value through its taps. The first argument passed to `callAsync` / `promise` is forwarded to the first tap. A tap's non-`undefined` return / callback / resolution value replaces it for the next tap; `undefined` keeps the previous value. The hook completes with the value after the last tap.
0 commit comments