metal-events
Advanced tools
Comparing version 1.0.0-rc.5 to 1.0.0
@@ -7,4 +7,2 @@ 'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _metal = require('metal'); | ||
@@ -42,3 +40,3 @@ | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(EventEmitter).call(this)); | ||
var _this = _possibleConstructorReturn(this, _Disposable.call(this)); | ||
@@ -77,361 +75,340 @@ _this.events_ = []; | ||
_createClass(EventEmitter, [{ | ||
key: 'addListener', | ||
value: function addListener(events, listener, opt_default) { | ||
this.validateListener_(listener); | ||
EventEmitter.prototype.addListener = function addListener(events, listener, opt_default) { | ||
this.validateListener_(listener); | ||
events = this.normalizeEvents_(events); | ||
for (var i = 0; i < events.length; i++) { | ||
this.addSingleListener_(events[i], listener, opt_default); | ||
} | ||
return new _EventHandle2.default(this, events, listener); | ||
events = this.normalizeEvents_(events); | ||
for (var i = 0; i < events.length; i++) { | ||
this.addSingleListener_(events[i], listener, opt_default); | ||
} | ||
/** | ||
* Adds a listener to the end of the listeners array for a single event. | ||
* @param {string} event | ||
* @param {!Function} listener | ||
* @param {boolean} opt_default Flag indicating if this listener is a default | ||
* action for this event. Default actions are run last, and only if no previous | ||
* listener call `preventDefault()` on the received event facade. | ||
* @return {!EventHandle} Can be used to remove the listener. | ||
* @param {Function=} opt_origin The original function that was added as a | ||
* listener, if there is any. | ||
* @protected | ||
*/ | ||
return new _EventHandle2.default(this, events, listener); | ||
}; | ||
}, { | ||
key: 'addSingleListener_', | ||
value: function addSingleListener_(event, listener, opt_default, opt_origin) { | ||
this.emit('newListener', event, listener); | ||
/** | ||
* Adds a listener to the end of the listeners array for a single event. | ||
* @param {string} event | ||
* @param {!Function} listener | ||
* @param {boolean} opt_default Flag indicating if this listener is a default | ||
* action for this event. Default actions are run last, and only if no previous | ||
* listener call `preventDefault()` on the received event facade. | ||
* @return {!EventHandle} Can be used to remove the listener. | ||
* @param {Function=} opt_origin The original function that was added as a | ||
* listener, if there is any. | ||
* @protected | ||
*/ | ||
if (!this.events_[event]) { | ||
this.events_[event] = []; | ||
} | ||
this.events_[event].push({ | ||
default: opt_default, | ||
fn: listener, | ||
origin: opt_origin | ||
}); | ||
var listeners = this.events_[event]; | ||
if (listeners.length > this.maxListeners_ && !listeners.warned) { | ||
console.warn('Possible EventEmitter memory leak detected. %d listeners added ' + 'for event %s. Use emitter.setMaxListeners() to increase limit.', listeners.length, event); | ||
listeners.warned = true; | ||
} | ||
EventEmitter.prototype.addSingleListener_ = function addSingleListener_(event, listener, opt_default, opt_origin) { | ||
this.emit('newListener', event, listener); | ||
if (!this.events_[event]) { | ||
this.events_[event] = []; | ||
} | ||
this.events_[event].push({ | ||
default: opt_default, | ||
fn: listener, | ||
origin: opt_origin | ||
}); | ||
/** | ||
* Disposes of this instance's object references. | ||
* @override | ||
*/ | ||
}, { | ||
key: 'disposeInternal', | ||
value: function disposeInternal() { | ||
this.events_ = []; | ||
var listeners = this.events_[event]; | ||
if (listeners.length > this.maxListeners_ && !listeners.warned) { | ||
console.warn('Possible EventEmitter memory leak detected. %d listeners added ' + 'for event %s. Use emitter.setMaxListeners() to increase limit.', listeners.length, event); | ||
listeners.warned = true; | ||
} | ||
}; | ||
/** | ||
* Execute each of the listeners in order with the supplied arguments. | ||
* @param {string} event | ||
* @param {*} opt_args [arg1], [arg2], [...] | ||
* @return {boolean} Returns true if event had listeners, false otherwise. | ||
*/ | ||
/** | ||
* Disposes of this instance's object references. | ||
* @override | ||
*/ | ||
}, { | ||
key: 'emit', | ||
value: function emit(event) { | ||
var args = _metal.array.slice(arguments, 1); | ||
var listeners = (this.events_[event] || []).concat(); | ||
var facade; | ||
if (this.getShouldUseFacade()) { | ||
facade = { | ||
preventDefault: function preventDefault() { | ||
facade.preventedDefault = true; | ||
}, | ||
target: this, | ||
type: event | ||
}; | ||
args.push(facade); | ||
} | ||
EventEmitter.prototype.disposeInternal = function disposeInternal() { | ||
this.events_ = []; | ||
}; | ||
var defaultListeners = []; | ||
for (var i = 0; i < listeners.length; i++) { | ||
if (listeners[i].default) { | ||
defaultListeners.push(listeners[i]); | ||
} else { | ||
listeners[i].fn.apply(this, args); | ||
} | ||
} | ||
if (!facade || !facade.preventedDefault) { | ||
for (var j = 0; j < defaultListeners.length; j++) { | ||
defaultListeners[j].fn.apply(this, args); | ||
} | ||
} | ||
/** | ||
* Execute each of the listeners in order with the supplied arguments. | ||
* @param {string} event | ||
* @param {*} opt_args [arg1], [arg2], [...] | ||
* @return {boolean} Returns true if event had listeners, false otherwise. | ||
*/ | ||
if (event !== '*') { | ||
this.emit.apply(this, ['*', event].concat(args)); | ||
} | ||
return listeners.length > 0; | ||
EventEmitter.prototype.emit = function emit(event) { | ||
var args = _metal.array.slice(arguments, 1); | ||
var listeners = (this.events_[event] || []).concat(); | ||
var facade; | ||
if (this.getShouldUseFacade()) { | ||
facade = { | ||
preventDefault: function preventDefault() { | ||
facade.preventedDefault = true; | ||
}, | ||
target: this, | ||
type: event | ||
}; | ||
args.push(facade); | ||
} | ||
/** | ||
* Gets the configuration option which determines if an event facade should | ||
* be sent as a param of listeners when emitting events. If set to true, the | ||
* facade will be passed as the first argument of the listener. | ||
* @return {boolean} | ||
*/ | ||
var defaultListeners = []; | ||
for (var i = 0; i < listeners.length; i++) { | ||
if (listeners[i].default) { | ||
defaultListeners.push(listeners[i]); | ||
} else { | ||
listeners[i].fn.apply(this, args); | ||
} | ||
} | ||
if (!facade || !facade.preventedDefault) { | ||
for (var j = 0; j < defaultListeners.length; j++) { | ||
defaultListeners[j].fn.apply(this, args); | ||
} | ||
} | ||
}, { | ||
key: 'getShouldUseFacade', | ||
value: function getShouldUseFacade() { | ||
return this.shouldUseFacade_; | ||
if (event !== '*') { | ||
this.emit.apply(this, ['*', event].concat(args)); | ||
} | ||
/** | ||
* Returns an array of listeners for the specified event. | ||
* @param {string} event | ||
* @return {Array} Array of listeners. | ||
*/ | ||
return listeners.length > 0; | ||
}; | ||
}, { | ||
key: 'listeners', | ||
value: function listeners(event) { | ||
return (this.events_[event] || []).map(function (listener) { | ||
return listener.fn; | ||
}); | ||
} | ||
/** | ||
* Gets the configuration option which determines if an event facade should | ||
* be sent as a param of listeners when emitting events. If set to true, the | ||
* facade will be passed as the first argument of the listener. | ||
* @return {boolean} | ||
*/ | ||
/** | ||
* Adds a listener that will be invoked a fixed number of times for the | ||
* events. After each event is triggered the specified amount of times, the | ||
* listener is removed for it. | ||
* @param {!(Array|string)} events | ||
* @param {number} amount The amount of times this event should be listened | ||
* to. | ||
* @param {!Function} listener | ||
* @return {!EventHandle} Can be used to remove the listener. | ||
*/ | ||
}, { | ||
key: 'many', | ||
value: function many(events, amount, listener) { | ||
events = this.normalizeEvents_(events); | ||
for (var i = 0; i < events.length; i++) { | ||
this.many_(events[i], amount, listener); | ||
} | ||
EventEmitter.prototype.getShouldUseFacade = function getShouldUseFacade() { | ||
return this.shouldUseFacade_; | ||
}; | ||
return new _EventHandle2.default(this, events, listener); | ||
} | ||
/** | ||
* Returns an array of listeners for the specified event. | ||
* @param {string} event | ||
* @return {Array} Array of listeners. | ||
*/ | ||
/** | ||
* Adds a listener that will be invoked a fixed number of times for a single | ||
* event. After the event is triggered the specified amount of times, the | ||
* listener is removed. | ||
* @param {string} event | ||
* @param {number} amount The amount of times this event should be listened | ||
* to. | ||
* @param {!Function} listener | ||
* @protected | ||
*/ | ||
}, { | ||
key: 'many_', | ||
value: function many_(event, amount, listener) { | ||
var self = this; | ||
EventEmitter.prototype.listeners = function listeners(event) { | ||
return (this.events_[event] || []).map(function (listener) { | ||
return listener.fn; | ||
}); | ||
}; | ||
if (amount <= 0) { | ||
return; | ||
} | ||
/** | ||
* Adds a listener that will be invoked a fixed number of times for the | ||
* events. After each event is triggered the specified amount of times, the | ||
* listener is removed for it. | ||
* @param {!(Array|string)} events | ||
* @param {number} amount The amount of times this event should be listened | ||
* to. | ||
* @param {!Function} listener | ||
* @return {!EventHandle} Can be used to remove the listener. | ||
*/ | ||
function handlerInternal() { | ||
if (--amount === 0) { | ||
self.removeListener(event, handlerInternal); | ||
} | ||
listener.apply(self, arguments); | ||
} | ||
self.addSingleListener_(event, handlerInternal, false, listener); | ||
EventEmitter.prototype.many = function many(events, amount, listener) { | ||
events = this.normalizeEvents_(events); | ||
for (var i = 0; i < events.length; i++) { | ||
this.many_(events[i], amount, listener); | ||
} | ||
/** | ||
* Checks if a listener object matches the given listener function. To match, | ||
* it needs to either point to that listener or have it as its origin. | ||
* @param {!Object} listenerObj | ||
* @param {!Function} listener | ||
* @return {boolean} | ||
* @protected | ||
*/ | ||
return new _EventHandle2.default(this, events, listener); | ||
}; | ||
}, { | ||
key: 'matchesListener_', | ||
value: function matchesListener_(listenerObj, listener) { | ||
return listenerObj.fn === listener || listenerObj.origin && listenerObj.origin === listener; | ||
} | ||
/** | ||
* Adds a listener that will be invoked a fixed number of times for a single | ||
* event. After the event is triggered the specified amount of times, the | ||
* listener is removed. | ||
* @param {string} event | ||
* @param {number} amount The amount of times this event should be listened | ||
* to. | ||
* @param {!Function} listener | ||
* @protected | ||
*/ | ||
/** | ||
* Converts the parameter to an array if only one event is given. | ||
* @param {!(Array|string)} events | ||
* @return {!Array} | ||
* @protected | ||
*/ | ||
}, { | ||
key: 'normalizeEvents_', | ||
value: function normalizeEvents_(events) { | ||
return _metal.core.isString(events) ? [events] : events; | ||
EventEmitter.prototype.many_ = function many_(event, amount, listener) { | ||
var self = this; | ||
if (amount <= 0) { | ||
return; | ||
} | ||
/** | ||
* Removes a listener for the specified events. | ||
* Caution: changes array indices in the listener array behind the listener. | ||
* @param {!(Array|string)} events | ||
* @param {!Function} listener | ||
* @return {!Object} Returns emitter, so calls can be chained. | ||
*/ | ||
function handlerInternal() { | ||
if (--amount === 0) { | ||
self.removeListener(event, handlerInternal); | ||
} | ||
listener.apply(self, arguments); | ||
} | ||
}, { | ||
key: 'off', | ||
value: function off(events, listener) { | ||
this.validateListener_(listener); | ||
self.addSingleListener_(event, handlerInternal, false, listener); | ||
}; | ||
events = this.normalizeEvents_(events); | ||
for (var i = 0; i < events.length; i++) { | ||
var listenerObjs = this.events_[events[i]] || []; | ||
this.removeMatchingListenerObjs_(listenerObjs, listener); | ||
} | ||
/** | ||
* Checks if a listener object matches the given listener function. To match, | ||
* it needs to either point to that listener or have it as its origin. | ||
* @param {!Object} listenerObj | ||
* @param {!Function} listener | ||
* @return {boolean} | ||
* @protected | ||
*/ | ||
return this; | ||
} | ||
/** | ||
* Adds a listener to the end of the listeners array for the specified events. | ||
* @param {!(Array|string)} events | ||
* @param {!Function} listener | ||
* @return {!EventHandle} Can be used to remove the listener. | ||
*/ | ||
EventEmitter.prototype.matchesListener_ = function matchesListener_(listenerObj, listener) { | ||
return listenerObj.fn === listener || listenerObj.origin && listenerObj.origin === listener; | ||
}; | ||
}, { | ||
key: 'on', | ||
value: function on() { | ||
return this.addListener.apply(this, arguments); | ||
} | ||
/** | ||
* Converts the parameter to an array if only one event is given. | ||
* @param {!(Array|string)} events | ||
* @return {!Array} | ||
* @protected | ||
*/ | ||
/** | ||
* Adds a one time listener for the events. This listener is invoked only the | ||
* next time each event is fired, after which it is removed. | ||
* @param {!(Array|string)} events | ||
* @param {!Function} listener | ||
* @return {!EventHandle} Can be used to remove the listener. | ||
*/ | ||
}, { | ||
key: 'once', | ||
value: function once(events, listener) { | ||
return this.many(events, 1, listener); | ||
EventEmitter.prototype.normalizeEvents_ = function normalizeEvents_(events) { | ||
return _metal.core.isString(events) ? [events] : events; | ||
}; | ||
/** | ||
* Removes a listener for the specified events. | ||
* Caution: changes array indices in the listener array behind the listener. | ||
* @param {!(Array|string)} events | ||
* @param {!Function} listener | ||
* @return {!Object} Returns emitter, so calls can be chained. | ||
*/ | ||
EventEmitter.prototype.off = function off(events, listener) { | ||
this.validateListener_(listener); | ||
events = this.normalizeEvents_(events); | ||
for (var i = 0; i < events.length; i++) { | ||
var listenerObjs = this.events_[events[i]] || []; | ||
this.removeMatchingListenerObjs_(listenerObjs, listener); | ||
} | ||
/** | ||
* Removes all listeners, or those of the specified events. It's not a good | ||
* idea to remove listeners that were added elsewhere in the code, | ||
* especially when it's on an emitter that you didn't create. | ||
* @param {(Array|string)=} opt_events | ||
* @return {!Object} Returns emitter, so calls can be chained. | ||
*/ | ||
return this; | ||
}; | ||
}, { | ||
key: 'removeAllListeners', | ||
value: function removeAllListeners(opt_events) { | ||
if (opt_events) { | ||
var events = this.normalizeEvents_(opt_events); | ||
for (var i = 0; i < events.length; i++) { | ||
this.events_[events[i]] = null; | ||
} | ||
} else { | ||
this.events_ = {}; | ||
/** | ||
* Adds a listener to the end of the listeners array for the specified events. | ||
* @param {!(Array|string)} events | ||
* @param {!Function} listener | ||
* @return {!EventHandle} Can be used to remove the listener. | ||
*/ | ||
EventEmitter.prototype.on = function on() { | ||
return this.addListener.apply(this, arguments); | ||
}; | ||
/** | ||
* Adds a one time listener for the events. This listener is invoked only the | ||
* next time each event is fired, after which it is removed. | ||
* @param {!(Array|string)} events | ||
* @param {!Function} listener | ||
* @return {!EventHandle} Can be used to remove the listener. | ||
*/ | ||
EventEmitter.prototype.once = function once(events, listener) { | ||
return this.many(events, 1, listener); | ||
}; | ||
/** | ||
* Removes all listeners, or those of the specified events. It's not a good | ||
* idea to remove listeners that were added elsewhere in the code, | ||
* especially when it's on an emitter that you didn't create. | ||
* @param {(Array|string)=} opt_events | ||
* @return {!Object} Returns emitter, so calls can be chained. | ||
*/ | ||
EventEmitter.prototype.removeAllListeners = function removeAllListeners(opt_events) { | ||
if (opt_events) { | ||
var events = this.normalizeEvents_(opt_events); | ||
for (var i = 0; i < events.length; i++) { | ||
this.events_[events[i]] = null; | ||
} | ||
return this; | ||
} else { | ||
this.events_ = {}; | ||
} | ||
return this; | ||
}; | ||
/** | ||
* Removes all listener objects from the given array that match the given | ||
* listener function. | ||
* @param {!Array.<Object>} listenerObjs | ||
* @param {!Function} listener | ||
* @protected | ||
*/ | ||
/** | ||
* Removes all listener objects from the given array that match the given | ||
* listener function. | ||
* @param {!Array.<Object>} listenerObjs | ||
* @param {!Function} listener | ||
* @protected | ||
*/ | ||
}, { | ||
key: 'removeMatchingListenerObjs_', | ||
value: function removeMatchingListenerObjs_(listenerObjs, listener) { | ||
for (var i = listenerObjs.length - 1; i >= 0; i--) { | ||
if (this.matchesListener_(listenerObjs[i], listener)) { | ||
listenerObjs.splice(i, 1); | ||
} | ||
EventEmitter.prototype.removeMatchingListenerObjs_ = function removeMatchingListenerObjs_(listenerObjs, listener) { | ||
for (var i = listenerObjs.length - 1; i >= 0; i--) { | ||
if (this.matchesListener_(listenerObjs[i], listener)) { | ||
listenerObjs.splice(i, 1); | ||
} | ||
} | ||
}; | ||
/** | ||
* Removes a listener for the specified events. | ||
* Caution: changes array indices in the listener array behind the listener. | ||
* @param {!(Array|string)} events | ||
* @param {!Function} listener | ||
* @return {!Object} Returns emitter, so calls can be chained. | ||
*/ | ||
/** | ||
* Removes a listener for the specified events. | ||
* Caution: changes array indices in the listener array behind the listener. | ||
* @param {!(Array|string)} events | ||
* @param {!Function} listener | ||
* @return {!Object} Returns emitter, so calls can be chained. | ||
*/ | ||
}, { | ||
key: 'removeListener', | ||
value: function removeListener() { | ||
return this.off.apply(this, arguments); | ||
} | ||
/** | ||
* By default EventEmitters will print a warning if more than 10 listeners | ||
* are added for a particular event. This is a useful default which helps | ||
* finding memory leaks. Obviously not all Emitters should be limited to 10. | ||
* This function allows that to be increased. Set to zero for unlimited. | ||
* @param {number} max The maximum number of listeners. | ||
* @return {!Object} Returns emitter, so calls can be chained. | ||
*/ | ||
EventEmitter.prototype.removeListener = function removeListener() { | ||
return this.off.apply(this, arguments); | ||
}; | ||
}, { | ||
key: 'setMaxListeners', | ||
value: function setMaxListeners(max) { | ||
this.maxListeners_ = max; | ||
return this; | ||
} | ||
/** | ||
* By default EventEmitters will print a warning if more than 10 listeners | ||
* are added for a particular event. This is a useful default which helps | ||
* finding memory leaks. Obviously not all Emitters should be limited to 10. | ||
* This function allows that to be increased. Set to zero for unlimited. | ||
* @param {number} max The maximum number of listeners. | ||
* @return {!Object} Returns emitter, so calls can be chained. | ||
*/ | ||
/** | ||
* Sets the configuration option which determines if an event facade should | ||
* be sent as a param of listeners when emitting events. If set to true, the | ||
* facade will be passed as the first argument of the listener. | ||
* @param {boolean} shouldUseFacade | ||
* @return {!Object} Returns emitter, so calls can be chained. | ||
*/ | ||
}, { | ||
key: 'setShouldUseFacade', | ||
value: function setShouldUseFacade(shouldUseFacade) { | ||
this.shouldUseFacade_ = shouldUseFacade; | ||
return this; | ||
} | ||
EventEmitter.prototype.setMaxListeners = function setMaxListeners(max) { | ||
this.maxListeners_ = max; | ||
return this; | ||
}; | ||
/** | ||
* Checks if the given listener is valid, throwing an exception when it's not. | ||
* @param {*} listener | ||
* @protected | ||
*/ | ||
/** | ||
* Sets the configuration option which determines if an event facade should | ||
* be sent as a param of listeners when emitting events. If set to true, the | ||
* facade will be passed as the first argument of the listener. | ||
* @param {boolean} shouldUseFacade | ||
* @return {!Object} Returns emitter, so calls can be chained. | ||
*/ | ||
}, { | ||
key: 'validateListener_', | ||
value: function validateListener_(listener) { | ||
if (!_metal.core.isFunction(listener)) { | ||
throw new TypeError('Listener must be a function'); | ||
} | ||
EventEmitter.prototype.setShouldUseFacade = function setShouldUseFacade(shouldUseFacade) { | ||
this.shouldUseFacade_ = shouldUseFacade; | ||
return this; | ||
}; | ||
/** | ||
* Checks if the given listener is valid, throwing an exception when it's not. | ||
* @param {*} listener | ||
* @protected | ||
*/ | ||
EventEmitter.prototype.validateListener_ = function validateListener_(listener) { | ||
if (!_metal.core.isFunction(listener)) { | ||
throw new TypeError('Listener must be a function'); | ||
} | ||
}]); | ||
}; | ||
@@ -438,0 +415,0 @@ return EventEmitter; |
@@ -7,4 +7,2 @@ 'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _metal = require('metal'); | ||
@@ -45,3 +43,3 @@ | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(EventEmitterProxy).call(this)); | ||
var _this = _possibleConstructorReturn(this, _Disposable.call(this)); | ||
@@ -102,143 +100,131 @@ _this.blacklist_ = opt_blacklist || {}; | ||
_createClass(EventEmitterProxy, [{ | ||
key: 'addListener_', | ||
value: function addListener_(event, listener) { | ||
return this.originEmitter_.on(event, listener); | ||
} | ||
EventEmitterProxy.prototype.addListener_ = function addListener_(event, listener) { | ||
return this.originEmitter_.on(event, listener); | ||
}; | ||
/** | ||
* Adds the proxy listener for the given event. | ||
* @param {string} event | ||
* @return {!EventHandle} The listened event's handle. | ||
* @protected | ||
*/ | ||
/** | ||
* Adds the proxy listener for the given event. | ||
* @param {string} event | ||
* @return {!EventHandle} The listened event's handle. | ||
* @protected | ||
*/ | ||
}, { | ||
key: 'addListenerForEvent_', | ||
value: function addListenerForEvent_(event) { | ||
return this.addListener_(event, this.emitOnTarget_.bind(this, event)); | ||
} | ||
/** | ||
* @inheritDoc | ||
*/ | ||
EventEmitterProxy.prototype.addListenerForEvent_ = function addListenerForEvent_(event) { | ||
return this.addListener_(event, this.emitOnTarget_.bind(this, event)); | ||
}; | ||
}, { | ||
key: 'disposeInternal', | ||
value: function disposeInternal() { | ||
this.removeListeners_(); | ||
this.proxiedEvents_ = null; | ||
this.originEmitter_ = null; | ||
this.targetEmitter_ = null; | ||
} | ||
/** | ||
* @inheritDoc | ||
*/ | ||
/** | ||
* Emits the specified event type on the target emitter. | ||
* @param {string} eventType | ||
* @protected | ||
*/ | ||
}, { | ||
key: 'emitOnTarget_', | ||
value: function emitOnTarget_(eventType) { | ||
var args = [eventType].concat(_metal.array.slice(arguments, 1)); | ||
this.targetEmitter_.emit.apply(this.targetEmitter_, args); | ||
} | ||
EventEmitterProxy.prototype.disposeInternal = function disposeInternal() { | ||
this.removeListeners_(); | ||
this.proxiedEvents_ = null; | ||
this.originEmitter_ = null; | ||
this.targetEmitter_ = null; | ||
}; | ||
/** | ||
* Proxies the given event from the origin to the target emitter. | ||
* @param {string} event | ||
*/ | ||
/** | ||
* Emits the specified event type on the target emitter. | ||
* @param {string} eventType | ||
* @protected | ||
*/ | ||
}, { | ||
key: 'proxyEvent', | ||
value: function proxyEvent(event) { | ||
if (this.shouldProxyEvent_(event)) { | ||
this.tryToAddListener_(event); | ||
} | ||
} | ||
/** | ||
* Removes the proxy listener for all events. | ||
* @protected | ||
*/ | ||
EventEmitterProxy.prototype.emitOnTarget_ = function emitOnTarget_(eventType) { | ||
var args = [eventType].concat(_metal.array.slice(arguments, 1)); | ||
this.targetEmitter_.emit.apply(this.targetEmitter_, args); | ||
}; | ||
}, { | ||
key: 'removeListeners_', | ||
value: function removeListeners_() { | ||
var events = Object.keys(this.proxiedEvents_); | ||
for (var i = 0; i < events.length; i++) { | ||
this.proxiedEvents_[events[i]].removeListener(); | ||
} | ||
this.proxiedEvents_ = {}; | ||
this.pendingEvents_ = []; | ||
/** | ||
* Proxies the given event from the origin to the target emitter. | ||
* @param {string} event | ||
*/ | ||
EventEmitterProxy.prototype.proxyEvent = function proxyEvent(event) { | ||
if (this.shouldProxyEvent_(event)) { | ||
this.tryToAddListener_(event); | ||
} | ||
}; | ||
/** | ||
* Changes the origin emitter. This automatically detaches any events that | ||
* were already being proxied from the previous emitter, and starts proxying | ||
* them on the new emitter instead. | ||
* @param {!EventEmitter} originEmitter | ||
*/ | ||
/** | ||
* Removes the proxy listener for all events. | ||
* @protected | ||
*/ | ||
}, { | ||
key: 'setOriginEmitter', | ||
value: function setOriginEmitter(originEmitter) { | ||
var _this2 = this; | ||
var events = this.originEmitter_ ? Object.keys(this.proxiedEvents_) : this.pendingEvents_; | ||
this.removeListeners_(); | ||
this.originEmitter_ = originEmitter; | ||
events.forEach(function (event) { | ||
return _this2.proxyEvent(event); | ||
}); | ||
EventEmitterProxy.prototype.removeListeners_ = function removeListeners_() { | ||
var events = Object.keys(this.proxiedEvents_); | ||
for (var i = 0; i < events.length; i++) { | ||
this.proxiedEvents_[events[i]].removeListener(); | ||
} | ||
this.proxiedEvents_ = {}; | ||
this.pendingEvents_ = []; | ||
}; | ||
/** | ||
* Checks if the given event should be proxied. | ||
* @param {string} event | ||
* @return {boolean} | ||
* @protected | ||
*/ | ||
/** | ||
* Changes the origin emitter. This automatically detaches any events that | ||
* were already being proxied from the previous emitter, and starts proxying | ||
* them on the new emitter instead. | ||
* @param {!EventEmitter} originEmitter | ||
*/ | ||
}, { | ||
key: 'shouldProxyEvent_', | ||
value: function shouldProxyEvent_(event) { | ||
if (this.whitelist_ && !this.whitelist_[event]) { | ||
return false; | ||
} | ||
if (this.blacklist_[event]) { | ||
return false; | ||
} | ||
return !this.proxiedEvents_[event]; | ||
} | ||
/** | ||
* Starts proxying all events from the origin to the target emitter. | ||
* @protected | ||
*/ | ||
EventEmitterProxy.prototype.setOriginEmitter = function setOriginEmitter(originEmitter) { | ||
var _this2 = this; | ||
}, { | ||
key: 'startProxy_', | ||
value: function startProxy_() { | ||
this.targetEmitter_.on('newListener', this.proxyEvent.bind(this)); | ||
var events = this.originEmitter_ ? Object.keys(this.proxiedEvents_) : this.pendingEvents_; | ||
this.removeListeners_(); | ||
this.originEmitter_ = originEmitter; | ||
events.forEach(function (event) { | ||
return _this2.proxyEvent(event); | ||
}); | ||
}; | ||
/** | ||
* Checks if the given event should be proxied. | ||
* @param {string} event | ||
* @return {boolean} | ||
* @protected | ||
*/ | ||
EventEmitterProxy.prototype.shouldProxyEvent_ = function shouldProxyEvent_(event) { | ||
if (this.whitelist_ && !this.whitelist_[event]) { | ||
return false; | ||
} | ||
if (this.blacklist_[event]) { | ||
return false; | ||
} | ||
return !this.proxiedEvents_[event]; | ||
}; | ||
/** | ||
* Adds a listener to the origin emitter, if it exists. Otherwise, stores | ||
* the pending listener so it can be used on a future origin emitter. | ||
* @param {string} event | ||
* @protected | ||
*/ | ||
/** | ||
* Starts proxying all events from the origin to the target emitter. | ||
* @protected | ||
*/ | ||
}, { | ||
key: 'tryToAddListener_', | ||
value: function tryToAddListener_(event) { | ||
if (this.originEmitter_) { | ||
this.proxiedEvents_[event] = this.addListenerForEvent_(event); | ||
} else { | ||
this.pendingEvents_.push(event); | ||
} | ||
EventEmitterProxy.prototype.startProxy_ = function startProxy_() { | ||
this.targetEmitter_.on('newListener', this.proxyEvent.bind(this)); | ||
}; | ||
/** | ||
* Adds a listener to the origin emitter, if it exists. Otherwise, stores | ||
* the pending listener so it can be used on a future origin emitter. | ||
* @param {string} event | ||
* @protected | ||
*/ | ||
EventEmitterProxy.prototype.tryToAddListener_ = function tryToAddListener_(event) { | ||
if (this.originEmitter_) { | ||
this.proxiedEvents_[event] = this.addListenerForEvent_(event); | ||
} else { | ||
this.pendingEvents_.push(event); | ||
} | ||
}]); | ||
}; | ||
@@ -245,0 +231,0 @@ return EventEmitterProxy; |
@@ -7,4 +7,2 @@ 'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _metal = require('metal'); | ||
@@ -43,3 +41,3 @@ | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(EventHandle).call(this)); | ||
var _this = _possibleConstructorReturn(this, _Disposable.call(this)); | ||
@@ -70,22 +68,18 @@ _this.emitter_ = emitter; | ||
_createClass(EventHandle, [{ | ||
key: 'disposeInternal', | ||
value: function disposeInternal() { | ||
this.removeListener(); | ||
this.emitter_ = null; | ||
this.listener_ = null; | ||
} | ||
EventHandle.prototype.disposeInternal = function disposeInternal() { | ||
this.removeListener(); | ||
this.emitter_ = null; | ||
this.listener_ = null; | ||
}; | ||
/** | ||
* Removes the listener subscription from the emitter. | ||
*/ | ||
/** | ||
* Removes the listener subscription from the emitter. | ||
*/ | ||
}, { | ||
key: 'removeListener', | ||
value: function removeListener() { | ||
if (!this.emitter_.isDisposed()) { | ||
this.emitter_.removeListener(this.event_, this.listener_); | ||
} | ||
EventHandle.prototype.removeListener = function removeListener() { | ||
if (!this.emitter_.isDisposed()) { | ||
this.emitter_.removeListener(this.event_, this.listener_); | ||
} | ||
}]); | ||
}; | ||
@@ -92,0 +86,0 @@ return EventHandle; |
@@ -7,4 +7,2 @@ 'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _metal = require('metal'); | ||
@@ -38,3 +36,3 @@ | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(EventHandler).call(this)); | ||
var _this = _possibleConstructorReturn(this, _Disposable.call(this)); | ||
@@ -52,36 +50,31 @@ _this.eventHandles_ = []; | ||
_createClass(EventHandler, [{ | ||
key: 'add', | ||
value: function add() { | ||
for (var i = 0; i < arguments.length; i++) { | ||
this.eventHandles_.push(arguments[i]); | ||
} | ||
EventHandler.prototype.add = function add() { | ||
for (var i = 0; i < arguments.length; i++) { | ||
this.eventHandles_.push(arguments[i]); | ||
} | ||
}; | ||
/** | ||
* Disposes of this instance's object references. | ||
* @override | ||
*/ | ||
/** | ||
* Disposes of this instance's object references. | ||
* @override | ||
*/ | ||
}, { | ||
key: 'disposeInternal', | ||
value: function disposeInternal() { | ||
this.eventHandles_ = null; | ||
} | ||
/** | ||
* Removes all listeners that have been added through the `add` method. | ||
*/ | ||
EventHandler.prototype.disposeInternal = function disposeInternal() { | ||
this.eventHandles_ = null; | ||
}; | ||
}, { | ||
key: 'removeAllListeners', | ||
value: function removeAllListeners() { | ||
for (var i = 0; i < this.eventHandles_.length; i++) { | ||
this.eventHandles_[i].removeListener(); | ||
} | ||
/** | ||
* Removes all listeners that have been added through the `add` method. | ||
*/ | ||
this.eventHandles_ = []; | ||
EventHandler.prototype.removeAllListeners = function removeAllListeners() { | ||
for (var i = 0; i < this.eventHandles_.length; i++) { | ||
this.eventHandles_[i].removeListener(); | ||
} | ||
}]); | ||
this.eventHandles_ = []; | ||
}; | ||
return EventHandler; | ||
@@ -88,0 +81,0 @@ }(_metal.Disposable); |
{ | ||
"name": "metal-events", | ||
"version": "1.0.0-rc.5", | ||
"version": "1.0.0", | ||
"description": "Classes responsible for emitting and listening to events", | ||
"license": "BSD", | ||
"repository": "metal/metal-events", | ||
"repository": "https://github.com/metal/metal/tree/master/packages/metal-events", | ||
"engines": { | ||
@@ -15,9 +15,7 @@ "node": ">=0.12.0", | ||
"lib", | ||
"src", | ||
"test" | ||
"src" | ||
], | ||
"scripts": { | ||
"compile": "babel --presets es2015 -d lib/ src/", | ||
"prepublish": "npm run compile", | ||
"test": "gulp test" | ||
"compile": "babel --presets metal -d lib/ src/", | ||
"prepublish": "npm run compile" | ||
}, | ||
@@ -28,12 +26,8 @@ "keywords": [ | ||
"dependencies": { | ||
"bootstrap": "^3.3.6", | ||
"metal": "^1.0.0-rc.1" | ||
"metal": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.4.5", | ||
"babel-preset-es2015": "^6.3.13", | ||
"gulp": "^3.8.11", | ||
"gulp-metal": "^1.0.0", | ||
"sinon": "^1.17.3" | ||
"babel-preset-metal": "^4.0.0" | ||
} | ||
} |
# metal-events | ||
Classes responsible for emitting and listening to events. | ||
## Setup | ||
1. Install NodeJS >= [v0.12.0](http://nodejs.org/dist/v0.12.0/), if you don't have it yet. | ||
2. Install global dependencies: | ||
``` | ||
[sudo] npm install -g gulp | ||
``` | ||
3. Install local dependencies: | ||
``` | ||
npm install | ||
``` | ||
4. Build the code: | ||
``` | ||
gulp build | ||
``` |
@@ -143,3 +143,3 @@ 'use strict'; | ||
setOriginEmitter(originEmitter) { | ||
var events = this.originEmitter_? | ||
var events = this.originEmitter_ ? | ||
Object.keys(this.proxiedEvents_) : | ||
@@ -146,0 +146,0 @@ this.pendingEvents_; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
2
1
45914
13
1244
4
- Removedbootstrap@^3.3.6
- Removedbootstrap@3.4.1(transitive)
Updatedmetal@^1.0.0