@deephaven/golden-layout
Version:
A multi-screen javascript Layout manager
118 lines (115 loc) • 4.09 kB
JavaScript
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/**
* A generic and very fast EventEmitter
* implementation. On top of emitting the
* actual event it emits an
*
* EventEmitter.ALL_EVENT
*
* event for every event triggered. This allows
* to hook into it and proxy events forwards
*
* @constructor
*/
class EventEmitter {
constructor() {
_defineProperty(this, "_mSubscriptions", void 0);
/**
* Alias for unbind
*/
_defineProperty(this, "off", this.unbind);
/**
* Alias for emit
*/
_defineProperty(this, "trigger", this.emit);
this._mSubscriptions = {};
this._mSubscriptions[EventEmitter.ALL_EVENT] = [];
}
/**
* Listen for events
*
* @param eventName The name of the event to listen to
* @param callback The callback to execute when the event occurs
* @param context The value of the this pointer within the callback function
*/
on(eventName, callback, context) {
if (typeof callback !== 'function') {
throw new Error('Tried to listen to event ' + eventName + ' with non-function callback ' + callback);
}
if (!this._mSubscriptions[eventName]) {
this._mSubscriptions[eventName] = [];
}
this._mSubscriptions[eventName].push({
fn: callback,
ctx: context
});
}
/**
* Emit an event and notify listeners
*
* @param eventName The name of the event
* @param args additional arguments that will be passed to the listener
*/
emit(eventName) {
var subs = this._mSubscriptions[eventName];
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
if (subs) {
for (var i = 0; i < subs.length; i++) {
var ctx = subs[i].ctx || {};
try {
subs[i].fn.apply(ctx, args);
} catch (e) {
console.error('Error while emitting event:', e);
}
}
}
args.unshift(eventName);
var allEventSubs = this._mSubscriptions[EventEmitter.ALL_EVENT];
for (var _i = 0; _i < allEventSubs.length; _i++) {
var _ctx = allEventSubs[_i].ctx || {};
try {
allEventSubs[_i].fn.apply(_ctx, args);
} catch (e) {
console.error('Error while emitting event to allEventSubs:', e);
}
}
}
/**
* Removes a listener for an event, or all listeners if no callback and context is provided.
*
* @param eventName The name of the event
* @param callback The previously registered callback method (optional)
* @param context The previously registered context (optional)
*/
unbind(eventName, callback, context) {
if (!this._mSubscriptions[eventName]) {
throw new Error('No subscriptions to unsubscribe for event ' + eventName);
}
var bUnbound = false;
for (var i = 0; i < this._mSubscriptions[eventName].length; i++) {
if ((!callback || this._mSubscriptions[eventName][i].fn === callback) && (!context || context === this._mSubscriptions[eventName][i].ctx)) {
this._mSubscriptions[eventName].splice(i, 1);
bUnbound = true;
}
}
if (bUnbound === false) {
throw new Error('Nothing to unbind for ' + eventName);
}
}
}
/**
* The name of the event that's triggered for every other event
*
* usage
*
* myEmitter.on( EventEmitter.ALL_EVENT, function( eventName, argsArray ){
* //do stuff
* });
*/
_defineProperty(EventEmitter, "ALL_EVENT", '__all');
export default EventEmitter;
//# sourceMappingURL=EventEmitter.js.map