@johngw/stream
Version:
Reactive programming tools using the WHATWG Streams API.
25 lines • 789 B
JavaScript
import { WritableReadablePair } from './WritableReadablePair.js';
/**
* Creates a {@see WritableReadablePair} that gives chunks to the `sink` when
* when `predicate` passes. It will pass **all** chunks to the readable side.
*
* @remarks
* A good way to signal that a sink is targeted to specific chunk types.
*
* @example
* ```
* myStream()
* .pipeThrough(tapWhen(isMyChunk, { write(myChunk) {} }))
* ```
*/
export function tapWhen(predicate, sink) {
return new WritableReadablePair({
...sink,
async write(chunk, readableController, writableController) {
if (predicate(chunk))
await sink.write?.(chunk, writableController);
readableController.enqueue(chunk);
},
});
}
//# sourceMappingURL=tapWhen.js.map