Socket
Book a DemoInstallSign in
Socket

state-buffer

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

state-buffer

state-buffer

Source
npmnpm
Version
0.0.0
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

state-buffer

NPM version build status Test coverage Downloads js-standard-style

Manage in-flight application state changes.

Installation

$ npm install state-buffer

Usage

const buffer = require('state-buffer')

const b = buffer(end => {
  // do stuff
  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

FAQs

Package last updated on 02 Jun 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts