Skip to content

Commit b4284b4

Browse files
authored
Merge pull request element-hq#13756 from vector-im/t3chguy/toasts2
Convert platforms to Typescript
2 parents ef60265 + d7ee8e1 commit b4284b4

4 files changed

Lines changed: 111 additions & 119 deletions

File tree

‎src/@types/global.d.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,10 @@ declare global {
3232
InstallTrigger: any;
3333
}
3434
}
35+
36+
// add method which is missing from the node typing
37+
declare module "url" {
38+
interface Url {
39+
format(): string;
40+
}
41+
}
Lines changed: 67 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @flow
2-
31
/*
42
Copyright 2016 Aviral Dasgupta
53
Copyright 2016 OpenMarket Ltd
@@ -21,11 +19,20 @@ limitations under the License.
2119
*/
2220

2321
import VectorBasePlatform, {updateCheckStatusEnum} from './VectorBasePlatform';
24-
import BaseEventIndexManager from 'matrix-react-sdk/src/indexing/BaseEventIndexManager';
22+
import BaseEventIndexManager, {
23+
MatrixEvent,
24+
MatrixProfile,
25+
SearchResult,
26+
CrawlerCheckpoint,
27+
EventAndProfile,
28+
SearchArgs,
29+
IndexStats
30+
} from 'matrix-react-sdk/src/indexing/BaseEventIndexManager';
2531
import dis from 'matrix-react-sdk/src/dispatcher/dispatcher';
2632
import { _t, _td } from 'matrix-react-sdk/src/languageHandler';
2733
import * as rageshake from 'matrix-react-sdk/src/rageshake/rageshake';
28-
import {MatrixClient} from "matrix-js-sdk";
34+
import {MatrixClient} from "matrix-js-sdk/src/client";
35+
import {Room} from "matrix-js-sdk/src/models/room";
2936
import Modal from "matrix-react-sdk/src/Modal";
3037
import InfoDialog from "matrix-react-sdk/src/components/views/dialogs/InfoDialog";
3138
import Spinner from "matrix-react-sdk/src/components/views/elements/Spinner";
@@ -34,6 +41,7 @@ import {Key} from "matrix-react-sdk/src/Keyboard";
3441
import React from "react";
3542
import {randomString} from "matrix-js-sdk/src/randomstring";
3643
import {Action} from "matrix-react-sdk/src/dispatcher/actions";
44+
import { ActionPayload } from "matrix-react-sdk/src/dispatcher/payloads";
3745

3846
const ipcRenderer = window.ipcRenderer;
3947
const isMac = navigator.platform.toUpperCase().includes('MAC');
@@ -57,7 +65,7 @@ function platformFriendlyName(): string {
5765
}
5866
}
5967

