2021-08-11 19:29:07 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { ipcRenderer } from 'electron';
|
|
|
|
|
|
|
|
import { explodePromise } from './util/explodePromise';
|
2021-11-08 21:43:37 +00:00
|
|
|
import { SECOND } from './util/durations';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from './logging/log';
|
2021-08-11 19:29:07 +00:00
|
|
|
|
|
|
|
type ResolveType = (data: unknown) => void;
|
|
|
|
|
|
|
|
export class CI {
|
|
|
|
private readonly eventListeners = new Map<string, Array<ResolveType>>();
|
|
|
|
|
|
|
|
private readonly completedEvents = new Map<string, Array<unknown>>();
|
|
|
|
|
|
|
|
constructor(public readonly deviceName: string) {
|
|
|
|
ipcRenderer.on('ci:event', (_, event, data) => {
|
|
|
|
this.handleEvent(event, data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-11-08 21:43:37 +00:00
|
|
|
public async waitForEvent(
|
|
|
|
event: string,
|
|
|
|
timeout = 60 * SECOND
|
|
|
|
): Promise<unknown> {
|
2021-08-11 19:29:07 +00:00
|
|
|
const pendingCompleted = this.completedEvents.get(event) || [];
|
|
|
|
const pending = pendingCompleted.shift();
|
|
|
|
if (pending) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`CI: resolving pending result for ${event}`, pending);
|
2021-08-11 19:29:07 +00:00
|
|
|
|
|
|
|
if (pendingCompleted.length === 0) {
|
|
|
|
this.completedEvents.delete(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pending;
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`CI: waiting for event ${event}`);
|
2021-11-08 21:43:37 +00:00
|
|
|
const { resolve, reject, promise } = explodePromise();
|
|
|
|
|
|
|
|
const timer = setTimeout(() => {
|
|
|
|
reject(new Error('Timed out'));
|
|
|
|
}, timeout);
|
2021-08-11 19:29:07 +00:00
|
|
|
|
|
|
|
let list = this.eventListeners.get(event);
|
|
|
|
if (!list) {
|
|
|
|
list = [];
|
|
|
|
this.eventListeners.set(event, list);
|
|
|
|
}
|
|
|
|
|
2021-11-08 21:43:37 +00:00
|
|
|
list.push((value: unknown) => {
|
|
|
|
clearTimeout(timer);
|
|
|
|
resolve(value);
|
|
|
|
});
|
2021-08-11 19:29:07 +00:00
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
public setProvisioningURL(url: string): void {
|
|
|
|
this.handleEvent('provisioning-url', url);
|
|
|
|
}
|
|
|
|
|
|
|
|
public handleEvent(event: string, data: unknown): void {
|
|
|
|
const list = this.eventListeners.get(event) || [];
|
|
|
|
const resolve = list.shift();
|
|
|
|
|
|
|
|
if (resolve) {
|
|
|
|
if (list.length === 0) {
|
|
|
|
this.eventListeners.delete(event);
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`CI: got event ${event} with data`, data);
|
2021-08-11 19:29:07 +00:00
|
|
|
resolve(data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`CI: postponing event ${event}`);
|
2021-08-11 19:29:07 +00:00
|
|
|
|
|
|
|
let resultList = this.completedEvents.get(event);
|
|
|
|
if (!resultList) {
|
|
|
|
resultList = [];
|
|
|
|
this.completedEvents.set(event, resultList);
|
|
|
|
}
|
|
|
|
resultList.push(data);
|
|
|
|
}
|
|
|
|
}
|