batchjs
Version:
Batch processing framework for NodeJS
59 lines (58 loc) • 1.77 kB
JavaScript
import { DiscardingSingleObjectDuplex } from "../interfaces/_index";
/**
* @class
* Class that allows you to emit only the first chunk in a stream and discard the rest.
* @extends DiscardingSingleObjectDuplex
* @template T
* @example
* ```typescript
* const stream:FirstStream<string> = new FirstStream({
* objectMode: true,
* matcher: (chunk: string) => chunk.length > 2
* });
*
* stream.write("first");
* stream.write("second");// Discarded
* stream.write("third");// Discarded
* stream.end();
*
* stream.on("data", (chunk: string) => {
* console.log(``Pushed chunk: ${chunk}```);
* });
* stream.on("discard", (chunk: string) => {
* console.log(``Discarded chunk: ${chunk}```);
* });
* ```
* ```shell
* >> Pushed chunk: first
* >> Discarded chunk: second
* >> Discarded chunk: third
* ```
*/
export class FirstStream extends DiscardingSingleObjectDuplex {
/**
* @constructor
* @param {ObjectDuplexOptions} options - The options for the FirstStream.
*/
constructor(options) {
super(options, () => true);
}
/**
* A method to write data to the stream, save first chunk and discard the rest, and execute the callback.
*
* @param {T} chunk - The data chunk to write to the stream.
* @param {BufferEncoding} encoding - The encoding of the data.
* @param {TransformCallback} callback - The callback function to be executed after writing the data.
* @return {void} This function does not return anything.
*/
_write(chunk, encoding, callback) {
if (this.result === undefined) {
this.result = chunk;
this._flush();
}
else {
this.emit("discard", chunk);
}
callback();
}
}