File tree Expand file tree Collapse file tree 3 files changed +42
-0
lines changed
Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -47,6 +47,11 @@ export class CrossTabClient<
4747 */
4848 role : 'follower' | 'leader'
4949
50+ /**
51+ * Start web socket reconnection.
52+ */
53+ forceConnect ( ) : void
54+
5055 on (
5156 event : 'add' | 'clean' | 'preadd' ,
5257 listener : ClientActionListener < Action >
Original file line number Diff line number Diff line change @@ -98,6 +98,16 @@ export class CrossTabClient extends Client {
9898 }
9999 }
100100
101+ forceConnect ( ) {
102+ if ( this . state === 'disconnected' ) {
103+ if ( this . role === 'leader' ) {
104+ this . node . connection . connect ( )
105+ }
106+ } else {
107+ sendToTabs ( this , 'connect' , Date . now ( ) )
108+ }
109+ }
110+
101111 getClientId ( ) {
102112 let key = storageKey ( this , 'client' )
103113 if ( ! this . isLocalStorage ) {
@@ -161,6 +171,8 @@ export class CrossTabClient extends Client {
161171 )
162172 this . node . emitter . emit ( 'error' , err )
163173 }
174+ } else if ( e . key === storageKey ( this , 'connect' ) ) {
175+ if ( this . role === 'leader' ) this . forceConnect ( )
164176 }
165177 }
166178
Original file line number Diff line number Diff line change @@ -508,6 +508,31 @@ it('notifies other tabs on user change', () => {
508508 expect ( users ) . toEqual ( [ '20' ] )
509509} )
510510
511+ it ( 'forces connection from follower through leader' , ( ) => {
512+ localStorage . setItem = ( name , value ) => {
513+ emitStorage ( name , value )
514+ }
515+
516+ client = createClient ( )
517+ let follower = createClient ( )
518+
519+ client . start ( )
520+ giveLock ( )
521+ follower . start ( )
522+
523+ client . state = 'disconnected'
524+ let connect = spyOn ( client . node . connection , 'connect' )
525+
526+ client . forceConnect ( )
527+ expect ( connect . callCount ) . toEqual ( 1 )
528+
529+ connect . callCount = 0
530+ follower . forceConnect ( )
531+ expect ( connect . callCount ) . toEqual ( 1 )
532+
533+ follower . destroy ( )
534+ } )
535+
511536it ( 'sends event on user changing in other tab' , ( ) => {
512537 client = createClient ( )
513538 let users : string [ ] = [ ]
You can’t perform that action at this time.
0 commit comments