calls-batch
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

Calls Batch

Execute calls in debounced batches, with pre/postflush hooks, useful for performance.

My initial use case for this was: I have a React application that will be re-rendered every time files in a particular folder changes, but there could be hundreds of those changes in 1s and that means hundreds of re-renders, so in order to avoid that the functions that should be called because of these changes are executed in batches and before each batch I'm pausing re-renders and after each batch I'm resuming re-renders.

Install

npm install --save calls-batch

Usage

import CallsBatch from 'calls-batch';

const batch = new CallsBatch ({
  preflush () {}, // Function to call before all the batched calls are executed
  postflush () {}, // Function to call before all the batched calls are executed
  wait: 100 // Debounce wait
})

function foo () {}

batch.add ( foo );
batch.add ( foo, [1] );
batch.add ( foo, [1, 2, 'foo'] );

const fooWrapped = batch.wrap ( foo ); // Returns a function which automatically adds a call to `method`, with the provided arguments, to the batch whenver called

fooWrapped ( 1 ); // Automatically add a call to `foo` with the provided arguments to the batch

// batch.flush (); // Force a flush immediately

API

new CallsBatch ( options )

Creates a new CallsBatch instance, the options object has the following shape:

{
  preflush?: Function,
  postflush?: Function,
  wait: number
}

batch.add ( method: Function, args?: any[] ): void

Add a function call to the batch, optionally passing an array of args that the passed method will be called with.

The call will be executed in a debounced manner, that is, as soon as no other batch.add call is performend in the following options.wait milliseconds all the queued calls will be executed.

batch.wrap ( method: Function ): Function

Returns a function which automatically adds a call to method, with the provided arguments, to the batch whenever called.

batch.flush (): Promise<void>

Force flushing the batched calls immediately.

License

MIT © Fabio Spampinato

Readme

Keywords

Package Sidebar

Install

npm i calls-batch

Weekly Downloads

5

Version

2.0.2

License

none

Unpacked Size

8.46 kB

Total Files

12

Last publish

Collaborators

  • fabiospampinato