# batch-data-runner

> Collect data to do the batch operation

Latest version **1.0.4** (published 2020-06-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install batch-data-runner
pnpm add batch-data-runner
yarn add batch-data-runner
bun add batch-data-runner
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2020-06-08 |
| First published | 2020-01-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 12.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | C-ZW |
| Maintainers | c-zw |
| Keywords | batch data, batch process, collect data, module, node, typescript |

## Links

- npm: https://www.npmjs.com/package/batch-data-runner
- Repository: https://github.com/C-ZW/batch-data-runner
- Issues: https://github.com/C-ZW/batch-data-runner/issues
- npm.io page: https://npm.io/package/batch-data-runner

## Dependencies (1)

- [@types/node](https://npm.io/package/@types/node.md) ^12.12.7

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.4 (latest) — 2020-06-08
- 1.0.3 — 2020-06-08
- 1.0.2 — 2020-06-08
- 1.0.1 — 2020-03-22
- 1.0.0 — 2020-01-12

## README

# Batch Data Runner

Collect data to do the batch operation, when buffer size is greather than limit or interval timer is triggerd.

## Installation
> npm install batch-data-runner

## Usage
```
// 1. If timeInterval = -1, mean no timeInterval.
// 2. If sizeLimit = -1, mean no size limit.
constructor(timeInterval: number, sizeLimit: number, callback: (data: T[]) => void)

add(data: T) 
on(event: 'error' | 'result', listener: (...args: any[]) => void)
```

## Examples
> Summation example

### Set time interval and size limit both
```ts
// run every 100 milisecond or size greater than 2.
let batchRunner = new BatchDataRunner<number>(100, 2, (data) => {
    return data.reduce((prev, cur) => prev + cur);
});
```

### Set time Interval only
```ts
// run every 100 milisecond.
let batchRunner = new BatchDataRunner<number>(100, -1, (data) => {
    return data.reduce((prev, cur) => prev + cur);
});
```

### Set size limit only
```ts
// run when size greater than 2.
let batchRunner = new BatchDataRunner<number>(-1, 2, (data) => {
    return data.reduce((prev, cur) => prev + cur);
});
```

### Add data 
```ts
batchRunner.add(1)
batchRunner.add(2)
batchRunner.add(3)
batchRunner.add(4)
batchRunner.add(5)
batchRunner.add(6)
batchRunner.add(7)
```

### Clear runner
```ts
batchRunner.exit();
```

### Result Handle
```ts
batchRunner.on('result', (result) => {
    console.log(result);
})
```

### Error Handle
```ts
batchRunner.on('error', (err) => {
    console.error(err);
});
```

### Output
```ts
3
7
11
```

### Async Task
> Add async await key word
```ts
let batchRunner = new BatchDataRunner<number>(-1, 2, async (data) => {
    return data.reduce((prev, cur) => prev + cur);
});

batchRunner.on('result', async (result) => {
    console.log(await result);
})
```

---
_Source: https://npm.io/package/batch-data-runner · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
