Created
July 17, 2018 12:14
-
-
Save sweikenb/3409b2d3ac725a533f2775d918d228aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* EventDispatcherEvent | |
* | |
* @param eventName | |
* @param payload | |
* @constructor | |
*/ | |
var EventDispatcherEvent = function (eventName, payload) { | |
this._stoppped = false; | |
this._name = eventName; | |
// make shure the payload is an array | |
if (!payload || Object.prototype.toString.call(payload) !== '[object Array]') { | |
this._payload = []; | |
} | |
else { | |
this._payload = payload; | |
} | |
}; | |
/** | |
* Returns the event-name | |
* | |
* @returns {*} | |
*/ | |
EventDispatcherEvent.prototype.getName = function () { | |
return this._name; | |
}; | |
/** | |
* Stops the event to it won't be dispatches to the remaining callbacks | |
* | |
* @returns {EventDispatcherEvent} | |
*/ | |
EventDispatcherEvent.prototype.stopPropagation = function () { | |
this._stoppped = true; | |
return this; | |
}; | |
/** | |
* Returns the stopped flag | |
* | |
* @returns {boolean} | |
*/ | |
EventDispatcherEvent.prototype.isStopped = function () { | |
return (true === this._stoppped); | |
}; | |
/** | |
* Executs the given callback for this event (if it's not stopped yet) | |
* | |
* @param callback | |
* @returns {EventDispatcherEvent} | |
*/ | |
EventDispatcherEvent.prototype.executeCallback = function (callback) { | |
// event already stopped? | |
if (!this.isStopped()) { | |
// create a copy if the payload | |
var args = this._payload.slice(0); | |
// add the event to the end of the callback args | |
args.push(this); | |
// run the callback | |
callback.apply(null, args) | |
} | |
return this; | |
}; | |
/******************************************************************************************/ | |
/** | |
* EventDispatcher | |
* | |
* @constructor | |
*/ | |
var EventDispatcher = function () { | |
this.registry = {}; | |
}; | |
/** | |
* Registers the given callback for the event | |
* | |
* @param eventName | |
* @param callback | |
* @returns {EventDispatcher} | |
*/ | |
EventDispatcher.prototype.on = function (eventName, callback) { | |
if (callback) { | |
if (typeof this.registry[eventName] === 'undefined') { | |
this.registry[eventName] = []; | |
} | |
this.registry[eventName].push(callback); | |
} | |
return this; | |
}; | |
/** | |
* Unregisters the given callback for the event, if no callback is provided ALL callbacks will be removed | |
* | |
* @param eventName | |
* @param callback | |
* @returns {EventDispatcher} | |
*/ | |
EventDispatcher.prototype.off = function (eventName, callback) { | |
if (typeof this.registry[eventName] !== 'undefined') { | |
if (callback) { | |
for (var i = 0; i < this.registry[eventName].length; i++) { | |
if (this.registry[eventName][i] === callback) { | |
delete this.registry[eventName][i]; | |
i--; | |
} | |
} | |
} | |
else { | |
delete this.registry[eventName]; | |
} | |
} | |
return this; | |
}; | |
/** | |
* Fires an event and dispatch payload to all events | |
* | |
* @param eventName | |
* @param payload | |
* @returns {EventDispatcher} | |
*/ | |
EventDispatcher.prototype.fire = function (eventName, payload) { | |
if (typeof this.registry[eventName] !== 'undefined') { | |
var fireEvent = new EventDispatcherEvent(eventName, payload); | |
for (var i = 0; i < this.registry[eventName].length; i++) { | |
fireEvent.executeCallback(this.registry[eventName][i]); | |
} | |
} | |
return this; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment