-
Notifications
You must be signed in to change notification settings - Fork 132
/
ElectronEventLoader.ts
36 lines (30 loc) · 1.22 KB
/
ElectronEventLoader.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
import { DesktopAgent } from "@kite9/fdc3-standard";
import { GetAgentParams } from "@kite9/fdc3-standard";
import { Loader } from "./Loader";
/**
* This approach will resolve the loader promise if the fdc3Ready event occurs.
* This is done by electron implementations setting window.fdc3.
*/
export class ElectronEventLoader implements Loader {
done = false
poll(endTime: number, resolve: (value: DesktopAgent | void) => void, reject: (reason?: any) => void) {
const timeRemaining = endTime - Date.now()
if (globalThis.window.fdc3 != null) {
resolve(globalThis.window.fdc3)
} else if ((timeRemaining > 0) && (this.done == false)) {
setTimeout(() => this.poll(endTime, resolve, reject), 100);
} else {
resolve();
}
}
cancel(): void {
this.done = true;
}
get(params: GetAgentParams): Promise<DesktopAgent | void> {
return new Promise<DesktopAgent | void>((resolve, reject) => {
const endPollTime = Date.now() + (params.timeoutMs!! + 500)
// console.log("Starting poll: " + endPollTime + " " + params.timeout + " " + Date.now())
this.poll(endPollTime, resolve, reject)
});
}
}