Skip to content

Commit 47f637f

Browse files
committed
chore: docstrings
1 parent e64c06c commit 47f637f

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/models/promise_queue.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ type AddTaskOptions = {
1818
signal?: AbortSignal;
1919
};
2020

21+
/**
22+
* PromiseQueue is a task executor that allows a certain number of concurrent
23+
* tasks to run at any time.
24+
* When a task is added it returns a promise that resolves when the underlying task resolves.
25+
*
26+
* PromiseQueue is also an EventEmitter for the events:
27+
* - new_job: When a new job is added to the queue.
28+
* - finished_job: When a job is finished.
29+
* - next: When we try to start the next job.
30+
* - queue_empty: When the last job on queue start running.
31+
* - idle: When all jobs are done and there is no more jobs to run.
32+
* - job_start: When we start running a new job.
33+
*/
2134
export default class PromiseQueue extends EventEmitter {
2235
#queue: PriorityQueue<() => Promise<void>>;
2336

@@ -37,6 +50,10 @@ export default class PromiseQueue extends EventEmitter {
3750
this.#startInterval();
3851
}
3952

53+
/**
54+
* Try to start any jobs we can and set a timeout for the next interval.
55+
* Will stop the interval if the PromiseQueue is paused.
56+
*/
4057
#startInterval() {
4158
this.processQueue();
4259
this.#intervalId = setTimeout(() => {
@@ -48,52 +65,83 @@ export default class PromiseQueue extends EventEmitter {
4865
}, 1000);
4966
}
5067

68+
/**
69+
* If there is an interval scheduled to run, stop and clear internal state.
70+
*/
5171
#clearInterval() {
5272
if (this.#intervalId) {
5373
clearInterval(this.#intervalId);
5474
this.#intervalId = null;
5575
}
5676
}
5777

78+
/**
79+
* Check idf the PromiseQueue is paused.
80+
*/
5881
public get isPaused() {
5982
return this.#isPaused;
6083
}
6184

85+
/**
86+
* Pause or stop processing the tasks.
87+
* Does not stop any running tasks.
88+
*/
6289
public stop() {
6390
this.#isPaused = true;
6491
this.#clearInterval();
6592
}
6693

94+
/**
95+
* Unpause or continue processing tasks.
96+
*/
6797
public continue() {
6898
this.#isPaused = false;
6999
this.#startInterval();
70100
}
71101

102+
/**
103+
* Getter for how many jobs are currently running.
104+
*/
72105
public get jobsRunning() {
73106
return this.#jobsRunning;
74107
}
75108

109+
/**
110+
* Called after a task is done, will try to start a new task.
111+
*/
76112
#next() {
77113
this.emit('next');
78114
this.#jobsRunning--;
79115
this.#tryToStartNextJob();
80116
}
81117

118+
/**
119+
* Whether we can start a new job.
120+
*/
82121
get #canStartJob() {
83122
return this.#jobsRunning < this.concurrent;
84123
}
85124

125+
/**
126+
* Getter for how many concurrent jobs can run.
127+
*/
86128
public get concurrent() {
87129
return this.#allowedConcurrentJobs;
88130
}
89131

132+
/**
133+
* Setter for concurrent jobs.
134+
*/
90135
public set concurrent(value) {
91136
if (value < 1) {
92137
throw new Error('Cannot have less than 1 job running.');
93138
}
94139
this.#allowedConcurrentJobs = value;
95140
}
96141

142+
/**
143+
* Check if we can start a new job and start it.
144+
*/
97145
#tryToStartNextJob(): boolean {
98146
if (this.#isPaused) {
99147
return false;
@@ -121,6 +169,9 @@ export default class PromiseQueue extends EventEmitter {
121169
return false;
122170
}
123171

172+
/**
173+
* When the signal emits an abort event we should reject with the same reason.
174+
*/
124175
static async throwOnAbort(signal: AbortSignal): Promise<never> {
125176
return new Promise((_resolve, reject) => {
126177
signal.addEventListener(
@@ -141,6 +192,12 @@ export default class PromiseQueue extends EventEmitter {
141192
while (this.#tryToStartNextJob()) {}
142193
}
143194

195+
/**
196+
* Add a new task to the queue, the returned promise will resolve when the task resolves.
197+
* @param task The underlying job to run.
198+
* @param options.priority The task priority, the higher it is the sooner the task will run.
199+
* @param option.signal The `AbortSignal` that can be used to abort the task from the caller.
200+
*/
144201
async add<TaskResultType>(task: Task<TaskResultType>, options?: AddTaskOptions) {
145202
this.emit('new_job');
146203
return new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)