/
Messaging.js
262 lines (227 loc) · 8.41 KB
/
Messaging.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* ************************************************************************
qooxdoo - the new era of web development
http://qooxdoo.org
Copyright:
2004-2011 1&1 Internet AG, Germany, http://www.1und1.de
License:
LGPL: http://www.gnu.org/licenses/lgpl.html
EPL: http://www.eclipse.org/org/documents/epl-v10.php
See the LICENSE file in the project's top-level directory for details.
Authors:
* Tino Butz (tbtz)
* Martin Wittemann (wittemann)
************************************************************************ */
/**
* Define messages to react on certain channels.
*
* The channel names will be used in the {@link #on} method to define handlers which will
* be called on certain channels and routes. The {@link #emit} method can be used
* to execute a given route on a channel. {@link #onAny} defines a handler on any channel.
*
* *Example*
*
* Here is a little example of how to use the messaging.
*
* <pre class='javascript'>
* var m = new qx.event.Messaging();
*
* m.on("get", "/address/{id}", function(data) {
* var id = data.params.id; // 1234
* // do something with the id...
* },this);
*
* m.emit("get", "/address/1234");
* </pre>
*/
qx.Bootstrap.define("qx.event.Messaging",
{
construct : function()
{
this._listener = {},
this.__listenerIdCount = 0;
this.__channelToIdMapping = {};
},
members :
{
_listener : null,
__listenerIdCount : null,
__channelToIdMapping : null,
/**
* Adds a route handler for the given channel. The route is called
* if the {@link #emit} method finds a match.
*
* @param channel {String} The channel of the message.
* @param type {String|RegExp} The type, used for checking if the executed path matches.
* @param handler {Function} The handler to call if the route matches the executed path.
* @param scope {var ? null} The scope of the handler.
* @return {String} The id of the route used to remove the route.
*/
on : function(channel, type, handler, scope) {
return this._addListener(channel, type, handler, scope);
},
/**
* Adds a handler for the "any" channel. The "any" channel is called
* before all other channels.
*
* @param type {String|RegExp} The route, used for checking if the executed path matches
* @param handler {Function} The handler to call if the route matches the executed path
* @param scope {var ? null} The scope of the handler.
* @return {String} The id of the route used to remove the route.
*/
onAny : function(type, handler, scope) {
return this._addListener("any", type, handler, scope);
},
/**
* Adds a listener for a certain channel.
*
* @param channel {String} The channel the route should be registered for
* @param type {String|RegExp} The type, used for checking if the executed path matches
* @param handler {Function} The handler to call if the route matches the executed path
* @param scope {var ? null} The scope of the handler.
* @return {String} The id of the route used to remove the route.
*/
_addListener : function(channel, type, handler, scope) {
var listeners = this._listener[channel] = this._listener[channel] || {};
var id = this.__listenerIdCount++;
var params = [];
var param = null;
// Convert the route to a regular expression.
if (qx.lang.Type.isString(type))
{
var paramsRegexp = /\{([\w\d]+)\}/g;
while ((param = paramsRegexp.exec(type)) !== null) {
params.push(param[1]);
}
type = new RegExp("^" + type.replace(paramsRegexp, "([^\/]+)") + "$");
}
listeners[id] = {regExp:type, params:params, handler:handler, scope:scope};
this.__channelToIdMapping[id] = channel;
return id;
},
/**
* Removes a registered listener by the given id.
*
* @param id {String} The id of the registered listener.
*/
remove : function(id) {
var channel = this.__channelToIdMapping[id];
var listener = this._listener[channel];
delete listener[id];
delete this.__channelToIdMapping[id];
},
/**
* Checks if a listener is registered for the given path in the given channel.
*
* @param channel {String} The channel of the message.
* @param path {String} The path to check.
*/
isListenerRegisteredFor : function(channel, path) {
var listeners = this._listener[channel];
if (!listeners || qx.lang.Object.isEmpty(listeners)) {
qx.Bootstrap.info("No listener found for channel: " + channel);
return false;
}
for (var id in listeners)
{
var listener = listeners[id];
if (listener.regExp.test(path)) {
return true;
}
}
qx.Bootstrap.info("No listener found for path: " + path);
return false;
},
/**
* Sends a message on the given channel and informs all matching route handlers.
*
* @param channel {String} The channel of the message.
* @param path {String} The path to execute
* @param params {Map} The given parameters that should be propagated
* @param customData {var} The given custom data that should be propagated
*/
emit : function(channel, path, params, customData) {
this._emit(channel, path, params, customData);
},
/**
* Executes a certain channel with a given path. Informs all
* route handlers that match with the path.
*
* @param channel {String} The channel to execute.
* @param path {String} The path to check
* @param params {Map} The given parameters that should be propagated
* @param customData {var} The given custom data that should be propagated
*/
_emit : function(channel, path, params, customData)
{
var listenerMatchedAny = false;
var listener = this._listener["any"];
listenerMatchedAny = this._emitListeners(channel, path, listener, params, customData);
var listenerMatched = false;
listener = this._listener[channel];
listenerMatched = this._emitListeners(channel, path, listener, params, customData);
if (!listenerMatched && !listenerMatchedAny) {
qx.Bootstrap.info("No listener found for " + path);
}
},
/**
* Executes all given listener for a certain channel. Checks all listeners if they match
* with the given path and executes the stored handler of the matching route.
*
* @param channel {String} The channel to execute.
* @param path {String} The path to check
* @param listeners {Map[]} All routes to test and execute.
* @param params {Map} The given parameters that should be propagated
* @param customData {var} The given custom data that should be propagated
*
* @return {Boolean} Whether the route has been executed
*/
_emitListeners : function(channel, path, listeners, params, customData)
{
if (!listeners || qx.lang.Object.isEmpty(listeners)) {
return false;
}
var listenerMatched = false;
for (var id in listeners)
{
var listener = listeners[id];
listenerMatched |= this._emitRoute(channel, path, listener, params, customData);
}
return listenerMatched;
},
/**
* Executes a certain listener. Checks if the listener matches the given path and
* executes the stored handler of the route.
*
* @param channel {String} The channel to execute.
* @param path {String} The path to check
* @param listener {Map} The route data.
* @param params {Map} The given parameters that should be propagated
* @param customData {var} The given custom data that should be propagated
*
* @return {Boolean} Whether the route has been executed
*/
_emitRoute : function(channel, path, listener, params, customData)
{
var match = listener.regExp.exec(path);
if (match)
{
var params = params || {};
var param = null;
var value = null;
match.shift(); // first match is the whole path
for (var i=0; i < match.length; i++)
{
value = match[i];
param = listener.params[i];
if (param) {
params[param] = value;
} else {
params[i] = value;
}
}
listener.handler.call(listener.scope, {path:path, params:params, customData:customData});
}
return match != undefined;
}
}
});