Skip to content

Commit 6405819

Browse files
committed
chore: linter changes
1 parent d09c989 commit 6405819

File tree

1 file changed

+50
-43
lines changed

1 file changed

+50
-43
lines changed

src/models/promise_queue.ts

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import EventEmitter from 'events';
22
import PriorityQueue from './priority_queue';
33

4-
type TaskOptions = { signal?: AbortSignal; };
4+
type TaskOptions = { signal?: AbortSignal };
55

66
type Task<TaskResultType> =
7-
| ((options?: TaskOptions) => PromiseLike<TaskResultType>)
8-
| ((options?: TaskOptions) => TaskResultType);
7+
| ((options?: TaskOptions) => PromiseLike<TaskResultType>)
8+
| ((options?: TaskOptions) => TaskResultType);
99

1010
type AddTaskOptions = {
1111
priority?: number;
1212
signal?: AbortSignal;
13-
}
14-
13+
};
14+
1515
export default class PromiseQueue extends EventEmitter {
1616
#queue: PriorityQueue<() => Promise<void>>;
1717

@@ -22,7 +22,7 @@ export default class PromiseQueue extends EventEmitter {
2222

2323
#isPaused = false;
2424

25-
#intervalId: ReturnType<typeof setInterval>|null = null;
25+
#intervalId: ReturnType<typeof setInterval> | null = null;
2626

2727
constructor() {
2828
super();
@@ -61,7 +61,7 @@ export default class PromiseQueue extends EventEmitter {
6161
}
6262

6363
#next() {
64-
this.emit('next')
64+
this.emit('next');
6565
this.#jobsRunning--;
6666
this.#tryToStartNextJob();
6767
}
@@ -105,47 +105,54 @@ export default class PromiseQueue extends EventEmitter {
105105
return false;
106106
}
107107

108-
static async throwOnAbort(signal: AbortSignal): Promise<never> {
109-
return new Promise((_resolve, reject) => {
110-
signal.addEventListener('abort', () => {
111-
reject(signal.reason);
112-
}, {once: true});
113-
});
114-
}
115-
116-
/**
117-
* Try to start jobs until the concurrency limit is reached.
118-
*/
119-
processQueue(): void {
120-
while (this.#tryToStartNextJob()) {}
121-
}
108+
static async throwOnAbort(signal: AbortSignal): Promise<never> {
109+
return new Promise((_resolve, reject) => {
110+
signal.addEventListener(
111+
'abort',
112+
() => {
113+
reject(signal.reason);
114+
},
115+
{ once: true }
116+
);
117+
});
118+
}
119+
120+
/**
121+
* Try to start jobs until the concurrency limit is reached.
122+
*/
123+
processQueue(): void {
124+
// eslint-disable-next-line no-empty
125+
while (this.#tryToStartNextJob()) {}
126+
}
122127

123128
async add<TaskResultType>(task: Task<TaskResultType>, options?: AddTaskOptions) {
124129
this.emit('new_job');
125130
return new Promise((resolve, reject) => {
126-
this.#queue.push(PriorityQueue.makeNode(options?.priority || 0, async () => {
127-
this.#jobsRunning++;
128-
try {
129-
// Throw if the operation was aborted and don't run.
130-
options?.signal?.throwIfAborted();
131-
132-
// Run the task until completion or until the task aborts.
133-
let operation = task({signal: options?.signal});
134-
// If a signal was passed, we may abort the operation from the outside.
135-
// This does not abort internally the task, it has to also manage the abort signal.
136-
if (options?.signal) {
137-
operation = Promise.race([operation, PromiseQueue.throwOnAbort(options.signal)]);
131+
this.#queue.push(
132+
PriorityQueue.makeNode(options?.priority || 0, async () => {
133+
this.#jobsRunning++;
134+
try {
135+
// Throw if the operation was aborted and don't run.
136+
options?.signal?.throwIfAborted();
137+
138+
// Run the task until completion or until the task aborts.
139+
let operation = task({ signal: options?.signal });
140+
// If a signal was passed, we may abort the operation from the outside.
141+
// This does not abort internally the task, it has to also manage the abort signal.
142+
if (options?.signal) {
143+
operation = Promise.race([operation, PromiseQueue.throwOnAbort(options.signal)]);
144+
}
145+
const result = await operation;
146+
resolve(result);
147+
// Completed task
148+
} catch (error: unknown) {
149+
reject(error);
150+
} finally {
151+
this.emit('finished_job');
152+
this.#next();
138153
}
139-
const result = await operation;
140-
resolve(result);
141-
// Completed task
142-
} catch (error: unknown) {
143-
reject(error);
144-
} finally {
145-
this.emit('finished_job');
146-
this.#next();
147-
}
148-
}));
154+
})
155+
);
149156
});
150157
}
151158
}

0 commit comments

Comments
 (0)