Skip to content

Commit 904e8bb

Browse files
committed
Bump to v1.0.1: Improve README example to use the batched-items-accumulator package
1 parent 17d4305 commit 904e8bb

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,21 @@ The `NonOverlappingRecurringTask` class provides the following getter methods to
5656
In many applications, MongoDB documents originate from sources such as message queues or user interactions. Instead of upserting each document individually - potentially causing excessive network load - it is common to **accumulate** them in memory before performing a periodic batch flush to the database.
5757

5858
The **non-overlapping execution guarantee** ensures that multiple batches are never upserted concurrently, helping to keep network bandwidth usage under control. This guarantee allows users to set a relatively low interval while focusing on their business logic without worrying about overlapping operations.
59+
60+
This example leverages the [batched-items-accumulator](https://www.npmjs.com/package/batched-items-accumulator) package to accumulate documents into fixed-size batches (number-of-documents wise). It abstracts batch management, allowing users to focus on application logic:
5961
```ts
6062
import {
6163
NonOverlappingRecurringTask,
6264
INonOverlappingRecurringTaskOptions
6365
} from 'non-overlapping-recurring-task';
66+
import { BatchedAccumulator } from 'batched-items-accumulator';
6467
import { Collection } from 'mongodb';
65-
import { BatchAccumulator } from './batch-accumulator';
6668

6769
const FLUSH_INTERVAL_MS = 5000;
70+
const BATCH_SIZE = 512;
6871

6972
class PeriodicDocumentFlusher<DocumentType> {
70-
private readonly _documentsAccumulator = new BatchAccumulator<DocumentType>();
73+
private readonly _documentsAccumulator = new BatchedAccumulator<DocumentType>(BATCH_SIZE);
7174
private readonly _recurringFlush: NonOverlappingRecurringTask<MongoError>;
7275

7376
/**
@@ -90,8 +93,8 @@ class PeriodicDocumentFlusher<DocumentType> {
9093
);
9194
}
9295

93-
public start(): Promise<boolean> {
94-
return this._recurringFlush.start();
96+
public async start(): Promise<void> {
97+
await this._recurringFlush.start();
9598
}
9699

97100
public async stop(): Promise<void> {
@@ -101,7 +104,7 @@ class PeriodicDocumentFlusher<DocumentType> {
101104

102105
public add(doc: DocumentType): void {
103106
// Accumulate documents in memory for batch processing.
104-
this._documentsAccumulator.addItem(doc);
107+
this._documentsAccumulator.accumulateItem(doc);
105108
}
106109

107110
private async _bulkUpsert(batch: DocumentType[]): Promise<void> {
@@ -117,7 +120,7 @@ class PeriodicDocumentFlusher<DocumentType> {
117120
* For brevity, this example focuses solely on the upsert process.
118121
*/
119122
private async _flushAccumulatedBatches(): Promise<void> {
120-
const batches: DocumentType[][] = this._documentsAccumulator.extractBatches();
123+
const batches: DocumentType[][] = this._documentsAccumulator.extractAccumulatedBatches();
121124
for (const batch of batches) {
122125
await this._bulkUpsert(batch);
123126
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "non-overlapping-recurring-task",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "A modern `setInterval` substitute tailored for asynchronous tasks, ensuring non-overlapping executions by skipping attempts if a previous execution is still in progress. Features execution status getters, graceful teardown, and a fixed delay between runs. The ability to gracefully await the completion of an ongoing execution makes it ideal for production apps demanding smooth resource cleanup.",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)