1 | (function() {
|
2 | var MessageBuilder, Router, collectPlugin, crema, disposable, plugins, pullPlugin, pushPlugin, _;
|
3 |
|
4 | crema = require("crema");
|
5 |
|
6 | MessageBuilder = require("./message").Builder;
|
7 |
|
8 | pushPlugin = require("./push/plugin");
|
9 |
|
10 | pullPlugin = require("./pull/plugin");
|
11 |
|
12 | collectPlugin = require("./collect/plugin");
|
13 |
|
14 | plugins = require("./plugins");
|
15 |
|
16 | disposable = require("disposable");
|
17 |
|
18 | _ = require("underscore");
|
19 |
|
20 | Router = (function() {
|
21 | |
22 |
|
23 | function Router() {
|
24 | this.directors = {};
|
25 | this._messageBuilder = new MessageBuilder(this);
|
26 | this._plugins = new plugins(this);
|
27 | this.use(pushPlugin);
|
28 | this.use(pullPlugin);
|
29 | this.use(collectPlugin);
|
30 | }
|
31 |
|
32 | |
33 |
|
34 |
|
35 |
|
36 | Router.prototype.use = function(plugin) {
|
37 | return this._plugins.add(plugin);
|
38 | };
|
39 |
|
40 | |
41 |
|
42 |
|
43 | Router.prototype.using = function() {
|
44 | return this._plugins.using();
|
45 | };
|
46 |
|
47 | |
48 |
|
49 |
|
50 |
|
51 | Router.prototype.on = function(routeOrListeners, ops, callback) {
|
52 | var listenerDisposables, route, routes, type, _fn, _i, _len,
|
53 | _this = this;
|
54 | if (typeof ops === 'function') {
|
55 | callback = ops;
|
56 | ops = {};
|
57 | }
|
58 | listenerDisposables = disposable.create();
|
59 | if (typeof routeOrListeners === "object" && !callback) {
|
60 | for (type in routeOrListeners) {
|
61 | listenerDisposables.add(this.on(type, routeOrListeners[type]));
|
62 | }
|
63 | return listenerDisposables;
|
64 | }
|
65 | if (typeof routeOrListeners === "string") {
|
66 | routes = crema(routeOrListeners);
|
67 | } else if (routeOrListeners instanceof Array) {
|
68 | routes = routeOrListeners;
|
69 | } else {
|
70 | routes = [routeOrListeners];
|
71 | }
|
72 | _fn = function(route) {
|
73 | if (ops.type) route.type = ops.type;
|
74 | if (ops.tags) _.extend(route.tags, ops.tags);
|
75 | listenerDisposables.add(_this.directors[route.type].addListener(route, callback));
|
76 | return _this._plugins.newListener({
|
77 | route: route,
|
78 | callback: callback
|
79 | });
|
80 | };
|
81 | for (_i = 0, _len = routes.length; _i < _len; _i++) {
|
82 | route = routes[_i];
|
83 | _fn(route);
|
84 | }
|
85 | return listenerDisposables;
|
86 | };
|
87 |
|
88 | |
89 |
|
90 |
|
91 | Router.prototype.routeExists = function(ops) {
|
92 | var type;
|
93 | if (typeof ops.channel === 'string') {
|
94 | ops.channel = crema.parseChannel(ops.channel);
|
95 | }
|
96 | if (!ops.type) {
|
97 | for (type in this.directors) {
|
98 | if (this.directors[type].routeExists(ops)) return true;
|
99 | }
|
100 | return false;
|
101 | }
|
102 | return this.directors[ops.type].routeExists(ops);
|
103 | };
|
104 |
|
105 | |
106 |
|
107 |
|
108 | Router.prototype.channels = function(ops) {
|
109 | var channels, director, name;
|
110 | channels = [];
|
111 | for (name in this.directors) {
|
112 | director = this.directors[name];
|
113 | channels = channels.concat(director.channels(ops));
|
114 | }
|
115 | return channels;
|
116 | };
|
117 |
|
118 | Router.prototype.dispatch = function(messageWriter) {
|
119 | return this.directors[messageWriter.type].dispatch(messageWriter);
|
120 | };
|
121 |
|
122 | |
123 |
|
124 |
|
125 |
|
126 | Router.prototype.req = function() {
|
127 | return this.request.apply(this, arguments);
|
128 | };
|
129 |
|
130 | |
131 |
|
132 |
|
133 |
|
134 | Router.prototype.request = function(channel, query, headers) {
|
135 | return this._messageBuilder.clean().channel(typeof channel === "string" ? crema.parseChannel(channel) : channel).query(query).headers(headers);
|
136 | };
|
137 |
|
138 | return Router;
|
139 |
|
140 | })();
|
141 |
|
142 | module.exports = Router;
|
143 |
|
144 | }).call(this);
|