diff options
Diffstat (limited to 'language-web/src/main/js/editor/PendingRequest.ts')
-rw-r--r-- | language-web/src/main/js/editor/PendingRequest.ts | 55 |
1 files changed, 0 insertions, 55 deletions
diff --git a/language-web/src/main/js/editor/PendingRequest.ts b/language-web/src/main/js/editor/PendingRequest.ts deleted file mode 100644 index 49d4c36c..00000000 --- a/language-web/src/main/js/editor/PendingRequest.ts +++ /dev/null | |||
@@ -1,55 +0,0 @@ | |||
1 | import { getLogger } from '../logging'; | ||
2 | |||
3 | const REQUEST_TIMEOUT_MS = 1000; | ||
4 | |||
5 | const log = getLogger('PendingRequest'); | ||
6 | |||
7 | export class PendingRequest { | ||
8 | private readonly resolveCallback: (value: unknown) => void; | ||
9 | |||
10 | private readonly rejectCallback: (reason?: unknown) => void; | ||
11 | |||
12 | private readonly timeoutCallback: () => void; | ||
13 | |||
14 | private resolved = false; | ||
15 | |||
16 | private timeoutId: NodeJS.Timeout; | ||
17 | |||
18 | constructor( | ||
19 | resolve: (value: unknown) => void, | ||
20 | reject: (reason?: unknown) => void, | ||
21 | timeout: () => void, | ||
22 | ) { | ||
23 | this.resolveCallback = resolve; | ||
24 | this.rejectCallback = reject; | ||
25 | this.timeoutCallback = timeout; | ||
26 | this.timeoutId = setTimeout(() => { | ||
27 | if (!this.resolved) { | ||
28 | this.reject(new Error('Request timed out')); | ||
29 | this.timeoutCallback(); | ||
30 | } | ||
31 | }, REQUEST_TIMEOUT_MS); | ||
32 | } | ||
33 | |||
34 | resolve(value: unknown): void { | ||
35 | if (this.resolved) { | ||
36 | log.warn('Trying to resolve already resolved promise'); | ||
37 | return; | ||
38 | } | ||
39 | this.markResolved(); | ||
40 | this.resolveCallback(value); | ||
41 | } | ||
42 | |||
43 | reject(reason?: unknown): void { | ||
44 | if (this.resolved) { | ||
45 | log.warn('Trying to reject already resolved promise'); | ||
46 | } | ||
47 | this.markResolved(); | ||
48 | this.rejectCallback(reason); | ||
49 | } | ||
50 | |||
51 | private markResolved() { | ||
52 | this.resolved = true; | ||
53 | clearTimeout(this.timeoutId); | ||
54 | } | ||
55 | } | ||