# atomic-batcher

> A simple batching function that allows you to atomically batch a series of operations.

Latest version **1.0.2** (published 2017-02-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install atomic-batcher
pnpm add atomic-batcher
yarn add atomic-batcher
bun add atomic-batcher
```

## 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.2 |
| Published | 2017-02-12 |
| First published | 2016-07-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Mathias Buus |
| Maintainers | mafintosh |

## Links

- npm: https://www.npmjs.com/package/atomic-batcher
- Repository: https://github.com/mafintosh/atomic-batcher
- Issues: https://github.com/mafintosh/atomic-batcher/issues
- npm.io page: https://npm.io/package/atomic-batcher

## Recent versions

- 1.0.2 (latest) — 2017-02-12
- 1.0.1 — 2017-01-26
- 1.0.0 — 2016-07-11

## README

# atomic-batcher

A simple batching function that allows you to atomically batch a series of operations. If you are looking for the same thing with a stream interface checkout [byte-stream](https://github.com/maxogden/byte-stream).

```
npm install atomic-batcher
```

[![build status](http://img.shields.io/travis/mafintosh/atomic-batcher.svg?style=flat)](http://travis-ci.org/mafintosh/atomic-batcher)

## Usage

``` js
var batcher = require('atomic-batcher')
var db = require('level')('some.db')

var batch = batcher(function work (ops, cb) {
  // only one batch will happen at the time
  console.log('Batching:', ops, '\n')
  db.batch(ops, cb)
})

batch({type: 'put', key: 'hello', value: 'world-1'})
batch({type: 'put', key: 'hello', value: 'world-2'})
batch({type: 'put', key: 'hello', value: 'world-3'})
batch({type: 'put', key: 'hi', value: 'hello'}, function () {
  console.log('Printing latest values:\n')
  db.get('hello', console.log) // returns world-3
  db.get('hi', console.log) // returns hello
})
```

Running the above example will print

```
Batching: [ { type: 'put', key: 'hello', value: 'world-1' } ]

Batching: [ { type: 'put', key: 'hello', value: 'world-2' },
  { type: 'put', key: 'hello', value: 'world-3' },
  { type: 'put', key: 'hi', value: 'hello' } ]

Printing latest values:

null 'world-3'
null 'hello'
```

## API

#### `var batch = batcher(worker)`

Create a new batching function. `worker` should be a function that accepts a batch and a callback, `(batch, cb)`.
Only one batch is guaranteed to be run at the time.

The `batch` function accepts a value or an array of values and a callback, `batch(value(s), cb)`. The callback is called when the batch containing the values have been run.

## License

MIT

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