@@ -35,6 +35,7 @@ import {
3535
3636import { ReliableChannelEvent , ReliableChannelEvents } from "./events.js" ;
3737import { MissingMessageRetriever } from "./missing_message_retriever.js" ;
38+ import { RandomTimeout } from "./random_timeout.js" ;
3839import { RetryManager } from "./retry_manager.js" ;
3940import { SyncStatus } from "./sync_status.js" ;
4041
@@ -151,8 +152,7 @@ export class ReliableChannel<
151152 ) => AsyncGenerator < Promise < T | undefined > [ ] > ;
152153
153154 private eventListenerCleanups : Array < ( ) => void > = [ ] ;
154- private readonly syncMinIntervalMs : number ;
155- private syncTimeout : ReturnType < typeof setTimeout > | undefined ;
155+ private syncRandomTimeout : RandomTimeout ;
156156 private sweepInBufInterval : ReturnType < typeof setInterval > | undefined ;
157157 private readonly sweepInBufIntervalMs : number ;
158158 private processTaskTimeout : ReturnType < typeof setTimeout > | undefined ;
@@ -207,8 +207,11 @@ export class ReliableChannel<
207207 }
208208 }
209209
210- this . syncMinIntervalMs =
211- options ?. syncMinIntervalMs ?? DEFAULT_SYNC_MIN_INTERVAL_MS ;
210+ this . syncRandomTimeout = new RandomTimeout (
211+ options ?. syncMinIntervalMs ?? DEFAULT_SYNC_MIN_INTERVAL_MS ,
212+ 2 ,
213+ this . sendSyncMessage . bind ( this )
214+ ) ;
212215
213216 this . sweepInBufIntervalMs =
214217 options ?. sweepInBufIntervalMs ?? DEFAULT_SWEEP_IN_BUF_INTERVAL_MS ;
@@ -244,7 +247,7 @@ export class ReliableChannel<
244247
245248 /**
246249 * Emit events when the channel is aware of missing message.
247- * Note that "syncd " may mean some messages are irretrievably lost.
250+ * Note that "synced " may mean some messages are irretrievably lost.
248251 * Check the emitted data for details.
249252 *
250253 * @emits [[StatusEvents]]
@@ -592,18 +595,11 @@ export class ReliableChannel<
592595 void this . restartSync ( 2 ) ;
593596 } , timeoutMs ) ;
594597 }
598+ this . syncRandomTimeout . restart ( multiplier ) ;
595599 }
596600
597601 private stopSync ( ) : void {
598- if ( this . syncTimeout ) {
599- clearTimeout ( this . syncTimeout ) ;
600- this . syncTimeout = undefined ;
601- }
602- }
603-
604- // Used to enable overriding when testing
605- private random ( ) : number {
606- return Math . random ( ) ;
602+ this . syncRandomTimeout . stop ( ) ;
607603 }
608604
609605 private safeSendEvent < T extends ReliableChannelEvent > (
0 commit comments