state-buffer

Manage in-flight application state changes.
Installation
$ npm install state-buffer
Usage
const buffer = require('state-buffer')
const b = buffer(end => {
end(err, res)
})
b(data, (err, res) => {
if (err) return console.error('uh oh, error state')
console.log('all good!')
})
Retries
state-buffer doesn't retry when locked. Here's an example retry engine:
const queue = require('state-buffer')
const q = queue(queueHandler)
const buffer = []
q.on('overflow', val => buffer.push(val))
q.on('finish', () => {
if (!buffer.length) return
q(buffer.shift())
})
API
b = buffer(cb(end))
Create a state buffer that executes a function when called. end must be
called when done to unlock the buffer.
b(data, cb)
Pass data into the state buffer, calling the callback once done. The state
buffer is considered locked while data is in flight, and will emit an
'overflow' event if multiple writes occur.
b.on(event, cb)
Listen for events.
locked = b.locked
Return whether or not the state buffer is locked.
Events
error error occurred
start started working
finish finished working
success finished successfully
overflow received data while locked
Why?
Optimistic updates within your application state are dangerous if rollbacks
aren't dealth with properly. state-buffer manages these updates in a
non-destructive way while also taking care of race-conditions. This approach is
inspired by how
Facebook's Relay handles
mutation persistance.
License
MIT