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