react-broadcast-sync 1.5.0 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [1.5.1](https://github.com/IdanShalem/react-broadcast-sync/compare/v1.5.0...v1.5.1) (2025-07-03)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * added readme for batching ([568a52c](https://github.com/IdanShalem/react-broadcast-sync/commit/568a52c4d23443a1e4c2a0c2ab10a6c618c68529))
7
+
1
8
  # [1.5.0](https://github.com/IdanShalem/react-broadcast-sync/compare/v1.4.0...v1.5.0) (2025-07-03)
2
9
 
3
10
 
package/README.md CHANGED
@@ -228,20 +228,24 @@ interface BroadcastOptions {
228
228
  namespace?: string; // Channel namespace for isolation
229
229
  deduplicationTTL?: number; // Time in ms to keep message IDs for deduplication (default: 5 minutes)
230
230
  cleanupDebounceMs?: number; // Debounce time in ms for cleanup operations (default: 0)
231
+ batchingDelayMs?: number; // Delay in ms to batch outgoing messages (default: 20). If > 0, messages are batched and sent together.
232
+ excludedBatchMessageTypes?: string[]; // Message types to always send immediately, never batched (default: []).
231
233
  }
232
234
  ```
233
235
 
234
236
  #### Default Values
235
237
 
236
- | Option | Default Value | Description |
237
- | ------------------- | ------------- | ----------------------------------- |
238
- | `sourceName` | `undefined` | Auto-generated if not provided |
239
- | `cleaningInterval` | `1000` | 1 second between cleanup runs |
240
- | `keepLatestMessage` | `false` | Keep all messages by default |
241
- | `registeredTypes` | `[]` | Accept all message types by default |
242
- | `namespace` | `''` | No namespace by default |
243
- | `deduplicationTTL` | `300000` | 5 minutes (5 _ 60 _ 1000 ms) |
244
- | `cleanupDebounceMs` | `0` | No debounce by default |
238
+ | Option | Default Value | Description |
239
+ | --------------------------- | ------------- | ----------------------------------- |
240
+ | `sourceName` | `undefined` | Auto-generated if not provided |
241
+ | `cleaningInterval` | `1000` | 1 second between cleanup runs |
242
+ | `keepLatestMessage` | `false` | Keep all messages by default |
243
+ | `registeredTypes` | `[]` | Accept all message types by default |
244
+ | `namespace` | `''` | No namespace by default |
245
+ | `deduplicationTTL` | `300000` | 5 minutes (5 _ 60 _ 1000 ms) |
246
+ | `cleanupDebounceMs` | `0` | No debounce by default |
247
+ | `batchingDelayMs` | `20` | Batch delay in ms (0 = off) |
248
+ | `excludedBatchMessageTypes` | `[]` | Types never batched |
245
249
 
246
250
  #### Return Value
247
251
 
@@ -484,6 +488,40 @@ function TabStatus() {
484
488
  - Implement debouncing for rapid state changes
485
489
  - Consider using `expirationDuration` for temporary messages
486
490
 
491
+ #### Batching Mechanism
492
+
493
+ **Batching** allows you to group multiple outgoing messages and send them together in a single post to the BroadcastChannel. This can significantly reduce the number of cross-tab events, improve performance, and avoid flooding the channel when many messages are sent in rapid succession (e.g., during fast typing or bulk updates).
494
+
495
+ - **batchingDelayMs**: If set to a value greater than 0 (default: 20ms), outgoing messages are collected for up to this delay and then sent as an array. If 0 or negative, batching is disabled and all messages are sent immediately.
496
+ - **excludedBatchMessageTypes**: An array of message types that should always be sent immediately, even if batching is enabled. Use this for urgent or high-priority messages (e.g., 'alert', 'sync-now').
497
+
498
+ **How it works:**
499
+
500
+ - When batching is enabled, calls to `postMessage` within the batching window are buffered and sent as a batch (array of messages) after the delay.
501
+ - On the receiving side, the hook automatically handles both single messages and batches (arrays). If you listen to the channel directly, always check if `Array.isArray(event.data)`.
502
+ - If the tab unmounts or the channel closes, any unsent batched messages are flushed immediately.
503
+
504
+ **Why batching matters:**
505
+
506
+ - Reduces the number of events and improves efficiency, especially for high-frequency updates.
507
+ - Prevents message storms that can occur when many updates happen in a short time.
508
+ - Lets you control which messages are always sent immediately for real-time needs.
509
+
510
+ **Example:**
511
+
512
+ ```tsx
513
+ const { postMessage } = useBroadcastChannel('my-channel', {
514
+ batchingDelayMs: 50, // Batch messages for up to 50ms
515
+ excludedBatchMessageTypes: ['alert'], // Always send 'alert' immediately
516
+ });
517
+
518
+ // These will be batched if sent within 50ms
519
+ postMessage('edit', { field: 'a', value: 1 });
520
+ postMessage('edit', { field: 'b', value: 2 });
521
+ // This will be sent immediately
522
+ postMessage('alert', { message: 'Something happened!' });
523
+ ```
524
+
487
525
  ### Memory Management
488
526
 
489
527
  - Clear messages when they're no longer needed using `clearReceivedMessages` / `clearSentMessages`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-broadcast-sync",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
4
4
  "description": "A lightweight React hook and provider for syncing state across browser tabs using BroadcastChannel API.",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",