1 | (function() {
|
2 | var MessageReader, MessageWriter, Reader, Writer, outcome,
|
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 | Reader = require("./io/reader");
|
7 |
|
8 | Writer = require("./io/writer");
|
9 |
|
10 | outcome = require("outcome");
|
11 |
|
12 | exports.Reader = MessageReader = (function(_super) {
|
13 |
|
14 | __extends(MessageReader, _super);
|
15 |
|
16 | |
17 |
|
18 |
|
19 |
|
20 | function MessageReader(writer, from, channel, query, headers, tags, callback) {
|
21 | this.writer = writer;
|
22 | this.from = from;
|
23 | this.channel = channel;
|
24 | this.query = query;
|
25 | this.headers = headers != null ? headers : {};
|
26 | this.tags = tags != null ? tags : {};
|
27 | this.callback = callback != null ? callback : null;
|
28 | MessageReader.__super__.constructor.call(this, writer);
|
29 | }
|
30 |
|
31 | return MessageReader;
|
32 |
|
33 | })(Reader);
|
34 |
|
35 | exports.Writer = MessageWriter = (function(_super) {
|
36 |
|
37 | __extends(MessageWriter, _super);
|
38 |
|
39 | |
40 |
|
41 |
|
42 | function MessageWriter(_ops) {
|
43 | this._ops = _ops;
|
44 | this.channel = _ops.channel;
|
45 | this.tags = _ops.tags;
|
46 | this.callback = _ops.callback;
|
47 | this.next = _ops.next;
|
48 | this.pre = _ops.pre;
|
49 | this.type = _ops.type;
|
50 | this.from = _ops.from;
|
51 | this.headers = _ops.headers;
|
52 | this.query = _ops.query;
|
53 | MessageWriter.__super__.constructor.call(this);
|
54 | }
|
55 |
|
56 | |
57 |
|
58 |
|
59 | MessageWriter.prototype.reader = function(index, numListeners) {
|
60 | return new MessageReader(this, this.from, this.channel, this.query, this.headers, this.tags, this.callback);
|
61 | };
|
62 |
|
63 | return MessageWriter;
|
64 |
|
65 | })(Writer);
|
66 |
|
67 | exports.Builder = (function() {
|
68 | |
69 |
|
70 | function _Class(router) {
|
71 | this.router = router;
|
72 | this.clean();
|
73 | }
|
74 |
|
75 | |
76 |
|
77 |
|
78 |
|
79 |
|
80 | _Class.prototype.options = function(value) {
|
81 | if (!arguments.length) return this._ops;
|
82 | this._ops = value || {};
|
83 | return this;
|
84 | };
|
85 |
|
86 | |
87 |
|
88 |
|
89 | _Class.prototype.clean = function() {
|
90 | this._ops = {};
|
91 | return this.from(this.router);
|
92 | };
|
93 |
|
94 | |
95 |
|
96 |
|
97 |
|
98 | _Class.prototype.tag = function(key, value) {
|
99 | if (!arguments.length) return this._ops.tags;
|
100 | if (!this._ops.tags) this._ops.tags = {};
|
101 | if (typeof key === 'string') {
|
102 | if (arguments.length === 1) return this._ops.tags[key];
|
103 | this._ops.tags[key] = value;
|
104 | } else {
|
105 | this._ops.tags = key || {};
|
106 | }
|
107 | return this;
|
108 | };
|
109 |
|
110 | |
111 |
|
112 |
|
113 | _Class.prototype.hasListeners = function(search) {
|
114 | search = search != null ? search : {
|
115 | tags: this._ops.tags
|
116 | };
|
117 | return !!this.router.directors[this.type()].getListeners({
|
118 | channel: this._ops.channel
|
119 | }, search).length;
|
120 | };
|
121 |
|
122 | |
123 |
|
124 |
|
125 | _Class.prototype.type = function(value) {
|
126 | return this._param('type', arguments);
|
127 | };
|
128 |
|
129 | |
130 |
|
131 |
|
132 | _Class.prototype.from = function(value) {
|
133 | return this._param('from', arguments);
|
134 | };
|
135 |
|
136 | |
137 |
|
138 |
|
139 | _Class.prototype.to = function(value) {
|
140 | return this._param('to', arguments);
|
141 | };
|
142 |
|
143 | |
144 |
|
145 |
|
146 | _Class.prototype.channel = function(value) {
|
147 | return this._param('channel', arguments);
|
148 | };
|
149 |
|
150 | |
151 |
|
152 |
|
153 |
|
154 | _Class.prototype.query = function(value) {
|
155 | return this._param('query', arguments);
|
156 | };
|
157 |
|
158 | |
159 |
|
160 |
|
161 |
|
162 | _Class.prototype.headers = function(value) {
|
163 | return this._param('headers', arguments);
|
164 | };
|
165 |
|
166 | |
167 |
|
168 |
|
169 |
|
170 |
|
171 | _Class.prototype.response = function(callback) {
|
172 | return this._param('response', arguments);
|
173 | };
|
174 |
|
175 | |
176 |
|
177 |
|
178 |
|
179 | _Class.prototype.error = function(callback) {
|
180 | return this._param('error', arguments);
|
181 | };
|
182 |
|
183 | |
184 |
|
185 |
|
186 |
|
187 | _Class.prototype.success = function(callback) {
|
188 | return this._param('success', arguments);
|
189 | };
|
190 |
|
191 | |
192 |
|
193 |
|
194 |
|
195 | _Class.prototype.next = function(middleware) {
|
196 | return this._param('next', arguments);
|
197 | };
|
198 |
|
199 | |
200 |
|
201 |
|
202 |
|
203 | _Class.prototype.pre = function(middleware) {
|
204 | return this._param('pre', arguments);
|
205 | };
|
206 |
|
207 | |
208 |
|
209 |
|
210 | _Class.prototype.dispatch = function(type) {
|
211 | var writer;
|
212 | this._ops.callback = outcome({
|
213 | error: this.error(),
|
214 | success: this.success(),
|
215 | callback: this.response()
|
216 | });
|
217 | if (type) this.type(type);
|
218 | writer = new MessageWriter(this._ops);
|
219 | this.router.dispatch(writer);
|
220 | return writer;
|
221 | };
|
222 |
|
223 | |
224 |
|
225 |
|
226 | _Class.prototype._param = function(name, args) {
|
227 | if (!args.length) return this._ops[name];
|
228 | this._ops[name] = args[0];
|
229 | return this;
|
230 | };
|
231 |
|
232 | return _Class;
|
233 |
|
234 | })();
|
235 |
|
236 | }).call(this);
|