1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.PrioritizedTaskExecutor = void 0;
|
4 | var PrioritizedTaskExecutor = /** @class */ (function () {
|
5 | /**
|
6 | * Executes tasks up to maxPoolSize at a time, other items are put in a priority queue.
|
7 | * @class PrioritizedTaskExecutor
|
8 | * @private
|
9 | * @param maxPoolSize The maximum size of the pool
|
10 | */
|
11 | function PrioritizedTaskExecutor(maxPoolSize) {
|
12 | this.maxPoolSize = maxPoolSize;
|
13 | this.currentPoolSize = 0;
|
14 | this.queue = [];
|
15 | }
|
16 | /**
|
17 | * Executes the task or queues it if no spots are available.
|
18 | * When a task is added, check if there are spots left in the pool.
|
19 | * If a spot is available, claim that spot and give back the spot once the asynchronous task has been resolved.
|
20 | * When no spots are available, add the task to the task queue. The task will be executed at some point when another task has been resolved.
|
21 | * @private
|
22 | * @param priority The priority of the task
|
23 | * @param fn The function that accepts the callback, which must be called upon the task completion.
|
24 | */
|
25 | PrioritizedTaskExecutor.prototype.executeOrQueue = function (priority, fn) {
|
26 | var _this = this;
|
27 | if (this.currentPoolSize < this.maxPoolSize) {
|
28 | this.currentPoolSize++;
|
29 | fn(function () {
|
30 | _this.currentPoolSize--;
|
31 | if (_this.queue.length > 0) {
|
32 | _this.queue.sort(function (a, b) { return b.priority - a.priority; });
|
33 | var item = _this.queue.shift();
|
34 | _this.executeOrQueue(item.priority, item.fn);
|
35 | }
|
36 | });
|
37 | }
|
38 | else {
|
39 | this.queue.push({ priority: priority, fn: fn });
|
40 | }
|
41 | };
|
42 | /**
|
43 | * Checks if the taskExecutor is finished.
|
44 | * @private
|
45 | * @returns Returns `true` if the taskExecutor is finished, otherwise returns `false`.
|
46 | */
|
47 | PrioritizedTaskExecutor.prototype.finished = function () {
|
48 | return this.currentPoolSize === 0;
|
49 | };
|
50 | return PrioritizedTaskExecutor;
|
51 | }());
|
52 | exports.PrioritizedTaskExecutor = PrioritizedTaskExecutor;
|
53 | //# sourceMappingURL=prioritizedTaskExecutor.js.map |
\ | No newline at end of file |