Home Reference Source
public class | source

Batcher

Extends:

events~EventEmitter → Batcher

Batcher provides a way to batch a lot of inputs into groups. It has two mechanisms to do this. The first mechanism is to set the 'batchSize' option to the maximum desired size of a single batch. When the buffer has reached this size it will flush the batch. The second option is to set the 'interval' option. If this option is set the batch will be flushed when either maximum batch size is reached or the interval has passed.

Example:

const {Batcher} = require('@scriptinator/winston-influx');

let batch = new Batcher({interval: 10});

batch.on('flush', function(data) {
  console.log('Here is a large array of data!');
  console.log(data);
});

for(let i = 0; i < 5000; i++) {
  batch.write(i);
}

Constructor Summary

Public Constructor
public

Create a new Batcher and, if the 'interval' option is supplied, start the interval timer.

Member Summary

Private Members
private
private

If the interval options is set, this contains a reference for the set interval

private

Method Summary

Public Methods
public

close()

Stop the interval timer.

public

flush()

Flush the current batch.

public

write(item: *)

Write an item to the batch.

Public Constructors

public constructor(options: IBatcherOptions) source

Create a new Batcher and, if the 'interval' option is supplied, start the interval timer.

Params:

NameTypeAttributeDescription
options IBatcherOptions
  • optional
  • default: {batchSize: 5000, interval: 0}

Private Members

private _batch: Array source

private _interval: number source

If the interval options is set, this contains a reference for the set interval

private _options: IBatcherOptions source

Public Methods

public close() source

Stop the interval timer.

public flush() source

Flush the current batch. If the batch is empty, this method does nothing.

public write(item: *) source

Write an item to the batch.

Params:

NameTypeAttributeDescription
item *