File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -104,6 +104,7 @@ class _ClientImpl {
104104 this . debug = debug ;
105105 this . gameStateOverride = null ;
106106 this . subscribers = { } ;
107+ this . _running = false ;
107108
108109 this . reducer = CreateGameReducer ( {
109110 game : this . game ,
@@ -320,6 +321,8 @@ class _ClientImpl {
320321
321322 start ( ) {
322323 this . transport . connect ( ) ;
324+ this . notifySubscribers ( ) ;
325+ this . _running = true ;
323326
324327 if (
325328 process . env . NODE_ENV !== 'production' &&
@@ -341,6 +344,7 @@ class _ClientImpl {
341344
342345 stop ( ) {
343346 this . transport . disconnect ( ) ;
347+ this . _running = false ;
344348
345349 if ( this . _debugPanel != null ) {
346350 this . _debugPanel . $destroy ( ) ;
@@ -352,7 +356,10 @@ class _ClientImpl {
352356 const id = Object . keys ( this . subscribers ) . length ;
353357 this . subscribers [ id ] = fn ;
354358 this . transport . subscribe ( ( ) => this . notifySubscribers ( ) ) ;
355- fn ( this . getState ( ) ) ;
359+
360+ if ( this . _running || ! this . multiplayer ) {
361+ fn ( this . getState ( ) ) ;
362+ }
356363
357364 // Return a handle that allows the caller to unsubscribe.
358365 return ( ) => {
Original file line number Diff line number Diff line change @@ -660,6 +660,31 @@ describe('subscribe', () => {
660660 client . transport . callback ( ) ;
661661 expect ( fn ) . toHaveBeenCalled ( ) ;
662662 } ) ;
663+
664+ describe ( 'multiplayer' , ( ) => {
665+ test ( 'subscribe before start' , ( ) => {
666+ const fn = jest . fn ( ) ;
667+ const client = Client ( {
668+ game : { } ,
669+ multiplayer : { local : true } ,
670+ } ) ;
671+ client . subscribe ( fn ) ;
672+ expect ( fn ) . not . toBeCalled ( ) ;
673+ client . start ( ) ;
674+ expect ( fn ) . toBeCalled ( ) ;
675+ } ) ;
676+
677+ test ( 'subscribe after start' , ( ) => {
678+ const fn = jest . fn ( ) ;
679+ const client = Client ( {
680+ game : { } ,
681+ multiplayer : { local : true } ,
682+ } ) ;
683+ client . start ( ) ;
684+ client . subscribe ( fn ) ;
685+ expect ( fn ) . toBeCalled ( ) ;
686+ } ) ;
687+ } ) ;
663688} ) ;
664689
665690test ( 'override game state' , ( ) => {
You can’t perform that action at this time.
0 commit comments