New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

cb-buffer

Package Overview
Dependencies
Maintainers
3
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cb-buffer

A minimalist NodeJS module for callback buffering.

  • 2.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
357
decreased by-24.36%
Maintainers
3
Weekly downloads
 
Created
Source

cb-buffer

cb-buffer

Patreon PayPal AMA Version Downloads Get help on Codementor

A minimalist NodeJS module for callback buffering.

:cloud: Installation

$ npm i --save cb-buffer

:clipboard: Example

// Dependencies
const CallbackBuffering = require("cb-buffer");

// Create a new callback buffer
var cb = new CallbackBuffering();

// Callbacks a random unique number after 1 sec
function getUniqueRandomNumberAsync(callback) {
    if (cb.check(callback)) { return; }
    setTimeout(() => {
        debugger
        cb.done(Math.random());
    }, 1000);
}

// Request the unique number few times.
// It should be unique, and generated once
getUniqueRandomNumberAsync(console.log);
getUniqueRandomNumberAsync(console.log);
getUniqueRandomNumberAsync(console.log);
getUniqueRandomNumberAsync(console.log);
// ... after one second
// => 0.3639475910458714
// => 0.3639475910458714
// => 0.3639475910458714
// => 0.3639475910458714

// After one second (after the random number is found)
// we request it again
setTimeout(() => {
    getUniqueRandomNumberAsync(console.log);
    // => 0.3639475910458714
}, 1000);

:memo: Documentation

CbBuffer()

Creates a new instance of CbBuffer.

The instance will contain methods documented below and the following fields:

  • buffer (Array): An array of functions to be called.
  • waiting (Boolean): A flag representing the state of the buffer.
  • is_done (Boolean): A flag representing the done state (is done or not).
  • args (Array): The callback function arguments.
Return
  • CbBuffer The CbBuffer instance.

check(fn)

Use this function to append the new function and return if needed:

if (cb.check(callback)) { return; }
Params
  • Function fn: The callback function.
Return
  • Boolean true if thw async function was called already.

call(fn)

Calls the provided function with the callback arguments.

Params
  • Function fn: The function to call.

clear()

CLears the callback array.

undone()

Resets the internal data.

done()

Calls all the functions from the buffer.

:yum: How to contribute

Have an idea? Found a bug? See how to contribute.

:moneybag: Donations

Another way to support the development of my open-source modules is to set up a recurring donation, via Patreon. :rocket:

PayPal donations are appreciated too! Each dollar helps.

Thanks! :heart:

:dizzy: Where is this library used?

If you are using this library in one of your projects, add it in this list. :sparkles:

:scroll: License

MIT © Ionică Bizău

Keywords

FAQs

Package last updated on 26 Dec 2016

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc