batchjs
Version:
Batch processing framework for NodeJS
49 lines (48 loc) • 1.31 kB
JavaScript
import { SingleObjectDuplex } from "../interfaces/_index";
/**
* @class
* Class that allows you to count the number of chunks in a stream.
* @extends SingleObjectDuplex
* @template T
* @example
* ```typescript
* const stream:CountStream<string> = new CountStream({
* objectMode: true,
* });
*
* stream.write("data1");
* stream.write("data2");
* stream.write("data3");
* stream.end();
*
* stream.on("data", (chunk: number) => {
* console.log(``Received chunks: ${chunk}```);
* });
* ```
* ```shell
* >> Received chunks: 3
* ```
*/
export class CountStream extends SingleObjectDuplex {
result = 0;
/**
* @constructor
* @param {ObjectDuplexOptions} options - The options for the GroupBy.
*/
constructor(options) {
super(options, () => false);
}
/**
* Writes a chunk of data to the stream, groups it according to a specified function,
* and executes 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}
*/
_write(chunk, encoding, callback) {
this.result++;
callback();
}
}