9
9
import { MatrixClient , SyncState } from "matrix-js-sdk/src/matrix" ;
10
10
import { EventEmitter } from "events" ;
11
11
12
+ import { MatrixClientPeg } from "../MatrixClientPeg" ;
12
13
import { ActionPayload } from "../dispatcher/payloads" ;
13
14
import { IDestroyable } from "../utils/IDestroyable" ;
14
15
import { Action } from "../dispatcher/actions" ;
15
16
import { MatrixDispatcher } from "../dispatcher/dispatcher" ;
16
17
17
18
export abstract class ReadyWatchingStore extends EventEmitter implements IDestroyable {
18
- private static instances : ReadyWatchingStore [ ] = [ ] ;
19
- protected _matrixClient : MatrixClient | null = null ;
19
+ protected matrixClient : MatrixClient | null = null ;
20
20
private dispatcherRef : string | null = null ;
21
21
22
- public static set matrixClient ( client : MatrixClient ) {
23
- for ( const instance of ReadyWatchingStore . instances ) {
24
- instance . start ( client ) ;
25
- }
26
- }
27
-
28
22
public constructor ( protected readonly dispatcher : MatrixDispatcher ) {
29
23
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 ;
38
24
}
39
25
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 ) ;
43
28
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 ( ) ;
47
31
if ( matrixClient ) {
32
+ this . matrixClient = matrixClient ;
48
33
await this . onReady ( ) ;
49
34
}
50
35
}
@@ -53,10 +38,8 @@ export abstract class ReadyWatchingStore extends EventEmitter implements IDestro
53
38
return this . matrixClient ; // for external readonly access
54
39
}
55
40
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 ;
60
43
}
61
44
62
45
public destroy ( ) : void {
@@ -91,13 +74,13 @@ export abstract class ReadyWatchingStore extends EventEmitter implements IDestro
91
74
if ( this . matrixClient ) {
92
75
await this . onNotReady ( ) ;
93
76
}
94
- this . _matrixClient = payload . matrixClient ;
77
+ this . matrixClient = payload . matrixClient ;
95
78
await this . onReady ( ) ;
96
79
}
97
80
} else if ( payload . action === "on_client_not_viable" || payload . action === Action . OnLoggedOut ) {
98
81
if ( this . matrixClient ) {
99
82
await this . onNotReady ( ) ;
100
- this . _matrixClient = null ;
83
+ this . matrixClient = null ;
101
84
}
102
85
}
103
86
} ;
0 commit comments