UNPKG

2.93 kBJavaScriptView Raw
1(function() {
2 var LinkedQueue, Response, _,
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 LinkedQueue = require("../collections/linkedQueue");
7
8 Response = require("./response");
9
10 _ = require("underscore");
11
12 module.exports = (function(_super) {
13
14 __extends(_Class, _super);
15
16 /*
17 constructor
18 */
19
20 function _Class(message, first, director) {
21 var _this = this;
22 this.message = message;
23 this.first = first;
24 this.director = director;
25 this.router = director.router;
26 this.from = message.from;
27 _Class.__super__.constructor.call(this, first);
28 this.response = new Response(this);
29 this.response.reader().dump(function() {
30 return _this.message.callback.apply(_this.message, arguments);
31 }, this.message.headers);
32 }
33
34 /*
35 */
36
37 _Class.prototype.start = function() {
38 return this.next();
39 };
40
41 /*
42 returns param, or query data
43 */
44
45 _Class.prototype.data = function(name) {
46 var obj, _i, _len;
47 if (arguments.length === 0) {
48 return _.extend({}, this.current.params, this.message.query);
49 } else if (arguments.length > 1) {
50 obj = {};
51 for (_i = 0, _len = arguments.length; _i < _len; _i++) {
52 name = arguments[_i];
53 obj[name] = this.data(name);
54 }
55 obj;
56 }
57 return this.current.params[name] || this.message.query[name];
58 };
59
60 /*
61 flattens all param data into one object
62 */
63
64 _Class.prototype.flattenData = function(reset) {
65 var allData, cur;
66 if (this._allData && !reset) return this._allData;
67 cur = this.current;
68 allData = _.defaults(cur.params, this.message.query);
69 cur = cur.getNextSibling();
70 while (cur) {
71 _.defaults(allData, cur.params);
72 cur = cur.getNextSibling();
73 }
74 return this._allData = allData;
75 };
76
77 /*
78 */
79
80 _Class.prototype._onNext = function(middleware, args) {
81 if (args.length) {
82 if (args[0]) {
83 return _onError(args[0]);
84 } else {
85 _onNextData(args[1]);
86 }
87 }
88 this.message.params = middleware.params;
89 try {
90 this.message.cache(this.hasNext);
91 return this._next(middleware, args);
92 } catch (e) {
93 return this.response.error(e);
94 }
95 };
96
97 /*
98 */
99
100 _Class.prototype._next = function(middleware) {
101 return middleware.listener(this);
102 };
103
104 /*
105 */
106
107 _Class.prototype._onError = function(error) {};
108
109 /*
110 */
111
112 _Class.prototype._onNextData = function() {};
113
114 return _Class;
115
116 })(LinkedQueue);
117
118}).call(this);