batch-do

Do batched tasks easily

Usage no npm install needed!

<script type="module">
  import batchDo from 'https://cdn.skypack.dev/batch-do';
</script>

README

batch-do

Do batched tasks easily

Features

  • Relatively fast
  • Single micro-task batching
  • ESM & CJS compatible

How it works

Installation

We recommend install via npm because of it's cache and flat node modules tree

npm i batch-do

then you able to import to Node.js/Browser easily

// Node.js
const batch = require("batch-do");
const { createContext } = batch;

// ES6
import batch, { createContext } from "batch-do";

Usage

const [state: number, setState: (value: number) => void] = React.useState(0);
batch(() => {
  setState(state + 1);
});

Documentation

batch

batch(() => {
  console.log("log 1");
}, ctx?);
batch(() => {
  console.log("log 2");
}, ctx?);
// log 1
// log 2

Arguments list

  • ctx - Context list

createContext

const ctx = createContext(
  resolveBatchs,
  pendingResolve,
  awaitBatch,
  maxCallsPerBatch
);

Arguments list

  • resolveBatchs - Function to apply batched functions and frees up current batch pending list
  • pendingResolve - Function to resolve apply batchs function
  • awaitBatch - Merge all async batches into single call or ordered
  • maxCallsPerBatch - Argument to enable max limit for batches

License

MIT