npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@x-oasis/batchinator

v0.2.5

Published

batchinator function

Readme

@x-oasis/batchinator

Batches callback executions with configurable leading/trailing behavior. Similar to debounce/throttle but with more control over execution timing.

Inspired by React Native's Batchinator.

Installation

npm install @x-oasis/batchinator
# or
pnpm add @x-oasis/batchinator
# or
yarn add @x-oasis/batchinator

Usage

Basic Usage

import Batchinator from '@x-oasis/batchinator';

const callback = (message: string) => {
  console.log(message);
};

const batchinator = new Batchinator(callback, 100);

// Schedule multiple calls
batchinator.schedule('First');
batchinator.schedule('Second');
batchinator.schedule('Third');

// Only the last call ('Third') will execute after 100ms

With Leading Option

Execute immediately on the first call, then batch subsequent calls:

import Batchinator from '@x-oasis/batchinator';

const batchinator = new Batchinator(callback, 100, {
  leading: true,
  trailing: true
});

batchinator.schedule('First');  // Executes immediately
batchinator.schedule('Second');  // Batched
batchinator.schedule('Third');  // Batched
// After 100ms, 'Third' executes (trailing)

With Trailing: false

Only execute on the leading edge:

import Batchinator from '@x-oasis/batchinator';

const batchinator = new Batchinator(callback, 100, {
  leading: true,
  trailing: false
});

batchinator.schedule('First');  // Executes immediately
batchinator.schedule('Second');  // Ignored
batchinator.schedule('Third');  // Ignored
// No trailing execution

Flush Scheduled Task

Immediately execute the scheduled task:

import Batchinator from '@x-oasis/batchinator';

const batchinator = new Batchinator(callback, 100);

batchinator.schedule('First');
batchinator.schedule('Second');
batchinator.flush();  // Executes immediately with 'Second'

// Or flush with new arguments
batchinator.flush('Custom');

Dispose

Cancel the scheduled task and optionally execute it:

import Batchinator from '@x-oasis/batchinator';

const batchinator = new Batchinator(callback, 100);

batchinator.schedule('First');
batchinator.schedule('Second');

// Dispose and execute
batchinator.dispose();  // Executes with 'Second'

// Dispose without executing
batchinator.dispose({ abort: true });  // Cancels without executing

Check Schedule Status

Check if a task is currently scheduled:

import Batchinator from '@x-oasis/batchinator';

const batchinator = new Batchinator(callback, 100);

batchinator.schedule('First');
console.log(batchinator.inSchedule());  // true

// After execution or cancel
batchinator.flush();
console.log(batchinator.inSchedule());  // false

API

new Batchinator(callback, delayMS, options?)

Creates a new Batchinator instance.

Parameters

  • callback (Function): The function to batch.
  • delayMS (number): The delay in milliseconds before executing the batched callback.
  • options (Object, optional): Configuration options.
    • leading (boolean, default: false): Execute immediately on the first call.
    • trailing (boolean, default: true): Execute after the delay period.

Returns

Returns a new Batchinator instance.

Instance Methods

schedule(...args)

Schedule the callback execution with the given arguments. If called multiple times, only the last call's arguments will be used.

  • args (...any[]): Arguments to pass to the callback.

flush(...args)

Immediately execute the scheduled task. If arguments are provided, they will be used instead of stored arguments.

  • args (...any[], optional): Optional arguments to use instead of stored args.

dispose(options?)

Dispose the scheduled task. By default, executes the callback with stored arguments unless abort is true.

  • options (Object, optional): Configuration options.
    • abort (boolean, default: false): If true, cancel without executing callback.

inSchedule()

Check if a task is currently scheduled.

  • Returns: boolean - true if a task is scheduled, false otherwise.

Examples

React Native Interaction Manager

import Batchinator from '@x-oasis/batchinator';

// Batch UI updates to avoid blocking the main thread
const updateUI = (data: any) => {
  // Update UI with data
};

const batchinator = new Batchinator(updateUI, 16, {
  leading: false,
  trailing: true
});

// Multiple rapid updates
for (let i = 0; i < 100; i++) {
  batchinator.schedule({ index: i });
}
// Only the last update executes after 16ms

Form Input Batching

import Batchinator from '@x-oasis/batchinator';

const saveForm = (formData: any) => {
  // Save form data
  console.log('Saving:', formData);
};

const batchinator = new Batchinator(saveForm, 500, {
  leading: false,
  trailing: true
});

// User types in form
input.addEventListener('input', (e) => {
  batchinator.schedule({ value: e.target.value });
});
// Form saves 500ms after user stops typing

Immediate Execution with Batching

import Batchinator from '@x-oasis/batchinator';

const logMessage = (message: string) => {
  console.log(message);
};

const batchinator = new Batchinator(logMessage, 1000, {
  leading: true,
  trailing: true
});

batchinator.schedule('First');   // Executes immediately
batchinator.schedule('Second');  // Batched
batchinator.schedule('Third');   // Batched
// After 1000ms, 'Third' executes

Cleanup on Component Unmount

import Batchinator from '@x-oasis/batchinator';
import { useEffect } from 'react';

function MyComponent() {
  const batchinator = new Batchinator(handleUpdate, 100);

  useEffect(() => {
    // Use batchinator
    batchinator.schedule(data);

    // Cleanup on unmount
    return () => {
      batchinator.dispose({ abort: true });
    };
  }, []);
}

Differences from Debounce/Throttle

  • Batchinator: Batches multiple calls and executes with the last arguments. Supports leading/trailing execution.
  • Debounce: Delays execution until after a period of inactivity.
  • Throttle: Limits execution to at most once per period.

When to Use Batchinator

  • React Native Interaction Manager scenarios
  • Batching UI updates
  • Form auto-save (with leading: false, trailing: true)
  • Event handling where you want immediate feedback but batched processing

See Also

License

ISC