99import { MatrixClient , SyncState } from "matrix-js-sdk/src/matrix" ;
1010import { EventEmitter } from "events" ;
1111
12+ import { MatrixClientPeg } from "../MatrixClientPeg" ;
1213import { ActionPayload } from "../dispatcher/payloads" ;
1314import { IDestroyable } from "../utils/IDestroyable" ;
1415import { Action } from "../dispatcher/actions" ;
1516import { MatrixDispatcher } from "../dispatcher/dispatcher" ;
1617
1718export abstract class ReadyWatchingStore extends EventEmitter implements IDestroyable {
18- private static instances : ReadyWatchingStore [ ] = [ ] ;
19- protected _matrixClient : MatrixClient | null = null ;
19+ protected matrixClient : MatrixClient | null = null ;
2020 private dispatcherRef : string | null = null ;
2121
22- public static set matrixClient ( client : MatrixClient ) {
23- for ( const instance of ReadyWatchingStore . instances ) {
24- instance . start ( client ) ;
25- }
26- }
27-
2822 public constructor ( protected readonly dispatcher : MatrixDispatcher ) {
2923 super ( ) ;
30-
31- this . dispatcherRef = this . dispatcher . register ( this . onAction ) ;
32-
33- ReadyWatchingStore . instances . push ( this ) ;
34- }
35-
36- public get matrixClient ( ) : MatrixClient | null {
37- return this . _matrixClient ;
3824 }
3925
40- public async start ( matrixClient : MatrixClient | null ) : Promise < void > {
41- const oldClient = this . _matrixClient ;
42- this . _matrixClient = matrixClient ;
26+ public async start ( ) : Promise < void > {
27+ this . dispatcherRef = this . dispatcher . register ( this . onAction ) ;
4328
44- if ( oldClient !== matrixClient ) {
45- await this . onNotReady ( ) ;
46- }
29+ // MatrixClientPeg can be undefined in tests because of circular dependencies with other stores
30+ const matrixClient = MatrixClientPeg ?. get ( ) ;
4731 if ( matrixClient ) {
32+ this . matrixClient = matrixClient ;
4833 await this . onReady ( ) ;
4934 }
5035 }
@@ -53,10 +38,8 @@ export abstract class ReadyWatchingStore extends EventEmitter implements IDestro
5338 return this . matrixClient ; // for external readonly access
5439 }
5540
56- // XXX: This method is intended only for use in tests.
57- public async useUnitTestClient ( cli : MatrixClient ) : Promise < void > {
58- this . _matrixClient = cli ;
59- await this . onReady ( ) ;
41+ public useUnitTestClient ( cli : MatrixClient ) : void {
42+ this . matrixClient = cli ;
6043 }
6144
6245 public destroy ( ) : void {
@@ -91,13 +74,13 @@ export abstract class ReadyWatchingStore extends EventEmitter implements IDestro
9174 if ( this . matrixClient ) {
9275 await this . onNotReady ( ) ;
9376 }
94- this . _matrixClient = payload . matrixClient ;
77+ this . matrixClient = payload . matrixClient ;
9578 await this . onReady ( ) ;
9679 }
9780 } else if ( payload . action === "on_client_not_viable" || payload . action === Action . OnLoggedOut ) {
9881 if ( this . matrixClient ) {
9982 await this . onNotReady ( ) ;
100- this . _matrixClient = null ;
83+ this . matrixClient = null ;
10184 }
10285 }
10386 } ;
0 commit comments