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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@raphaabreu/message-batcher

v1.0.1

Published

Message batcher

Downloads

1,978

Readme

MessageBatcher

The MessageBatcher class is a utility that helps you batch messages and process them in groups. It is useful when you want to reduce the number of operations or API calls by aggregating messages and processing them together.

Features

  • Add messages to a queue.
  • Flush messages in batch when the queue reaches a certain size.
  • Start an interval timer to flush messages periodically.
  • Stop the interval timer.
  • Stop the interval timer and flush remaining messages.

Usage

First, install the package:

npm i @raphaabreu/message-batcher

Then import the MessageBatcher class:

import { MessageBatcher } from '@raphaabreu/message-batcher';

Creating a MessageBatcher instance

To create a new MessageBatcher instance, you need to specify the batchSize and a callback function. The batchSize determines the maximum number of messages that can be processed in one batch. The callback function is called with an array of messages when the batch is ready to be processed.

const batchSize = 5;
const callback = async (messages: string[]) => {
  // Process messages here
};

const messageBatcher = new MessageBatcher<string>(batchSize, callback);

Adding messages

You can add messages to the queue using the add method:

messageBatcher.add('message1');
messageBatcher.add(['message2', 'message3']);

The add method accepts a single message or an array of messages.

Starting the interval timer

To start the interval timer, call the start method and pass the maxBatchIntervalMs parameter:

const maxBatchIntervalMs = 10000; // 10 seconds
messageBatcher.start(maxBatchIntervalMs);

The maxBatchIntervalMs determines the maximum interval between message batch processing.

Stopping the interval timer

To stop the interval timer, call the stop method:

messageBatcher.stop();

Stopping the interval timer and flushing remaining messages

To stop the interval timer and flush any remaining messages in the queue, call the stopAndFlush method:

await messageBatcher.stopAndFlush();

Flushing messages manually

You can also manually flush messages using the flush method:

await messageBatcher.flush();

This can be useful if you want to process messages immediately, without waiting for the interval timer to trigger.

Tests

To run the provided unit tests just execute npm run tests.

License

MIT License

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Support

If you have any issues or questions, please open an issue on the project repository.