Преглед на файлове

feat(ts) migrate modules\util\Deferred to TS

master
Naman Jain преди 4 месеца
родител
ревизия
01b6815fe8
No account linked to committer's email address
променени са 2 файла, в които са добавени 16 реда и са изтрити 10 реда
  1. 1
    1
      globals.d.ts
  2. 15
    9
      modules/util/Deferred.ts

+ 1
- 1
globals.d.ts Целия файл

1
 export {};
1
 export {};
2
 
2
 
3
-declare global {    
3
+declare global {
4
     type Timeout = ReturnType<typeof setTimeout>;
4
     type Timeout = ReturnType<typeof setTimeout>;
5
     interface Window {
5
     interface Window {
6
         connectionTimes: any;
6
         connectionTimes: any;

modules/util/Deferred.js → modules/util/Deferred.ts Целия файл

1
-
2
 /**
1
 /**
3
  * Promise-like object which can be passed around for resolving it later. It
2
  * Promise-like object which can be passed around for resolving it later. It
4
  * implements the "thenable" interface, so it can be used wherever a Promise
3
  * implements the "thenable" interface, so it can be used wherever a Promise
6
  *
5
  *
7
  * In addition a "reject on timeout" functionality is provided.
6
  * In addition a "reject on timeout" functionality is provided.
8
  */
7
  */
9
-export default class Deferred {
8
+export default class Deferred<T = any> {
9
+    promise: Promise<T>;
10
+    resolve: (value: T | PromiseLike<T>) => void;
11
+    reject: (reason?: any) => void;
12
+    then: Promise<T>['then'];
13
+    catch: Promise<T>['catch'];
14
+    private _timeout?: Timeout;
15
+
10
     /**
16
     /**
11
      * Instantiates a Deferred object.
17
      * Instantiates a Deferred object.
12
      */
18
      */
13
     constructor() {
19
     constructor() {
14
-        this.promise = new Promise((resolve, reject) => {
15
-            this.resolve = (...args) => {
20
+        this.promise = new Promise<T>((resolve, reject) => {
21
+            this.resolve = (value: T | PromiseLike<T>) => {
16
                 this.clearRejectTimeout();
22
                 this.clearRejectTimeout();
17
-                resolve(...args);
23
+                resolve(value);
18
             };
24
             };
19
-            this.reject = (...args) => {
25
+            this.reject = (reason?: any) => {
20
                 this.clearRejectTimeout();
26
                 this.clearRejectTimeout();
21
-                reject(...args);
27
+                reject(reason);
22
             };
28
             };
23
         });
29
         });
24
         this.then = this.promise.then.bind(this.promise);
30
         this.then = this.promise.then.bind(this.promise);
28
     /**
34
     /**
29
      * Clears the reject timeout.
35
      * Clears the reject timeout.
30
      */
36
      */
31
-    clearRejectTimeout() {
37
+    clearRejectTimeout(): void {
32
         clearTimeout(this._timeout);
38
         clearTimeout(this._timeout);
33
     }
39
     }
34
 
40
 
35
     /**
41
     /**
36
      * Rejects the promise after the given timeout.
42
      * Rejects the promise after the given timeout.
37
      */
43
      */
38
-    setRejectTimeout(ms) {
44
+    setRejectTimeout(ms: number): void {
39
         this._timeout = setTimeout(() => {
45
         this._timeout = setTimeout(() => {
40
             this.reject(new Error('timeout'));
46
             this.reject(new Error('timeout'));
41
         }, ms);
47
         }, ms);

Loading…
Отказ
Запис