bulkony

0.0.1 • Public • Published

Bulkony

A bulks-generator tool

exmplae

const { BulksGenerator } = require("bulkony");

const bulker = new BulksGenerator({bulkSize: 3});

bulker.onBulk((bulk) => console.log(bulk));

for(let i=1; i<=10; i++)
    bulker.push(i);
bulker.flush();

will output:

[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[10]

API

BulksGenerator

Constructor

const bulker = new BulksGenerator(options, onBulk);
options

| Option Name | Type | Description | |-|-|-|-| | bulkSize | number | The number of elements in the bulkwill cause the bulker to dispatch|

onBulk

A callback thet will be called on each bulk. For example:

const onBulk = (bulk) => console.log(bulk);

.onBulk(callback)

Set the OnBulk with new callback.

.push(item)

Process a new item

.flush()

Pack the left items and send as a bulk, even if it has'nt reached bulkSize.

.iterate(generator)

Gets an iterable prop and handles it's iteration through bulker.

const bulker = new BulksGenerator({bulkSize: 3});
bulker.onBulk(console.log);
bulker.iterate([1,2,3,4,5]);

will output:

[1, 2, 3]
[4, 5]

AsyncBulksGenerator

same as BulksGenerator but supports async/await mechanism

async onBulk

async function onBulk(bulk) {
    console.log(bulk);
    await new Promise(r => setTimeout(r, 500));
}
const bulksGenerator = new AsyncBulksGenerator({ bulkSize: 3 }, onBulk);
await bulksGenerator.iterate([1,2,3,4,5]);
console.log("complete");

will outout:

[1, 2, 3]
// waits 500ms
[4, 5]
// waits 500ms
complete

async .push() and .flush()

async function onBulk(bulk) {
    console.log(bulk);
    await new Promise(r => setTimeout(r, 500));
}
const bulksGenerator = new AsyncBulksGenerator({ bulkSize: 3 }, onBulk);
for(let i of [1,2,3,4])
    await bulksGenerator.push(i);
await bulksGenerator.flush();
console.log("complete");

will output:

[1, 2, 3]
// waits 500ms
[4, 5]
// waits 500ms
complete

Usage with generators

const range = function*(from, to){
    for(let i=from; i<=to; i++)
        yield i;
}

const bulker = new BulksGenerator({bulkSize: 3});
bulker.onBulk(console.log);
bulker.iterate(range(10,20));

will output:

[ 10, 11, 12 ]
[ 13, 14, 15 ]
[ 16, 17, 18 ]
[ 19, 20 ]

* AsyncBulksGenerator does supports AsyncGenerator objects

Readme

Keywords

none

Package Sidebar

Install

npm i bulkony

Weekly Downloads

0

Version

0.0.1

License

ISC

Unpacked Size

6.95 kB

Total Files

8

Last publish

Collaborators

  • daniel.dt232