1 | import { throttle } from 'async-agent';
|
2 | import { Queue } from 'type-enforcer';
|
3 |
|
4 | export default class PriorityQueue extends Queue {
|
5 | constructor() {
|
6 | super();
|
7 |
|
8 | const self = this;
|
9 | self._currentCallTotal = 0;
|
10 | self._check = throttle(
|
11 | () => {
|
12 | if (self._currentCallTotal === 0 && self.length !== 0) {
|
13 | self.triggerFirst();
|
14 | }
|
15 | },
|
16 | 50,
|
17 | { leading: false }
|
18 | );
|
19 | }
|
20 |
|
21 | add(callback) {
|
22 | super.add(callback);
|
23 | this._check();
|
24 | }
|
25 |
|
26 | callStarted() {
|
27 | ++this._currentCallTotal;
|
28 | }
|
29 |
|
30 | callDone() {
|
31 | --this._currentCallTotal;
|
32 | this._check();
|
33 | }
|
34 | }
|