Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vicary/flushable-set
A Set with a flush callback triggered by max size.
https://github.com/vicary/flushable-set
Last synced: 3 months ago
JSON representation
A Set with a flush callback triggered by max size.
- Host: GitHub
- URL: https://github.com/vicary/flushable-set
- Owner: vicary
- License: mit
- Created: 2024-03-10T02:36:46.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-05-19T15:16:33.000Z (8 months ago)
- Last Synced: 2024-09-14T00:23:15.775Z (4 months ago)
- Language: TypeScript
- Size: 13.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# FlushableSet
A drop-in replacement of the native Set with an optional max size and a flush
callback.## Usage
```typescript
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.```typescript
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](https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub)](https://github.com/sponsors/vicary)