소스 검색

feat(ts) migrate AsyncQueue to TS

dev0
Naman Jain 6 달 전
부모
커밋
5aef12c0fa
No account linked to committer's email address
1개의 변경된 파일17개의 추가작업 그리고 10개의 파일을 삭제
  1. 17
    10
      modules/util/AsyncQueue.ts

modules/util/AsyncQueue.js → modules/util/AsyncQueue.ts 파일 보기

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

Loading…
취소
저장