Skip to content

Commit

Permalink
Refactor Room.js in Erizo Client (#939)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcague authored Jun 30, 2017
1 parent 7cb23b6 commit b2ad483
Show file tree
Hide file tree
Showing 10 changed files with 670 additions and 624 deletions.
27 changes: 17 additions & 10 deletions erizo_controller/erizoClient/src/Events.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,48 @@ this.Erizo = this.Erizo || {};
* Class EventDispatcher provides event handling to sub-classes.
* It is inherited from Publisher, Room, etc.
*/
Erizo.EventDispatcher = (specInput) => {
Erizo.EventDispatcher = () => {
const that = {};
const spec = specInput;
// Private vars
spec.dispatcher = {};
spec.dispatcher.eventListeners = {};
const dispatcher = {
eventListeners: {},
};

// Public functions

// It adds an event listener attached to an event type.
that.addEventListener = (eventType, listener) => {
if (spec.dispatcher.eventListeners[eventType] === undefined) {
spec.dispatcher.eventListeners[eventType] = [];
if (dispatcher.eventListeners[eventType] === undefined) {
dispatcher.eventListeners[eventType] = [];
}
spec.dispatcher.eventListeners[eventType].push(listener);
dispatcher.eventListeners[eventType].push(listener);
};

// It removes an available event listener.
that.removeEventListener = (eventType, listener) => {
const index = spec.dispatcher.eventListeners[eventType].indexOf(listener);
const index = dispatcher.eventListeners[eventType].indexOf(listener);
if (index !== -1) {
spec.dispatcher.eventListeners[eventType].splice(index, 1);
dispatcher.eventListeners[eventType].splice(index, 1);
}
};

// It dispatch a new event to the event listeners, based on the type
// of event. All events are intended to be LicodeEvents.
that.dispatchEvent = (event) => {
if (!event || !event.type) {
throw new Error('Undefined event');
}
L.Logger.debug(`Event: ${event.type}`);
const listeners = spec.dispatcher.eventListeners[event.type] || [];
const listeners = dispatcher.eventListeners[event.type] || [];
for (let i = 0; i < listeners.length; i += 1) {
listeners[i](event);
}
};

that.on = that.addEventListener;
that.off = that.removeEventListener;
that.emit = that.dispatchEvent;

return that;
};

Expand Down
Loading

0 comments on commit b2ad483

Please sign in to comment.