This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMatrixSharedClientService.ts
147 lines (131 loc) · 4.99 KB
/
MatrixSharedClientService.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) 2021-2022. Heusala Group Oy <[email protected]>. All rights reserved.
import { SimpleMatrixClient } from "./SimpleMatrixClient";
import { LogService } from "../core/LogService";
import { Observer, ObserverCallback } from "../core/Observer";
import { DEFAULT_IO_SERVER_HOSTNAME } from "./constants/matrix-backend";
import { SimpleSharedClientService, SharedClientServiceDestructor} from "../core/simpleRepository/types/SimpleSharedClientService";
import { SimpleSharedClientServiceEvent } from "../core/simpleRepository/types/SimpleSharedClientServiceEvent";
const LOG = LogService.createLogger('MatrixSharedClientService');
/**
* This service can be used to offer shared access to SimpleMatrixClient
* instance. We use it for our services using MatrixCrudRepository.
*/
export class MatrixSharedClientService implements SimpleSharedClientService {
public Event = SimpleSharedClientServiceEvent;
private _observer : Observer<SimpleSharedClientServiceEvent>;
private _client : SimpleMatrixClient | undefined;
private _initInProgress : boolean;
private _loginInProgress : boolean;
private _defaultServer : string;
public constructor () {
this._observer = new Observer<SimpleSharedClientServiceEvent>("MatrixSharedClientService");
this._client = undefined;
this._initInProgress = true;
this._loginInProgress = false;
this._defaultServer = DEFAULT_IO_SERVER_HOSTNAME;
}
public destroy (): void {
this._observer.destroy();
}
public getClient () : SimpleMatrixClient | undefined {
return this._client;
}
public setDefaultServer (value: string) {
this._defaultServer = value;
}
public isInitializing () : boolean {
return this._initInProgress;
}
/**
*
* @param name
* @param callback
*/
public on (
name: SimpleSharedClientServiceEvent,
callback: ObserverCallback<SimpleSharedClientServiceEvent>
): SharedClientServiceDestructor {
return this._observer.listenEvent(name, callback);
}
/**
*
* @param url
*/
public async login (
url: string
) : Promise<void> {
if (this._loginInProgress) {
throw new TypeError('Another login already in progress');
}
LOG.debug(`login: Parsing URL "${url}"`);
const u = new URL(url);
const proto = u?.protocol;
const hostname = u?.hostname ?? this._defaultServer;
const port = u?.port;
const username = decodeURIComponent(u?.username ?? '');
const password = decodeURIComponent(u?.password ?? '');
const hsUrl = `${proto}//${hostname}:${port}`;
LOG.debug(`Creating client to "${hsUrl}"`);
let client : SimpleMatrixClient = new SimpleMatrixClient(hsUrl);
this._loginInProgress = true;
LOG.debug(`Logging in to "https://${hostname}" as "${username}" with "${password}"`);
client = await client.login(username, password);
LOG.info(`Logged in to "${hostname}" as "${username}"`);
this._loginInProgress = false;
this._client = client;
if (this._observer.hasCallbacks(SimpleSharedClientServiceEvent.LOGGED_IN)) {
this._observer.triggerEvent(SimpleSharedClientServiceEvent.LOGGED_IN);
}
}
/**
*
* @param url
*/
public async initialize (
url : string
) : Promise<void> {
LOG.debug(`Initialization started: `, url);
this._initInProgress = true;
await this.login(url);
LOG.debug(`Initialization finished: `, url);
this._initInProgress = false;
if(this._observer.hasCallbacks(SimpleSharedClientServiceEvent.INITIALIZED)) {
this._observer.triggerEvent(SimpleSharedClientServiceEvent.INITIALIZED);
}
}
/**
*
*/
public async waitForInitialization () : Promise<void> {
if (this._initInProgress) {
let listener : any;
try {
await new Promise<void>((resolve, reject) => {
try {
listener = this._observer.listenEvent(
SimpleSharedClientServiceEvent.INITIALIZED,
() => {
try {
if (listener) {
listener();
listener = undefined;
}
resolve();
} catch(err) {
reject(err);
}
}
);
} catch(err) {
reject(err);
}
});
} finally {
if (listener) {
listener();
listener = undefined;
}
}
}
}
}