@mblaney/holster 1.0.11 → 1.0.12

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mblaney/holster",
3
- "version": "1.0.11",
3
+ "version": "1.0.12",
4
4
  "description": "Holster is a service that allows synchronising data between devices using Node, Deno, Bun or the browser.",
5
5
  "type": "module",
6
6
  "main": "index.js",
package/src/wire.js CHANGED
@@ -514,6 +514,46 @@ const Wire = opt => {
514
514
  // Client-side throttling state
515
515
  let clientThrottled = false
516
516
  let throttleUntil = 0
517
+ let messageQueue = []
518
+ let queueProcessor = null
519
+
520
+ const processQueue = () => {
521
+ if (messageQueue.length === 0) {
522
+ queueProcessor = null
523
+ return
524
+ }
525
+
526
+ const now = Date.now()
527
+ if (clientThrottled && now < throttleUntil) {
528
+ // Still throttled, schedule next queue check
529
+ queueProcessor = setTimeout(
530
+ processQueue,
531
+ Math.min(1000, throttleUntil - now),
532
+ )
533
+ return
534
+ }
535
+
536
+ // Throttle period ended, resume processing
537
+ if (clientThrottled && now >= throttleUntil) {
538
+ console.log(
539
+ `[CLIENT-THROTTLED] Throttle period ended, resuming queue processing`,
540
+ )
541
+ clientThrottled = false
542
+ throttleUntil = 0
543
+ }
544
+
545
+ // Process next message in queue
546
+ const data = messageQueue.shift()
547
+ sendToPeers(data)
548
+
549
+ // Schedule processing of next message if queue not empty
550
+ if (messageQueue.length > 0) {
551
+ // Add small delay between messages to avoid immediate re-throttling
552
+ queueProcessor = setTimeout(processQueue, 100)
553
+ } else {
554
+ queueProcessor = null
555
+ }
556
+ }
517
557
 
518
558
  const sendToPeers = data => {
519
559
  peers.forEach(peer => {
@@ -539,22 +579,23 @@ const Wire = opt => {
539
579
  }
540
580
 
541
581
  const send = data => {
542
- // Check if client is throttled before sending
582
+ // Check if client is throttled or queue is active
543
583
  const now = Date.now()
544
- if (clientThrottled && now < throttleUntil) {
545
- const delay = throttleUntil - now
584
+ if (clientThrottled || messageQueue.length > 0) {
585
+ // Add message to queue instead of sending immediately
586
+ messageQueue.push(data)
546
587
  console.log(
547
- `[CLIENT-THROTTLED] Delaying message for ${delay}ms, throttled until ${new Date(throttleUntil).toLocaleTimeString()}`,
588
+ `[CLIENT-THROTTLED] Queuing message (queue length: ${messageQueue.length})`,
548
589
  )
549
- setTimeout(() => sendToPeers(data), delay)
590
+
591
+ // Start queue processor if not already running
592
+ if (!queueProcessor) {
593
+ queueProcessor = setTimeout(processQueue, 100)
594
+ }
550
595
  return
551
- } else if (clientThrottled && now >= throttleUntil) {
552
- console.log(
553
- `[CLIENT-THROTTLED] Throttle period ended, resuming normal operation`,
554
- )
555
- clientThrottled = false
556
- throttleUntil = 0
557
596
  }
597
+
598
+ // Normal case - send immediately
558
599
  sendToPeers(data)
559
600
  }
560
601
  if (!(opt.peers instanceof Array)) {