-
Notifications
You must be signed in to change notification settings - Fork 132
/
TimeoutLoader.ts
35 lines (27 loc) · 998 Bytes
/
TimeoutLoader.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 { AgentError, DesktopAgent, GetAgentParams } from "@kite9/fdc3-standard";
import { Loader } from "./Loader";
/**
* This loader handles timing out.
*/
export class TimeoutLoader implements Loader {
done = false
poll(endTime: number, resolve: (value: DesktopAgent | void) => void, reject: (reason?: any) => void) {
const timeRemaining = endTime - Date.now()
if ((timeRemaining > 0) && (this.done == false)) {
setTimeout(() => this.poll(endTime, resolve, reject), 100);
} else if (this.done == false) {
reject(new Error(AgentError.AgentNotFound));
} 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!!
this.poll(endPollTime, resolve, reject)
});
}
}