1 | "use strict";
|
2 |
|
3 | var DLList, Events, Queues;
|
4 | DLList = require("./DLList");
|
5 | Events = require("./Events");
|
6 | Queues = class Queues {
|
7 | constructor(num_priorities) {
|
8 | var i;
|
9 | this.Events = new Events(this);
|
10 | this._length = 0;
|
11 |
|
12 | this._lists = function () {
|
13 | var j, ref, results;
|
14 | results = [];
|
15 |
|
16 | for (i = j = 1, ref = num_priorities; 1 <= ref ? j <= ref : j >= ref; i = 1 <= ref ? ++j : --j) {
|
17 | results.push(new DLList(() => {
|
18 | return this.incr();
|
19 | }, () => {
|
20 | return this.decr();
|
21 | }));
|
22 | }
|
23 |
|
24 | return results;
|
25 | }.call(this);
|
26 | }
|
27 |
|
28 | incr() {
|
29 | if (this._length++ === 0) {
|
30 | return this.Events.trigger("leftzero");
|
31 | }
|
32 | }
|
33 |
|
34 | decr() {
|
35 | if (--this._length === 0) {
|
36 | return this.Events.trigger("zero");
|
37 | }
|
38 | }
|
39 |
|
40 | push(job) {
|
41 | return this._lists[job.options.priority].push(job);
|
42 | }
|
43 |
|
44 | queued(priority) {
|
45 | if (priority != null) {
|
46 | return this._lists[priority].length;
|
47 | } else {
|
48 | return this._length;
|
49 | }
|
50 | }
|
51 |
|
52 | shiftAll(fn) {
|
53 | return this._lists.forEach(function (list) {
|
54 | return list.forEachShift(fn);
|
55 | });
|
56 | }
|
57 |
|
58 | getFirst(arr = this._lists) {
|
59 | var j, len, list;
|
60 |
|
61 | for (j = 0, len = arr.length; j < len; j++) {
|
62 | list = arr[j];
|
63 |
|
64 | if (list.length > 0) {
|
65 | return list;
|
66 | }
|
67 | }
|
68 |
|
69 | return [];
|
70 | }
|
71 |
|
72 | shiftLastFrom(priority) {
|
73 | return this.getFirst(this._lists.slice(priority).reverse()).shift();
|
74 | }
|
75 |
|
76 | };
|
77 | module.exports = Queues; |
\ | No newline at end of file |