1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.createTimeoutController = createTimeoutController;
|
7 |
|
8 | function createTimeoutController(timeout) {
|
9 | let timeoutId;
|
10 | const timeoutCallbacks = [];
|
11 | const timeoutController = {
|
12 | onTimeout(callback) {
|
13 | timeoutCallbacks.push(callback);
|
14 | },
|
15 |
|
16 | wrap(promise) {
|
17 | return new Promise((resolve, reject) => {
|
18 | timeoutController.timedOut = false;
|
19 | timeoutController.onTimeout(resolve);
|
20 |
|
21 | if (timeout) {
|
22 | timeoutId = setTimeout(() => {
|
23 | timeoutController.timedOut = true;
|
24 | timeoutCallbacks.forEach(callback => callback());
|
25 | resolve();
|
26 | }, timeout);
|
27 | }
|
28 |
|
29 | promise.then(resolve).catch(reject).finally(() => timeoutController.cancel());
|
30 | });
|
31 | },
|
32 |
|
33 | cancel() {
|
34 | clearTimeout(timeoutId);
|
35 | },
|
36 |
|
37 | timedOut: false
|
38 | };
|
39 | return timeoutController;
|
40 | } |
\ | No newline at end of file |