UNPKG

1.92 kBJavaScriptView Raw
1(function() {
2 var EventEmitter, LinkedQueue,
3 __hasProp = Object.prototype.hasOwnProperty,
4 __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
5
6 EventEmitter = require('events').EventEmitter;
7
8 module.exports = LinkedQueue = (function(_super) {
9
10 __extends(LinkedQueue, _super);
11
12 LinkedQueue.prototype.hasNext = true;
13
14 /*
15 moves into the next
16 */
17
18 function LinkedQueue(first, onNext) {
19 this.first = first;
20 LinkedQueue.__super__.constructor.call(this);
21 this.last = first.getLastSibling();
22 if (onNext) this._onNext = onNext;
23 }
24
25 /*
26 moves onto the next request (middleware)
27 */
28
29 LinkedQueue.prototype.next = function() {
30 if (!this.hasNext) return false;
31 this._setNext();
32 this._onNext(this.current, arguments);
33 return true;
34 };
35
36 /*
37 skips middleware
38 */
39
40 LinkedQueue.prototype.skipNext = function(count) {
41 if (count == null) count = 2;
42 if (!!this.hasNext) return false;
43 while (count-- && this.hasNext) {
44 this._setNext();
45 }
46 this._onNext(this.current);
47 return true;
48 };
49
50 /*
51 */
52
53 LinkedQueue.prototype._setNext = function() {
54 this.current = !!this.current ? this.current.getNextSibling() : this.first;
55 this.hasNext = !!this.current.getNextSibling();
56 if (!this.hasNext && !this.ended) {
57 this.ended = true;
58 return this._onEnd();
59 }
60 };
61
62 /*
63 */
64
65 LinkedQueue.prototype._onNext = function(middleware) {};
66
67 /*
68 */
69
70 LinkedQueue.prototype._onEnd = function() {};
71
72 return LinkedQueue;
73
74 })(EventEmitter);
75
76 module.exports = LinkedQueue;
77
78}).call(this);