Skip to main content

A drop-in replacement of the native Set with an optional max size and a flush callback.

Works with
This package works with Bun
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Browsers
JSR Score
100%
Published
2 weeks ago (0.1.4)

FlushableSet

A drop-in replacement of the native Set with an optional max size and a flush callback.

Usage

import { FlushableSet } from "@vicary/flushable-set";

const set = new FlushableSet(null, {
  maxSize: 3,
  onFlush() {
    console.log("Flushing items", Array.from(this));
  },
});

for (let i = 0; i < 10; i++) {
  set.add(i);
}

// Flushing items [0, 1, 2]
// Flushing items [3, 4, 5]
// Flushing items [6, 7, 8]
// Flushing items [9]

Asynchronous flush is supported with promise mutex, this comes in handy for database insertion from an external stream.

const chunk = new FlushableSet(null, {
  maxSize: 100,
  async onFlush() {
    await db.insertMany(Array.from(this));
  },
});

for await (const row of dataStream) {
  // Automatically flushes to database for every 100 rows
  await chunk.addAsync(row);
}

// Flush the remaining items
await chunk.flush();

Contributing

If you find a bug or would like to suggest a new feature, please open an issue or submit a pull request on GitHub.

License

FlushableSet is licensed under the MIT License. See the LICENSE file for more information.

Funding

If you find this project useful, please consider supporting it by donating to the author.

Donate

Built and signed on
GitHub Actions
View transparency log

Add Package

deno add @vicary/flushable-set

Import symbol

import * as mod from "@vicary/flushable-set";

Add Package

npx jsr add @vicary/flushable-set

Import symbol

import * as mod from "@vicary/flushable-set";

Add Package

yarn dlx jsr add @vicary/flushable-set

Import symbol

import * as mod from "@vicary/flushable-set";

Add Package

pnpm dlx jsr add @vicary/flushable-set

Import symbol

import * as mod from "@vicary/flushable-set";

Add Package

bunx jsr add @vicary/flushable-set

Import symbol

import * as mod from "@vicary/flushable-set";