eventdispatcher
Advanced tools
Comparing version 1.0.0 to 1.1.0
{ | ||
"name": "EventDispatcher", | ||
"main": "src/eventdispatcher.js", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"homepage": "https://github.com/soenkekluth/EventDispatcher", | ||
@@ -6,0 +6,0 @@ "authors": [ |
{ | ||
"name": "eventdispatcher", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "EventDispatcher like nodes EventEmitter", | ||
@@ -5,0 +5,0 @@ "main": "src/eventdispatcher.js", |
@@ -24,130 +24,112 @@ /** | ||
(function(exports) { | ||
'use strict'; | ||
'use strict'; | ||
function isEmpty(obj) { | ||
for (var prop in obj) { | ||
if (obj.hasOwnProperty(prop)){ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
var EventDispatcher = function() { | ||
this._eventMap = {}; | ||
this._destroyed = false; | ||
}; | ||
//IE8 | ||
if (!Array.prototype.indexOf) { | ||
Array.prototype.indexOf = function(obj, start) { | ||
for (var i = (start || 0), j = this.length; i < j; i++) { | ||
if (this[i] === obj) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
} | ||
EventDispatcher.prototype.addListener = function(event, listener) { | ||
var firstListener = false; | ||
var listener = this.getListener(event); | ||
if (listener === null) { | ||
firstListener = true; | ||
listener = this._eventMap[event] = []; | ||
} | ||
if (firstListener || listener.indexOf(listener) === -1) { | ||
listener.push(listener); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var EventDispatcher = function() { | ||
EventDispatcher.prototype.addListenerOnce = function(event, listener) { | ||
var s = this; | ||
var f2 = function() { | ||
s.removeListener(event, f2); | ||
return listener.apply(this, arguments); | ||
}; | ||
return this.addListener(event, f2); | ||
}; | ||
}; | ||
EventDispatcher.prototype.removeListener = function(event, listener) { | ||
EventDispatcher.prototype = { | ||
var listeners = this.getListener(event); | ||
if (listeners) { | ||
var i = listeners.indexOf(listener); | ||
if (i > -1) { | ||
this._eventMap[event] = listeners.splice(i, 1); | ||
if (listeners.length === 0) { | ||
delete(this._eventMap[event]); | ||
} | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
_eventMap: {}, | ||
_destroyed: false, | ||
EventDispatcher.prototype.removeAllListener = function(event) { | ||
var listeners = this.getListener(event); | ||
if (listeners) { | ||
this._eventMap[event].length = 0; | ||
delete(this._eventMap[event]); | ||
} | ||
return true; | ||
}; | ||
addListener: function(event, listener) { | ||
EventDispatcher.prototype.hasListener = function(event) { | ||
return this.getListener(event) !== null; | ||
}; | ||
this.getListener(event) || (this._eventMap[event] = []); | ||
EventDispatcher.prototype.hasListeners = function() { | ||
return this._eventMap && !isEmpty(this._eventMap); | ||
}; | ||
if (this.getListener(event).indexOf(listener) == -1) { | ||
this._eventMap[event].push(listener); | ||
} | ||
EventDispatcher.prototype.dispatch = function(eventType, eventObject) { | ||
var listeners = this.getListener(eventType); | ||
return this; | ||
}, | ||
if (listeners) { | ||
eventObject = (eventObject) ? eventObject : {}; | ||
eventObject.type = eventType; | ||
eventObject.target = eventObject.target || this; | ||
var i = -1; | ||
while (++i < listeners.length) { | ||
listeners[i].call(null, eventObject); | ||
} | ||
return true; | ||
} | ||
return false; | ||
}; | ||
EventDispatcher.prototype.getListener = function(event) { | ||
var result = this._eventMap ? this._eventMap[event] : null; | ||
return (result || null); | ||
}; | ||
addListenerOnce: function(event, listener) { | ||
var s = this; | ||
var f2 = function() { | ||
s.removeListener(event, f2); | ||
return listener.apply(this, arguments); | ||
}; | ||
return this.addListener(event, f2); | ||
}, | ||
EventDispatcher.prototype.destroy = function() { | ||
if (this._eventMap) { | ||
for (var i in this._eventMap) { | ||
this.removeAllListener(i); | ||
} | ||
this._eventMap = null; | ||
} | ||
this._destroyed = true; | ||
}; | ||
removeListener: function(event, listener) { | ||
var listeners = this.getListener(event); | ||
if (listeners) { | ||
var i = listeners.indexOf(listener); | ||
if (i > -1) { | ||
this._eventMap[event] = listeners.splice(i, 1); | ||
if (listeners.length === 0) { | ||
delete(this._eventMap[event]); | ||
} | ||
} | ||
} | ||
//Method Map | ||
EventDispatcher.prototype.on = EventDispatcher.prototype.bind = EventDispatcher.prototype.addEventListener = EventDispatcher.prototype.addListener; | ||
EventDispatcher.prototype.off = EventDispatcher.prototype.unbind = EventDispatcher.prototype.removeEventListener = EventDispatcher.prototype.removeListener; | ||
EventDispatcher.prototype.once = EventDispatcher.prototype.one = EventDispatcher.prototype.addListenerOnce; | ||
EventDispatcher.prototype.trigger = EventDispatcher.prototype.dispatchEvent = EventDispatcher.prototype.dispatch; | ||
return this; | ||
}, | ||
removeAllListener: function(event) { | ||
var listeners = this.getListener(event); | ||
if (listeners) { | ||
this._eventMap[event].length = 0; | ||
delete(this._eventMap[event]); | ||
} | ||
return this; | ||
}, | ||
dispatch: function(event, eventObject) { | ||
var listeners = this.getListener(event); | ||
if (listeners) { | ||
//var args = Array.prototype.slice.call(arguments, 1); | ||
eventObject = (eventObject) ? eventObject : {}; | ||
eventObject.type = event; | ||
eventObject.target = eventObject.target || this; | ||
var i = -1; | ||
while (++i < listeners.length) { | ||
//args ? listeners[i].apply(null, args) : listeners[i](); | ||
listeners[i].call(null, eventObject); | ||
} | ||
} else { | ||
// console.info('Nobody is listening to ' + event); | ||
} | ||
return this; | ||
}, | ||
getListener: function(event) { | ||
if (this._destroyed) { | ||
throw new Error('I am destroyed'); | ||
} | ||
return this._eventMap[event]; | ||
}, | ||
destroy: function() { | ||
if (this._eventMap) { | ||
for (var i in this._eventMap) { | ||
this.removeAllListener(i); | ||
} | ||
//TODO leave an empty object is better then throwing an error when using a fn after destroy? | ||
this._eventMap = null; | ||
} | ||
this._destroyed = true; | ||
} | ||
}; | ||
//Method Map | ||
EventDispatcher.prototype.on = EventDispatcher.prototype.bind = EventDispatcher.prototype.addEventListener = EventDispatcher.prototype.addListener; | ||
EventDispatcher.prototype.off = EventDispatcher.prototype.unbind = EventDispatcher.prototype.removeEventListener = EventDispatcher.prototype.removeListener; | ||
EventDispatcher.prototype.once = EventDispatcher.prototype.one = EventDispatcher.prototype.addListenerOnce; | ||
EventDispatcher.prototype.trigger = EventDispatcher.prototype.dispatchEvent = EventDispatcher.prototype.dispatch; | ||
(typeof module != "undefined" && module.exports) ? (module.exports = EventDispatcher) : (typeof define != "undefined" ? (define(function() { | ||
return EventDispatcher; | ||
})) : (exports.EventDispatcher = EventDispatcher)); | ||
})(this); | ||
module.exports = EventDispatcher; |
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
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
6535
5
140