@@ -38,27 +38,29 @@ export class MultiTabWorkerBroker {
3838 private workerReadyResolver : ( ( ) => void ) | null = null ;
3939 // Map rewritten JSONRPC IDs back to original IDs
4040 private rewrittenIdMap = new Map < string , string | number > ( ) ;
41- // Sequence number to ensure each rewritten ID is unique even if original IDs are reused
42- private rewriteSequence = 0 ;
4341 // Track all active connections (reader/writer pairs)
4442 private connections = new Map < string , { reader : MultiTabMessageReader ; writer : MultiTabMessageWriter } > ( ) ;
4543 private nextConnectionId = 0 ;
44+ private shouldDebug = false ;
4645
4746 constructor (
4847 private readonly lockName : string ,
4948 private readonly makeWorker : ( ) => Worker | Promise < Worker > ,
50- options ?: { timeout ?: number ; onStateChange ?: ( state : { isLeader : boolean } ) => void }
49+ options ?: { timeout ?: number ; onStateChange ?: ( state : { isLeader : boolean } ) => void ; debug ?: boolean }
5150 ) {
5251 this . brokerId = generateBrokerId ( ) ;
5352 this . timeout = options ?. timeout ?? 20_000 ;
5453 this . onStateChange = options ?. onStateChange ;
54+ this . shouldDebug = options ?. debug ?? false ;
5555 }
5656
5757 /** Central debug logging function */
5858 private debug ( message : string , ...args : any [ ] ) : void {
59- const role = this . isLeader ? "LEADER" : "FOLLOWER" ;
60- const prefix = `[MTB:${ this . brokerId . slice ( 0 , 20 ) } :${ role } ]` ;
61- console . debug ( prefix , message , ...args ) ;
59+ if ( this . shouldDebug ) {
60+ const role = this . isLeader ? "LEADER" : "FOLLOWER" ;
61+ const prefix = `[MTB:${ this . brokerId . slice ( 0 , 20 ) } :${ role } ]` ;
62+ console . debug ( prefix , message , ...args ) ;
63+ }
6264 }
6365
6466 /** Central error logging function */
@@ -158,8 +160,10 @@ export class MultiTabWorkerBroker {
158160 if ( ! acquiredLock ) {
159161 // We're a follower - the leader exists elsewhere
160162 this . isLeader = false ;
163+
161164 // Set up abort controller for the follower's lock request
162165 this . lockAbortController = new AbortController ( ) ;
166+
163167 // Try to acquire the lock in the background for failover
164168 this . waitForLockAndBecomeLeader ( ) ;
165169
@@ -241,12 +245,11 @@ export class MultiTabWorkerBroker {
241245 /** Rewrite a JSONRPC ID to make it globally unique */
242246 private rewriteId ( originalId : string | number ) : string {
243247 // Include a sequence number to handle ID reuse
244- const rewrittenId = `${ this . brokerId } :${ this . rewriteSequence ++ } : ${ originalId } ` ;
248+ const rewrittenId = `${ this . brokerId } :${ originalId } ` ;
245249 this . rewrittenIdMap . set ( rewrittenId , originalId ) ;
246250 return rewrittenId ;
247251 }
248252
249- /** Un-rewrite a JSONRPC ID back to its original form */
250253 private unrewriteId ( rewrittenId : string | number ) : { originalId : string | number ; isOurs : boolean } {
251254 // Check if this ID is one of ours
252255 if ( typeof rewrittenId === "string" && rewrittenId . startsWith ( `${ this . brokerId } :` ) ) {
@@ -259,7 +262,6 @@ export class MultiTabWorkerBroker {
259262 return { originalId : rewrittenId , isOurs : false } ;
260263 }
261264
262- /** Rewrite message IDs if present */
263265 private rewriteMessage ( message : Message ) : Message {
264266 // Only rewrite IDs for client->worker requests (messages with a method)
265267 // Responses (no method) must keep the original ID so the worker can match them
@@ -276,7 +278,6 @@ export class MultiTabWorkerBroker {
276278 return message ;
277279 }
278280
279- /** Un-rewrite message IDs if present and they belong to us */
280281 private unrewriteMessage ( message : Message ) : { message : Message ; isOurs : boolean } {
281282 if ( message && typeof message === "object" && "id" in message && message . id !== undefined ) {
282283 const { originalId, isOurs } = this . unrewriteId ( message . id as string | number ) ;
@@ -323,22 +324,19 @@ export class MultiTabWorkerBroker {
323324 }
324325 }
325326
326- /** Emit a message to all active connections */
327327 private emitToAllConnections ( message : Message ) : void {
328328 for ( const { reader } of this . connections . values ( ) ) {
329329 reader . _emitMessage ( message ) ;
330330 }
331331 }
332332
333- /** Emit an error to all active connections */
334333 private emitErrorToAllConnections ( error : Error ) : void {
335334 for ( const { reader, writer } of this . connections . values ( ) ) {
336335 reader . _emitError ( error ) ;
337336 writer . _emitError ( error ) ;
338337 }
339338 }
340339
341- /** Emit close event to all active connections */
342340 private emitCloseToAllConnections ( ) : void {
343341 for ( const { reader, writer } of this . connections . values ( ) ) {
344342 reader . _emitClose ( ) ;
@@ -512,11 +510,6 @@ export class MultiTabWorkerBroker {
512510 this . onStateChange ?.( { isLeader : false } ) ;
513511 }
514512 }
515-
516- /** Dispose the broker */
517- dispose ( ) : void {
518- void this . stop ( ) ;
519- }
520513}
521514
522515class MultiTabMessageReader implements MessageReader {
@@ -550,7 +543,6 @@ class MultiTabMessageReader implements MessageReader {
550543 return disposable ;
551544 }
552545
553- /** Internal method to emit a message to all listeners */
554546 _emitMessage ( message : Message ) : void {
555547 if ( ! this . disposed ) {
556548 if ( this . hasListener ) {
@@ -566,14 +558,12 @@ class MultiTabMessageReader implements MessageReader {
566558 }
567559 }
568560
569- /** Internal method to emit an error to all listeners */
570561 _emitError ( error : Error ) : void {
571562 if ( ! this . disposed ) {
572563 this . errorEmitter . fire ( error ) ;
573564 }
574565 }
575566
576- /** Internal method to emit close event to all listeners */
577567 _emitClose ( ) : void {
578568 if ( ! this . disposed ) {
579569 this . closeEmitter . fire ( ) ;
@@ -621,19 +611,16 @@ class MultiTabMessageWriter implements MessageWriter {
621611 // Not needed for broker communication
622612 }
623613
624- /** Internal method to set the write handler */
625614 _setWriteHandler ( handler : ( message : Message ) => Promise < void > ) : void {
626615 this . writeHandler = handler ;
627616 }
628617
629- /** Internal method to emit an error to all listeners */
630618 _emitError ( error : Error ) : void {
631619 if ( ! this . disposed ) {
632620 this . errorEmitter . fire ( [ error , undefined , undefined ] ) ;
633621 }
634622 }
635623
636- /** Internal method to emit close event to all listeners */
637624 _emitClose ( ) : void {
638625 if ( ! this . disposed ) {
639626 this . closeEmitter . fire ( ) ;
0 commit comments