This repository was archived by the owner on Oct 22, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathReadyWatchingStore.ts
102 lines (85 loc) · 3.48 KB
/
ReadyWatchingStore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Copyright 2024 New Vector Ltd.
* Copyright 2021, 2022 The Matrix.org Foundation C.I.C.
*
* SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
* Please see LICENSE files in the repository root for full details.
*/
import { MatrixClient, SyncState } from "matrix-js-sdk/src/matrix";
import { EventEmitter } from "events";
import { ActionPayload } from "../dispatcher/payloads";
import { IDestroyable } from "../utils/IDestroyable";
import { Action } from "../dispatcher/actions";
import { MatrixDispatcher } from "../dispatcher/dispatcher";
export abstract class ReadyWatchingStore extends EventEmitter implements IDestroyable {
private static instances: ReadyWatchingStore[] = [];
protected _matrixClient: MatrixClient | null = null;
private dispatcherRef: string | null = null;
public static set matrixClient(client: MatrixClient) {
for (const instance of ReadyWatchingStore.instances) {
instance.start(client);
}
}
public constructor(protected readonly dispatcher: MatrixDispatcher) {
super();
this.dispatcherRef = this.dispatcher.register(this.onAction);
}
public get matrixClient(): MatrixClient | null {
return this._matrixClient;
}
public async start(matrixClient: MatrixClient | null): Promise<void> {
const oldClient = this._matrixClient;
this._matrixClient = matrixClient;
if (oldClient !== matrixClient) {
await this.onNotReady();
}
if (matrixClient) {
await this.onReady();
}
}
public get mxClient(): MatrixClient | null {
return this.matrixClient; // for external readonly access
}
// XXX: This method is intended only for use in tests.
public async useUnitTestClient(cli: MatrixClient): Promise<void> {
this._matrixClient = cli;
await this.onReady();
}
public destroy(): void {
if (this.dispatcherRef !== null) this.dispatcher.unregister(this.dispatcherRef);
}
protected async onReady(): Promise<void> {
// Default implementation is to do nothing.
}
protected async onNotReady(): Promise<void> {
// Default implementation is to do nothing.
}
protected onDispatcherAction(payload: ActionPayload): void {
// Default implementation is to do nothing.
}
private onAction = async (payload: ActionPayload): Promise<void> => {
this.onDispatcherAction(payload);
if (payload.action === "MatrixActions.sync") {
// Only set the client on the transition into the PREPARED state.
// Everything after this is unnecessary (we only need to know once we have a client)
// and we intentionally don't set the client before this point to avoid stores
// updating for every event emitted during the cached sync.
if (
payload.prevState !== SyncState.Prepared &&
payload.state === SyncState.Prepared &&
this.matrixClient !== payload.matrixClient
) {
if (this.matrixClient) {
await this.onNotReady();
}
this._matrixClient = payload.matrixClient;
await this.onReady();
}
} else if (payload.action === "on_client_not_viable" || payload.action === Action.OnLoggedOut) {
if (this.matrixClient) {
await this.onNotReady();
this._matrixClient = null;
}
}
};
}