
cb-buffer

A minimalist NodeJS module for callback buffering.
:cloud: Installation
$ npm i --save cb-buffer
:clipboard: Example
const CallbackBuffering = require("cb-buffer");
var cb = new CallbackBuffering();
function getUniqueRandomNumberAsync(callback) {
if (cb.check(callback)) { return; }
setTimeout(() => {
debugger
cb.done(Math.random());
}, 1000);
}
getUniqueRandomNumberAsync(console.log);
getUniqueRandomNumberAsync(console.log);
getUniqueRandomNumberAsync(console.log);
getUniqueRandomNumberAsync(console.log);
setTimeout(() => {
getUniqueRandomNumberAsync(console.log);
}, 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