-
Notifications
You must be signed in to change notification settings - Fork 2
/
dhx.MQ.js
executable file
·260 lines (214 loc) · 7.59 KB
/
dhx.MQ.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
/*jslint browser: true, devel: true, white: true */
/*global $dhx, dhtmlx */
/*
Copyright (c) 2010,2011,2012,2013,2014 Morgan Roderick http://roderick.dk
License: MIT - http://mrgnrdrck.mit-license.org
https://github.com/mroderick/PubSubJS
*/
(function(root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['exports'], factory);
} else if (typeof exports === 'object') {
// CommonJS
factory(exports);
} else {
// Browser globals
var PubSub = {};
root.PubSub = PubSub;
factory(PubSub);
}
}((typeof window === 'object' && window) || this, function(PubSub) {
'use strict';
var messages = {},
lastUid = -1;
function hasKeys(obj) {
var key;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
return true;
}
}
return false;
}
/**
* Returns a function that throws the passed exception, for use as argument for setTimeout
* @param { Object } ex An Error object
*/
function throwException(ex) {
return function reThrowException() {
throw ex;
};
}
function callSubscriberWithDelayedExceptions(subscriber, message, data) {
try {
subscriber(message, data);
} catch (ex) {
setTimeout(throwException(ex), 0);
}
}
function callSubscriberWithImmediateExceptions(subscriber, message, data) {
subscriber(message, data);
}
function deliverMessage(originalMessage, matchedMessage, data, immediateExceptions) {
var subscribers = messages[matchedMessage],
callSubscriber = immediateExceptions ? callSubscriberWithImmediateExceptions : callSubscriberWithDelayedExceptions,
s;
if (!messages.hasOwnProperty(matchedMessage)) {
return;
}
for (s in subscribers) {
if (subscribers.hasOwnProperty(s)) {
callSubscriber(subscribers[s], originalMessage, data);
}
}
}
function createDeliveryFunction(message, data, immediateExceptions) {
return function deliverNamespaced() {
var topic = String(message),
position = topic.lastIndexOf('.');
// deliver the message as it is now
deliverMessage(message, message, data, immediateExceptions);
// trim the hierarchy and deliver message to each level
while (position !== -1) {
topic = topic.substr(0, position);
position = topic.lastIndexOf('.');
deliverMessage(message, topic, data, immediateExceptions);
}
};
}
function messageHasSubscribers(message) {
var topic = String(message),
found = Boolean(messages.hasOwnProperty(topic) && hasKeys(messages[topic])),
position = topic.lastIndexOf('.');
while (!found && position !== -1) {
topic = topic.substr(0, position);
position = topic.lastIndexOf('.');
found = Boolean(messages.hasOwnProperty(topic) && hasKeys(messages[topic]));
}
return found;
}
function publish(message, data, sync, immediateExceptions) {
var deliver = createDeliveryFunction(message, data, immediateExceptions),
hasSubscribers = messageHasSubscribers(message);
if (!hasSubscribers) {
return false;
}
if (sync === true) {
deliver();
} else {
setTimeout(deliver, 0);
}
return true;
}
/**
* PubSub.publish( message[, data] ) -> Boolean
* - message (String): The message to publish
* - data: The data to pass to subscribers
* Publishes the the message, passing the data to it's subscribers
**/
PubSub.publish = function(message, data) {
return publish(message, data, false, PubSub.immediateExceptions);
};
/**
* PubSub.publishSync( message[, data] ) -> Boolean
* - message (String): The message to publish
* - data: The data to pass to subscribers
* Publishes the the message synchronously, passing the data to it's subscribers
**/
PubSub.publishSync = function(message, data) {
return publish(message, data, true, PubSub.immediateExceptions);
};
/**
* PubSub.subscribe( message, func ) -> String
* - message (String): The message to subscribe to
* - func (Function): The function to call when a new message is published
* Subscribes the passed function to the passed message. Every returned token is unique and should be stored if
* you need to unsubscribe
**/
PubSub.subscribe = function(message, func) {
if (typeof func !== 'function') {
return false;
}
// message is not registered yet
if (!messages.hasOwnProperty(message)) {
messages[message] = {};
}
// forcing token as String, to allow for future expansions without breaking usage
// and allow for easy use as key names for the 'messages' object
var token = 'uid_' + String(++lastUid);
messages[message][token] = func;
// return token for unsubscribing
return token;
};
/* Public: Clears all subscriptions
*/
PubSub.clearAllSubscriptions = function clearAllSubscriptions() {
messages = {};
};
/*Public: Clear subscriptions by the topic
*/
PubSub.clearSubscriptions = function clearSubscriptions(topic) {
var m;
for (m in messages) {
if (messages.hasOwnProperty(m) && m.indexOf(topic) === 0) {
delete messages[m];
}
}
};
/* Public: removes subscriptions.
* When passed a token, removes a specific subscription.
* When passed a function, removes all subscriptions for that function
* When passed a topic, removes all subscriptions for that topic (hierarchy)
*
* value - A token, function or topic to unsubscribe.
*
* Examples
*
* // Example 1 - unsubscribing with a token
* var token = PubSub.subscribe('mytopic', myFunc);
* PubSub.unsubscribe(token);
*
* // Example 2 - unsubscribing with a function
* PubSub.unsubscribe(myFunc);
*
* // Example 3 - unsubscribing a topic
* PubSub.unsubscribe('mytopic');
*/
PubSub.unsubscribe = function(value) {
var isTopic = typeof value === 'string' && messages.hasOwnProperty(value),
isToken = !isTopic && typeof value === 'string',
isFunction = typeof value === 'function',
result = false,
m, message, t;
if (isTopic) {
delete messages[value];
return;
}
for (m in messages) {
if (messages.hasOwnProperty(m)) {
message = messages[m];
if (isToken && message[value]) {
delete message[value];
result = value;
// tokens are unique, so we can just stop here
break;
}
if (isFunction) {
for (t in message) {
if (message.hasOwnProperty(t) && message[t] === value) {
delete message[t];
result = true;
}
}
}
}
}
return result;
};
}));
(function(namespace) {
'use strict';
})(window.$dhx = window.$dhx || {});
$dhx.MQ = PubSub;