60-
function _onAction(payload: Object) {
68+
function _onAction(payload: ActionPayload) {
6169
// Whitelist payload actions, no point sending most across
6270
if (['call_state'].includes(payload.action)) {
6371
ipcRenderer.send('app_onAction', payload);
@@ -77,53 +85,60 @@ function getUpdateCheckStatus(status) {
7785
}
7886
}
7987

88+
interface IPCPayload {
89+
id?: number;
90+
error?: string;
91+
reply?: any;
92+
}
93+
8094
class SeshatIndexManager extends BaseEventIndexManager {
95+
private pendingIpcCalls: Record<number, { resolve, reject }> = {};
96+
private nextIpcCallId: number = 0;
97+
8198
constructor() {
8299
super();
83100

84-
this._pendingIpcCalls = {};
85-
this._nextIpcCallId = 0;
86-
ipcRenderer.on('seshatReply', this._onIpcReply.bind(this));
101+
ipcRenderer.on('seshatReply', this._onIpcReply);
87102
}
88103

89-
async _ipcCall(name: string, ...args: []): Promise<{}> {
104+
async _ipcCall(name: string, ...args: any[]): Promise<any> {
90105
// TODO this should be moved into the preload.js file.
91-
const ipcCallId = ++this._nextIpcCallId;
106+
const ipcCallId = ++this.nextIpcCallId;
92107
return new Promise((resolve, reject) => {
93-
this._pendingIpcCalls[ipcCallId] = {resolve, reject};
108+
this.pendingIpcCalls[ipcCallId] = {resolve, reject};
94109
window.ipcRenderer.send('seshat', {id: ipcCallId, name, args});
95110
});
96111
}
97112

98-
_onIpcReply(ev: {}, payload: {}) {
113+
_onIpcReply = (ev: {}, payload: IPCPayload) => {
99114
if (payload.id === undefined) {
100115
console.warn("Ignoring IPC reply with no ID");
101116
return;
102117
}
103118

104-
if (this._pendingIpcCalls[payload.id] === undefined) {
119+
if (this.pendingIpcCalls[payload.id] === undefined) {
105120
console.warn("Unknown IPC payload ID: " + payload.id);
106121
return;
107122
}
108123

109-
const callbacks = this._pendingIpcCalls[payload.id];
110-
delete this._pendingIpcCalls[payload.id];
124+
const callbacks = this.pendingIpcCalls[payload.id];
125+
delete this.pendingIpcCalls[payload.id];
111126
if (payload.error) {
112127
callbacks.reject(payload.error);
113128
} else {
114129
callbacks.resolve(payload.reply);
115130
}
116-
}
131+
};
117132

118133
async supportsEventIndexing(): Promise<boolean> {
119134
return this._ipcCall('supportsEventIndexing');
120135
}
121136

122-
async initEventIndex(): Promise<> {
137+
async initEventIndex(): Promise<void> {
123138
return this._ipcCall('initEventIndex');
124139
}
125140

126-
async addEventToIndex(ev: MatrixEvent, profile: MatrixProfile): Promise<> {
141+
async addEventToIndex(ev: MatrixEvent, profile: MatrixProfile): Promise<void> {
127142
return this._ipcCall('addEventToIndex', ev, profile);
128143
}
129144

@@ -135,27 +150,27 @@ class SeshatIndexManager extends BaseEventIndexManager {
135150
return this._ipcCall('isEventIndexEmpty');
136151
}
137152

138-
async commitLiveEvents(): Promise<> {
153+
async commitLiveEvents(): Promise<void> {
139154
return this._ipcCall('commitLiveEvents');
140155
}
141156

142-
async searchEventIndex(searchConfig: SearchConfig): Promise<SearchResult> {
157+
async searchEventIndex(searchConfig: SearchArgs): Promise<SearchResult> {
143158
return this._ipcCall('searchEventIndex', searchConfig);
144159
}
145160

146161
async addHistoricEvents(
147-
events: [HistoricEvent],
162+
events: [EventAndProfile],
148163
checkpoint: CrawlerCheckpoint | null,
149164
oldCheckpoint: CrawlerCheckpoint | null,
150-
): Promise<> {
165+
): Promise<boolean> {
151166
return this._ipcCall('addHistoricEvents', events, checkpoint, oldCheckpoint);
152167
}
153168

154-
async addCrawlerCheckpoint(checkpoint: CrawlerCheckpoint): Promise<> {
169+
async addCrawlerCheckpoint(checkpoint: CrawlerCheckpoint): Promise<void> {
155170
return this._ipcCall('addCrawlerCheckpoint', checkpoint);
156171
}
157172

158-
async removeCrawlerCheckpoint(checkpoint: CrawlerCheckpoint): Promise<> {
173+
async removeCrawlerCheckpoint(checkpoint: CrawlerCheckpoint): Promise<void> {
159174
return this._ipcCall('removeCrawlerCheckpoint', checkpoint);
160175
}
161176

@@ -167,27 +182,29 @@ class SeshatIndexManager extends BaseEventIndexManager {
167182
return this._ipcCall('loadCheckpoints');
168183
}
169184

170-
async closeEventIndex(): Promise<> {
185+
async closeEventIndex(): Promise<void> {
171186
return this._ipcCall('closeEventIndex');
172187
}
173188

174-
async getStats(): Promise<> {
189+
async getStats(): Promise<IndexStats> {
175190
return this._ipcCall('getStats');
176191
}
177192

178-
async deleteEventIndex(): Promise<> {
193+
async deleteEventIndex(): Promise<void> {
179194
return this._ipcCall('deleteEventIndex');
180195
}
181196
}
182197

183198
export default class ElectronPlatform extends VectorBasePlatform {
199+
private eventIndexManager: BaseEventIndexManager = new SeshatIndexManager();
200+
private pendingIpcCalls: Record<number, { resolve, reject }> = {};
201+
private nextIpcCallId: number = 0;
202+
// this is the opaque token we pass to the HS which when we get it in our callback we can resolve to a profile
203+
private ssoID: string = randomString(32);
204+
184205
constructor() {
185206
super();
186207

187-
this._pendingIpcCalls = {};
188-
this._nextIpcCallId = 0;
189-
this.eventIndexManager = new SeshatIndexManager();
190-
191208
dis.register(_onAction);
192209
/*
193210
IPC Call `check_updates` returns:
@@ -217,9 +234,6 @@ export default class ElectronPlatform extends VectorBasePlatform {
217234
dis.fire(Action.ViewUserSettings);
218235
});
219236

220-
this.startUpdateCheck = this.startUpdateCheck.bind(this);
221-
this.stopUpdateCheck = this.stopUpdateCheck.bind(this);
222-
223237
// register OS-specific shortcuts
224238
if (isMac) {
225239
registerShortcut(Categories.NAVIGATION, {
@@ -253,8 +267,6 @@ export default class ElectronPlatform extends VectorBasePlatform {
253267
});
254268
}
255269

256-
// this is the opaque token we pass to the HS which when we get it in our callback we can resolve to a profile
257-
this.ssoID = randomString(32);
258270
this._ipcCall("startSSOFlow", this.ssoID);
259271
}
260272

@@ -290,7 +302,7 @@ export default class ElectronPlatform extends VectorBasePlatform {
290302
return true;
291303
}
292304

293-
displayNotification(title: string, msg: string, avatarUrl: string, room: Object): Notification {
305+
displayNotification(title: string, msg: string, avatarUrl: string, room: Room): Notification {
294306
// GNOME notification spec parses HTML tags for styling...
295307
// Electron Docs state all supported linux notification systems follow this markup spec
296308
// https://github.com/electron/electron/blob/master/docs/tutorial/desktop-environment-integration.md#linux
@@ -307,14 +319,14 @@ export default class ElectronPlatform extends VectorBasePlatform {
307319
silent: true, // we play our own sounds
308320
};
309321
if (avatarUrl) notifBody['icon'] = avatarUrl;
310-
const notification = new global.Notification(title, notifBody);
322+
const notification = new window.Notification(title, notifBody);
311323

312324
notification.onclick = () => {
313325
dis.dispatch({
314326
action: 'view_room',
315327
room_id: room.roomId,
316328
});
317-
global.focus();
329+
window.focus();
318330
this._ipcCall('focusWindow');
319331
};
320332

@@ -337,11 +349,11 @@ export default class ElectronPlatform extends VectorBasePlatform {
337349
return true;
338350
}
339351

340-
async getAutoLaunchEnabled(): boolean {
352+
async getAutoLaunchEnabled(): Promise<boolean> {
341353
return this._ipcCall('getAutoLaunchEnabled');
342354
}
343355

344-
async setAutoLaunchEnabled(enabled: boolean): void {
356+
async setAutoLaunchEnabled(enabled: boolean): Promise<void> {
345357
return this._ipcCall('setAutoLaunchEnabled', enabled);
346358
}
347359

@@ -350,11 +362,11 @@ export default class ElectronPlatform extends VectorBasePlatform {
350362
return !isMac;
351363
}
352364

353-
async getAutoHideMenuBarEnabled(): boolean {
365+
async getAutoHideMenuBarEnabled(): Promise<boolean> {
354366
return this._ipcCall('getAutoHideMenuBarEnabled');
355367
}
356368

357-
async setAutoHideMenuBarEnabled(enabled: boolean): void {
369+
async setAutoHideMenuBarEnabled(enabled: boolean): Promise<void> {
358370
return this._ipcCall('setAutoHideMenuBarEnabled', enabled);
359371
}
360372

@@ -363,25 +375,25 @@ export default class ElectronPlatform extends VectorBasePlatform {
363375
return !isMac;
364376
}
365377

366-
async getMinimizeToTrayEnabled(): boolean {
378+
async getMinimizeToTrayEnabled(): Promise<boolean> {
367379
return this._ipcCall('getMinimizeToTrayEnabled');
368380
}
369381

370-
async setMinimizeToTrayEnabled(enabled: boolean): void {
382+
async setMinimizeToTrayEnabled(enabled: boolean): Promise<void> {
371383
return this._ipcCall('setMinimizeToTrayEnabled', enabled);
372384
}
373385

374-
async canSelfUpdate(): boolean {
386+
async canSelfUpdate(): Promise<boolean> {
375387
const feedUrl = await this._ipcCall('getUpdateFeedUrl');
376388
return Boolean(feedUrl);
377389
}
378390

379-
startUpdateCheck() {
391+
startUpdateCheck = () => {
380392
if (this.showUpdateCheck) return;
381393
super.startUpdateCheck();
382394

383395
ipcRenderer.send('check_updates');
384-
}
396+
};
385397

386398
installUpdate() {
387399
// IPC to the main process to install the update, since quitAndInstall
@@ -394,7 +406,7 @@ export default class ElectronPlatform extends VectorBasePlatform {
394406
return _t('Riot Desktop (%(platformName)s)', { platformName: platformFriendlyName() });
395407
}
396408

397-
screenCaptureErrorString(): ?string {
409+
screenCaptureErrorString(): string | null {
398410
return null;
399411
}
400412

@@ -409,10 +421,10 @@ export default class ElectronPlatform extends VectorBasePlatform {
409421
window.location.reload(false);
410422
}
411423

412-
async _ipcCall(name, ...args) {
413-
const ipcCallId = ++this._nextIpcCallId;
424+
async _ipcCall(name: string, ...args: any[]): Promise<any> {
425+
const ipcCallId = ++this.nextIpcCallId;
414426
return new Promise((resolve, reject) => {
415-
this._pendingIpcCalls[ipcCallId] = {resolve, reject};
427+
this.pendingIpcCalls[ipcCallId] = {resolve, reject};
416428
window.ipcRenderer.send('ipcCall', {id: ipcCallId, name, args});
417429
// Maybe add a timeout to these? Probably not necessary.
418430
});
@@ -424,13 +436,13 @@ export default class ElectronPlatform extends VectorBasePlatform {
424436
return;
425437
}
426438

427-
if (this._pendingIpcCalls[payload.id] === undefined) {
439+
if (this.pendingIpcCalls[payload.id] === undefined) {
428440
console.warn("Unknown IPC payload ID: " + payload.id);
429441
return;
430442
}
431443

432-
const callbacks = this._pendingIpcCalls[payload.id];
433-
delete this._pendingIpcCalls[payload.id];
444+
const callbacks = this.pendingIpcCalls[payload.id];
445+
delete this.pendingIpcCalls[payload.id];
434446
if (payload.error) {
435447
callbacks.reject(payload.error);
436448
} else {

0 commit comments

Comments
 (0)