This repository was archived by the owner on Dec 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathEventBroker.js
294 lines (294 loc) · 13.2 KB
/
EventBroker.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "./EventDispatcher", "./BaseEvent"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var EventDispatcher_1 = require("./EventDispatcher");
var BaseEvent_1 = require("./BaseEvent");
/**
* EventBroker is a simple publish and subscribe static class that you can use to fire and receive notifications.
* Loosely coupled event handling, the subscriber does not know the publisher. Both of them only need to know the event type.
*
* @class EventBroker
* @module StructureJS
* @submodule event
* @requires EventDispatcher
* @requires BaseEvent
* @static
* @author Robert S. (www.codeBelt.com)
*/
var EventBroker = (function () {
function EventBroker() {
throw new Error('[EventBroker] Do not instantiate the EventBroker class because it is a static class.');
}
/**
* Registers an event listener object with an EventBroker object so that the listener receives notification of an event.
*
* @method addEventListener
* @param type {String} The type of event.
* @param callback {Function} The listener function that processes the event. The callback function will receive a {{#crossLink "BaseEvent"}}{{/crossLink}} object or custom event that extends the {{#crossLink "BaseEvent"}}{{/crossLink}} class.
* @param scope {any} The scope of the callback function.
* @param [priority=0] {int} Influences the order in which the listeners are called. Listeners with lower priorities are called after ones with higher priorities.
* @static
* @public
* @example
* EventBroker.addEventListener('change', this._handlerMethod, this);
* // Example of using a constant event type.
* EventBroker.addEventListener(BaseEvent.CHANGE, this._handlerMethod, this);
*
* // The event passed to the method will always be a BaseEvent object.
* _handlerMethod(event) {
* console.log(event.data);
* }
*/
EventBroker.addEventListener = function (type, callback, scope, priority) {
if (priority === void 0) { priority = 0; }
EventBroker._eventDispatcher.addEventListener(type, callback, scope, priority);
};
/**
* Registers an event listener object once with an EventDispatcher object so the listener will receive the notification of an event.
*
* @method addEventListenerOnce
* @param type {String} The type of event.
* @param callback {Function} The listener function that processes the event. The callback function will receive a {{#crossLink "BaseEvent"}}{{/crossLink}} object or custom event that extends the {{#crossLink "BaseEvent"}}{{/crossLink}} class.
* @param scope {any} The scope of the callback function.
* @param [priority=0] {int} Influences the order in which the listeners are called. Listeners with lower priorities are called after ones with higher priorities.
* @static
* @public
* @example
* EventBroker.addEventListenerOnce('change', this._handlerMethod, this);
* // Example of using a constant event type.
* EventBroker.addEventListenerOnce(BaseEvent.CHANGE, this._handlerMethod, this);
*
* // The event passed to the method will always be a BaseEvent object.
* _handlerMethod(event) {
* console.log(event.data);
* }
*/
EventBroker.addEventListenerOnce = function (type, callback, scope, priority) {
if (priority === void 0) { priority = 0; }
EventBroker._eventDispatcher.addEventListenerOnce(type, callback, scope, priority);
};
/**
* Removes a specified listener from the EventBroker object.
*
* @method removeEventListener
* @param type {String} The type of event.
* @param callback {Function} The callback function to be removed.
* @param scope {any} The scope of the callback function to be removed.
* @static
* @public
* @example
* EventBroker.removeEventListener('change', this._handlerMethod, this);
*
* EventBroker.removeEventListener(BaseEvent.CHANGE, this._handlerMethod, this);
*/
EventBroker.removeEventListener = function (type, callback, scope) {
EventBroker._eventDispatcher.removeEventListener(type, callback, scope);
};
/**
* A way to listen for multiple events.
*
* If only listening for one event use {{#crossLink "EventBroker/addEventListener:method"}}{{/crossLink}}.
*
* @method waitFor
* @param eventTypes {Array<string>} A list of event types you are waiting for.
* @param callback {Function} The callback function that will be triggered when all event types are
* @param scope {any} The scope of the callback function.
* @static
* @public
* @example
* EventBroker.waitFor(['someEvent', 'anotherEvent', CustomEvent.CHANGE], this._handlerMethod, this);
*
* _handlerMethod(events) {
* // An array of the event objects you waited for.
* }
*/
EventBroker.waitFor = function (eventTypes, callback, scope) {
EventBroker._waitForList.push({
eventTypes: eventTypes,
callback: callback,
callbackScope: scope,
events: [],
once: false
});
};
/**
* A way to listen for multiple events. Once all events all are triggered this listener will be removed.
*
* If only listening for one event use {{#crossLink "EventBroker/addEventListenerOnce:method"}}{{/crossLink}}.
*
* @method waitForOnce
* @param eventTypes {Array<string>} A list of event types you are waiting for.
* @param callback {Function} The callback function that will be triggered when all event types are
* @param scope {any} The scope of the callback function.
* @static
* @public
* @example
* EventBroker.waitForOnce(['someEvent', 'anotherEvent', CustomEvent.CHANGE], this._handlerMethod, this);
*
* _handlerMethod(events) {
* // An array of the event objects you waited for.
* }
*/
EventBroker.waitForOnce = function (eventTypes, callback, scope) {
EventBroker._waitForList.push({
eventTypes: eventTypes,
callback: callback,
callbackScope: scope,
events: [],
once: true
});
};
/**
* A way to listen for multiple events. Once all events all are triggered it will no longer
*
* @method removeWaitFor
* @param eventTypes {Array<string>} A list of event types you are waiting for.
* @param callback {Function} The callback function that will be triggered when all event types are
* @param scope {any} The scope of the callback function.
* @static
* @public
* @example
* EventBroker.removeWaitFor(['someEvent', 'anotherEvent', CustomEvent.CHANGE], this._handlerMethod, this);
*/
EventBroker.removeWaitFor = function (eventTypes, callback, scope) {
var waitForObject;
for (var i = EventBroker._waitForList.length - 1; i >= 0; i--) {
waitForObject = EventBroker._waitForList[i];
if (waitForObject.eventTypes.toString() === eventTypes.toString() && waitForObject.callback === callback && waitForObject.callbackScope === scope) {
EventBroker._waitForList.splice(i, 1);
}
}
};
/**
* Dispatches an event within the EventBroker object.
*
* @method dispatchEvent
* @param event {string|BaseEvent} The Event object or event type string you want to dispatch.
* @param [data=null] {any} The optional data you want to send with the event. Do not use this parameter if you are passing in a {{#crossLink "BaseEvent"}}{{/crossLink}}.
* @param [scope=null] {any} You can optionally pass in the target of the object that dispatched the global event. Since {{#crossLink "EventBroker"}}{{/crossLink}}
* @static
* @public
* @example
* EventBroker.dispatchEvent('change');
*
* // Example: Sending data with the event.
* EventBroker.dispatchEvent('change', {some: 'data'});
*
* // Example: Sending a BaseEvent or custom event object.
* let event = new BaseEvent(BaseEvent.CHANGE);
* event.data = {some: 'data'};
* EventBroker.dispatchEvent(event);
*/
EventBroker.dispatchEvent = function (type, data, scope) {
if (data === void 0) { data = null; }
if (scope === void 0) { scope = EventBroker; }
var event = type;
if (typeof event === 'string') {
event = new BaseEvent_1.default(type, false, false, data);
}
event.target = scope;
event.currentTarget = scope;
EventBroker._eventDispatcher.dispatchEvent(event);
EventBroker._dispatchWaitFor(event);
};
/**
* Helper method to dispatch events on the waitForObject objects.
*
* @method _dispatchWaitFor
* @static
* @private
*/
EventBroker._dispatchWaitFor = function (event) {
var waitForObject;
var eventTypeIndex;
for (var i = EventBroker._waitForList.length - 1; i >= 0; i--) {
waitForObject = EventBroker._waitForList[i];
eventTypeIndex = waitForObject.eventTypes.indexOf(event.type);
if (eventTypeIndex > -1) {
waitForObject.events[eventTypeIndex] = event;
}
if (waitForObject.eventTypes.length === Object.keys(waitForObject.events).length) {
waitForObject.callback.call(waitForObject.scope, waitForObject.events);
waitForObject.events = [];
// If the once value is true we want to remove the listener right after this callback was called.
if (waitForObject.once === true) {
EventBroker._waitForList.splice(i, 1);
}
}
}
};
/**
* Check if EventBroker has a specific event listener already added.
*
* @method hasEventListener
* @param type {String} The type of event.
* @param callback {Function} The listener method to call.
* @param scope {any} The scope of the listener object.
* @return {boolean}
* @static
* @public
* @example
* EventBroker.hasEventListener(BaseEvent.CHANGE, this._handlerMethod, this);
*/
EventBroker.hasEventListener = function (type, callback, scope) {
return EventBroker._eventDispatcher.hasEventListener(type, callback, scope);
};
/**
* Returns and array of all current event types and there current listeners.
*
* @method getEventListeners
* @return {Array<any>}
* @static
* @public
* @example
* const listenerList = this.getEventListeners();
*/
EventBroker.getEventListeners = function () {
return EventBroker._eventDispatcher.getEventListeners();
};
/**
* Prints out each event listener in the console.log
*
* @method print
* @return {string}
* @static
* @public
* @example
* this.printEventListeners();
*
* // [ClassName] is listening for the 'BaseEvent.change' event.
* // [AnotherClassName] is listening for the 'BaseEvent.refresh' event.
*/
EventBroker.printEventListeners = function () {
EventBroker._eventDispatcher.printEventListeners();
};
return EventBroker;
}());
/**
* A reference to the EventDispatcher object.
*
* @property _eventDispatcher
* @type {EventDispatcher}
* @private
* @static
*/
EventBroker._eventDispatcher = new EventDispatcher_1.default();
/**
* A list of wait for objects.
*
* @property _waitForList
* @type {Array<{eventTypes:Array<string>, callback:Function, callbackScope:any, events:Array<any>, once:boolean}>}
* @private
* @static
*/
EventBroker._waitForList = [];
exports.default = EventBroker;
});