Ver código fonte

feat(ts) migrate AsyncQueue to TS

dev0
Naman Jain 6 meses atrás
pai
commit
5aef12c0fa
Nenhuma conta vinculada ao e-mail do autor do commit
1 arquivos alterados com 17 adições e 10 exclusões
  1. 17
    10
      modules/util/AsyncQueue.ts

modules/util/AsyncQueue.js → modules/util/AsyncQueue.ts Ver arquivo

1
 import { getLogger } from '@jitsi/logger';
1
 import { getLogger } from '@jitsi/logger';
2
-import { queue } from 'async-es';
2
+import { queue, AsyncQueue as AsyncQueueType } from 'async-es';
3
 
3
 
4
 const logger = getLogger('modules/util/AsyncQueue');
4
 const logger = getLogger('modules/util/AsyncQueue');
5
 
5
 
10
     /**
10
     /**
11
      * Creates new instance.
11
      * Creates new instance.
12
      */
12
      */
13
-    constructor(message) {
13
+    constructor(message: string) {
14
         super(message);
14
         super(message);
15
         this.name = 'ClearedQueueError';
15
         this.name = 'ClearedQueueError';
16
     }
16
     }
17
 }
17
 }
18
 
18
 
19
+export type Task = (callback: (err?: Error) => void) => void;
20
+export type TaskCallback = (err?: Error) => void;
21
+
19
 /**
22
 /**
20
  * A queue for async task execution.
23
  * A queue for async task execution.
21
  */
24
  */
22
 export default class AsyncQueue {
25
 export default class AsyncQueue {
26
+    private _queue: AsyncQueueType<Task>;
27
+    private _stopped: boolean;
28
+    private _taskCallbacks: Map<Task, TaskCallback | undefined>;
29
+
23
     /**
30
     /**
24
      * Creates new instance.
31
      * Creates new instance.
25
      */
32
      */
32
     /**
39
     /**
33
      * Removes any pending tasks from the queue.
40
      * Removes any pending tasks from the queue.
34
      */
41
      */
35
-    clear() {
42
+    clear(): void {
36
         for (const finishedCallback of this._taskCallbacks.values()) {
43
         for (const finishedCallback of this._taskCallbacks.values()) {
37
             try {
44
             try {
38
                 finishedCallback?.(new ClearedQueueError('The queue has been cleared'));
45
                 finishedCallback?.(new ClearedQueueError('The queue has been cleared'));
46
     /**
53
     /**
47
      * Internal task processing implementation which makes things work.
54
      * Internal task processing implementation which makes things work.
48
      */
55
      */
49
-    _processQueueTasks(task, finishedCallback) {
56
+    private _processQueueTasks(task: Task, finishedCallback: TaskCallback): void {
50
         try {
57
         try {
51
             task(finishedCallback);
58
             task(finishedCallback);
52
         } catch (error) {
59
         } catch (error) {
60
     /**
67
     /**
61
      * Pauses the execution of the tasks on the queue.
68
      * Pauses the execution of the tasks on the queue.
62
      */
69
      */
63
-    pause() {
70
+    pause(): void {
64
         this._queue.pause();
71
         this._queue.pause();
65
     }
72
     }
66
 
73
 
78
      *     }
85
      *     }
79
      * });
86
      * });
80
      *
87
      *
81
-     * @param {function} task - The task to be executed. See the description above.
82
-     * @param {function} [callback] - Optional callback to be called after the task has been executed.
88
+     * @param {Task} task - The task to be executed. See the description above.
89
+     * @param {TaskCallback} [callback] - Optional callback to be called after the task has been executed.
83
      */
90
      */
84
-    push(task, callback) {
91
+    push(task: Task, callback?: TaskCallback): void {
85
         if (this._stopped) {
92
         if (this._stopped) {
86
             callback && callback(new Error('The queue has been stopped'));
93
             callback && callback(new Error('The queue has been stopped'));
87
 
94
 
94
     /**
101
     /**
95
      * Resumes the execution of the tasks on the queue.
102
      * Resumes the execution of the tasks on the queue.
96
      */
103
      */
97
-    resume() {
104
+    resume(): void {
98
         this._queue.resume();
105
         this._queue.resume();
99
     }
106
     }
100
 
107
 
102
      * Shutdowns the queue. All already queued tasks will execute, but no future tasks can be added. If a task is added
109
      * Shutdowns the queue. All already queued tasks will execute, but no future tasks can be added. If a task is added
103
      * after the queue has been shutdown then the callback will be called with an error.
110
      * after the queue has been shutdown then the callback will be called with an error.
104
      */
111
      */
105
-    shutdown() {
112
+    shutdown(): void {
106
         this._stopped = true;
113
         this._stopped = true;
107
     }
114
     }
108
 }
115
 }

Carregando…
Cancelar
Salvar