eventdispatcher
Advanced tools
Comparing version 1.4.0 to 1.5.0
@@ -1,126 +0,3 @@ | ||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.EventDispatcher = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
'use strict'; | ||
function isEmpty(obj) { | ||
for (var prop in obj) { | ||
if (obj.hasOwnProperty(prop)){ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
var _instanceMap = {}; | ||
var EventDispatcher = function() { | ||
this._eventMap = {}; | ||
this._destroyed = false; | ||
//Method Map | ||
this.on = this.bind = this.addEventListener = this.addListener; | ||
this.off = this.unbind = this.removeEventListener = this.removeListener; | ||
this.once = this.one = this.addListenerOnce; | ||
this.emmit = this.trigger = this.dispatchEvent = this.dispatch; | ||
}; | ||
EventDispatcher.getInstance = function(key){ | ||
if(!key){ | ||
throw new Error('key must be'); | ||
} | ||
return _instanceMap[key] || (_instanceMap[key] = new EventDispatcher()); | ||
}; | ||
EventDispatcher.prototype.addListener = function(event, listener) { | ||
var listeners = this.getListener(event); | ||
if (!listeners) { | ||
this._eventMap[event] = [listener]; | ||
}else if (listeners.indexOf(listener) === -1) { | ||
listeners.push(listener); | ||
} | ||
return this; | ||
}; | ||
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) { | ||
if(typeof listener === 'undefined'){ | ||
return this.removeAllListener(event); | ||
} | ||
var listeners = this.getListener(event); | ||
if (listeners) { | ||
var i = listeners.indexOf(listener); | ||
if (i > -1) { | ||
listeners = listeners.splice(i, 1); | ||
if (!listeners.length) { | ||
delete(this._eventMap[event]); | ||
} | ||
} | ||
} | ||
return this; | ||
}; | ||
EventDispatcher.prototype.removeAllListener = function(event) { | ||
var listeners = this.getListener(event); | ||
if (listeners) { | ||
this._eventMap[event].length = 0; | ||
delete(this._eventMap[event]); | ||
} | ||
return this; | ||
}; | ||
EventDispatcher.prototype.hasListener = function(event) { | ||
return this.getListener(event) !== null; | ||
}; | ||
EventDispatcher.prototype.hasListeners = function() { | ||
return (this._eventMap !== null && this._eventMap !== undefined && !isEmpty(this._eventMap)); | ||
}; | ||
EventDispatcher.prototype.dispatch = function(eventType, eventObject) { | ||
var listeners = this.getListener(eventType); | ||
if (listeners) { | ||
eventObject = eventObject || {}; | ||
eventObject.type = eventType; | ||
eventObject.target = eventObject.target || this; | ||
var i = -1; | ||
while (++i < listeners.length) { | ||
listeners[i](eventObject); | ||
} | ||
// return true; | ||
} | ||
return this; | ||
}; | ||
EventDispatcher.prototype.getListener = function(event) { | ||
var result = this._eventMap ? this._eventMap[event] : null; | ||
return (result || null); | ||
}; | ||
EventDispatcher.prototype.destroy = function() { | ||
if (this._eventMap) { | ||
for (var i in this._eventMap) { | ||
this.removeAllListener(i); | ||
} | ||
this._eventMap = null; | ||
} | ||
this._destroyed = true; | ||
return this; | ||
}; | ||
module.exports = EventDispatcher; | ||
},{}]},{},[1])(1) | ||
}); | ||
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm5vZGVfbW9kdWxlcy9icm93c2VyLXBhY2svX3ByZWx1ZGUuanMiLCJz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KHJlc3VsdCB8fCBudWxsKTtcbiB9O1xuXG4gRXZlbnREaXNwYXRjaGVyLnByb3RvdHlwZS5kZXN0cm95ID0gZnVuY3Rpb24oKSB7XG4gICBpZiAodGhpcy5fZXZlbnRNYXApIHtcbiAgICAgZm9yICh2YXIgaSBpbiB0aGlzLl9ldmVudE1hcCkge1xuICAgICAgIHRoaXMucmVtb3ZlQWxsTGlzdGVuZXIoaSk7XG4gICAgIH1cbiAgICAgdGhpcy5fZXZlbnRNYXAgPSBudWxsO1xuICAgfVxuICAgdGhpcy5fZGVzdHJveWVkID0gdHJ1ZTtcbiAgIHJldHVybiB0aGlzO1xuIH07XG5cbiBtb2R1bGUuZXhwb3J0cyA9IEV2ZW50RGlzcGF0Y2hlcjtcbiJdfQ== | ||
'use strict';Object.defineProperty(exports,'__esModule',{value:true});function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError('Cannot call a class as a function')}}function isEmpty(obj){for(var prop in obj){if(obj.hasOwnProperty(prop)){return false}}return true}var _instanceMap={};var EventDispatcher=function(){function EventDispatcher(){_classCallCheck(this,EventDispatcher);this._eventMap={};this._destroyed=false;//Method Map | ||
this.on=this.bind=this.addEventListener=this.addListener;this.off=this.unbind=this.removeEventListener=this.removeListener;this.once=this.one=this.addListenerOnce;this.emmit=this.trigger=this.dispatchEvent=this.dispatch}EventDispatcher.getInstance=function getInstance(key){if(!key){throw new Error('key must be')}return _instanceMap[key]||(_instanceMap[key]=new EventDispatcher)};EventDispatcher.prototype.addListener=function addListener(event,listener){var listeners=this.getListener(event);if(!listeners){this._eventMap[event]=[listener]}else if(listeners.indexOf(listener)===-1){listeners.push(listener)}return this};EventDispatcher.prototype.addListenerOnce=function addListenerOnce(event,listener){var _this=this,_arguments=arguments;var s=this;var f2=function f2(){s.removeListener(event,f2);return listener.apply(_this,_arguments)};return this.addListener(event,f2)};EventDispatcher.prototype.removeListener=function removeListener(event,listener){if(typeof listener==='undefined'){return this.removeAllListener(event)}var listeners=this.getListener(event);if(listeners){var i=listeners.indexOf(listener);if(i>-1){listeners=listeners.splice(i,1);if(!listeners.length){delete this._eventMap[event]}}}return this};EventDispatcher.prototype.removeAllListener=function removeAllListener(event){var listeners=this.getListener(event);if(listeners){this._eventMap[event].length=0;delete this._eventMap[event]}return this};EventDispatcher.prototype.hasListener=function hasListener(event){return this.getListener(event)!==null};EventDispatcher.prototype.hasListeners=function hasListeners(){return this._eventMap!==null&&this._eventMap!==undefined&&!isEmpty(this._eventMap)};EventDispatcher.prototype.dispatch=function dispatch(eventType,eventObject){var listeners=this.getListener(eventType);if(listeners){eventObject=eventObject||{};eventObject.type=eventType;eventObject.target=eventObject.target||this;var i=-1;while(++i<listeners.length){listeners[i](eventObject)}// return true; | ||
}return this};EventDispatcher.prototype.getListener=function getListener(event){var result=this._eventMap?this._eventMap[event]:null;return result||null};EventDispatcher.prototype.destroy=function destroy(){if(this._eventMap){for(var i in this._eventMap){this.removeAllListener(i)}this._eventMap=null}this._destroyed=true;return this};return EventDispatcher}();exports.default=EventDispatcher;module.exports=exports['default']; |
{ | ||
"name": "eventdispatcher", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "EventDispatcher like nodes EventEmitter", | ||
"main": "src/index.js", | ||
"main": "dist/eventdispatcher.js", | ||
"jsnext:main": "src/eventdispatcher.js", | ||
"scripts": { | ||
"test": "node ./test", | ||
"build-debug": "browserify src/index.js -d --standalone EventDispatcher > dist/eventdispatcher.js", | ||
"build-min": "browserify src/index.js --standalone EventDispatcher | uglifyjs -c > dist/eventdispatcher.min.js", | ||
"dist": "npm run build-debug && npm run build-min" | ||
}, | ||
"test": "node test", | ||
"build-js": "browserify --fast -e src/eventdispatcher.js -d --standalone EventDispatcher -t babelify | derequire | exorcist dist/eventdispatcher.browser.js.map > dist/eventdispatcher.browser.js", | ||
"dist-js": "BABEL_ENV=production NODE_ENV=production browserify -e src/eventdispatcher.js --standalone EventDispatcher -t babelify | derequire > dist/eventdispatcher.browser.js", | ||
"dist-js-min": "uglifyjs -cmv dist/eventdispatcher.browser.js > dist/eventdispatcher.browser.min.js", | ||
"dist": "npm run dist-js && npm run dist-js-min && npm run build-lib", | ||
"start": "run-p watch-js server", | ||
"build-lib": "BABEL_ENV=production babel src -d dist", | ||
"watch-js": "watchify -d --poll=100 --fast --standalone EventDispatcher -e src/eventdispatcher.js -t babelify -o 'exorcist dist/eventdispatcher.browser.js.map > dist/eventdispatcher.browser.js' -v", | ||
"server": "browser-sync start --no-ghost-mode --server --directory --files 'examples/**/**, dist/*.js' --startPath examples/index.html" | ||
}, | ||
"repository": { | ||
@@ -29,6 +35,22 @@ "type": "git", | ||
"devDependencies": { | ||
"browserify": "13.0.0", | ||
"concurrently": "2.0.0", | ||
"uglify-js": "2.6.2" | ||
"babel-cli": "6.14.0", | ||
"babel-core": "6.14.0", | ||
"babel-plugin-add-module-exports": "0.2.1", | ||
"babel-plugin-transform-class-constructor-call": "6.8.0", | ||
"babel-plugin-transform-class-properties": "6.11.5", | ||
"babel-plugin-transform-decorators-legacy": "1.3.4", | ||
"babel-plugin-transform-es2015-classes": "6.14.0", | ||
"babel-plugin-transform-proto-to-assign": "6.9.0", | ||
"babel-plugin-transform-remove-console": "6.8.0", | ||
"babel-preset-es2015": "6.14.0", | ||
"babel-preset-stage-0": "6.5.0", | ||
"babelify": "7.3.0", | ||
"browser-sync": "2.14.3", | ||
"browserify": "13.1.0", | ||
"derequire": "2.0.3", | ||
"exorcist": "0.4.0", | ||
"npm-run-all": "3.0.0", | ||
"uglifyjs": "2.4.10", | ||
"watchify": "3.7.0" | ||
} | ||
} |
121
src/index.js
@@ -1,121 +0,2 @@ | ||
'use strict'; | ||
function isEmpty(obj) { | ||
for (var prop in obj) { | ||
if (obj.hasOwnProperty(prop)){ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
var _instanceMap = {}; | ||
var EventDispatcher = function() { | ||
this._eventMap = {}; | ||
this._destroyed = false; | ||
//Method Map | ||
this.on = this.bind = this.addEventListener = this.addListener; | ||
this.off = this.unbind = this.removeEventListener = this.removeListener; | ||
this.once = this.one = this.addListenerOnce; | ||
this.emmit = this.trigger = this.dispatchEvent = this.dispatch; | ||
}; | ||
EventDispatcher.getInstance = function(key){ | ||
if(!key){ | ||
throw new Error('key must be'); | ||
} | ||
return _instanceMap[key] || (_instanceMap[key] = new EventDispatcher()); | ||
}; | ||
EventDispatcher.prototype.addListener = function(event, listener) { | ||
var listeners = this.getListener(event); | ||
if (!listeners) { | ||
this._eventMap[event] = [listener]; | ||
}else if (listeners.indexOf(listener) === -1) { | ||
listeners.push(listener); | ||
} | ||
return this; | ||
}; | ||
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) { | ||
if(typeof listener === 'undefined'){ | ||
return this.removeAllListener(event); | ||
} | ||
var listeners = this.getListener(event); | ||
if (listeners) { | ||
var i = listeners.indexOf(listener); | ||
if (i > -1) { | ||
listeners = listeners.splice(i, 1); | ||
if (!listeners.length) { | ||
delete(this._eventMap[event]); | ||
} | ||
} | ||
} | ||
return this; | ||
}; | ||
EventDispatcher.prototype.removeAllListener = function(event) { | ||
var listeners = this.getListener(event); | ||
if (listeners) { | ||
this._eventMap[event].length = 0; | ||
delete(this._eventMap[event]); | ||
} | ||
return this; | ||
}; | ||
EventDispatcher.prototype.hasListener = function(event) { | ||
return this.getListener(event) !== null; | ||
}; | ||
EventDispatcher.prototype.hasListeners = function() { | ||
return (this._eventMap !== null && this._eventMap !== undefined && !isEmpty(this._eventMap)); | ||
}; | ||
EventDispatcher.prototype.dispatch = function(eventType, eventObject) { | ||
var listeners = this.getListener(eventType); | ||
if (listeners) { | ||
eventObject = eventObject || {}; | ||
eventObject.type = eventType; | ||
eventObject.target = eventObject.target || this; | ||
var i = -1; | ||
while (++i < listeners.length) { | ||
listeners[i](eventObject); | ||
} | ||
// return true; | ||
} | ||
return this; | ||
}; | ||
EventDispatcher.prototype.getListener = function(event) { | ||
var result = this._eventMap ? this._eventMap[event] : null; | ||
return (result || null); | ||
}; | ||
EventDispatcher.prototype.destroy = function() { | ||
if (this._eventMap) { | ||
for (var i in this._eventMap) { | ||
this.removeAllListener(i); | ||
} | ||
this._eventMap = null; | ||
} | ||
this._destroyed = true; | ||
return this; | ||
}; | ||
var EventDispatcher = require('./eventdispatcher'); | ||
module.exports = EventDispatcher; |
//TODO implement real test. | ||
// this is just for the fast fun! | ||
var EventDispatcher = require('../src/index'); | ||
var EventDispatcher = require('../dist/eventdispatcher'); | ||
var handler = function(event){ | ||
var handler = function(event) { | ||
console.log(event.type + ' was triggered'); | ||
@@ -25,4 +25,4 @@ }; | ||
console.log('trigger(evt1)' ,dispatcher.trigger('evt1')); | ||
console.log('trigger(evt2)' ,dispatcher.trigger('evt2')); | ||
console.log('trigger(evt1)', dispatcher.trigger('evt1')); | ||
console.log('trigger(evt2)', dispatcher.trigger('evt2')); | ||
@@ -33,9 +33,9 @@ console.log('dispatcher.off(evt2)', dispatcher.off('evt2')); | ||
console.log('trigger(evt1)' ,dispatcher.trigger('evt1')); | ||
console.log('trigger(evt2)' ,dispatcher.trigger('evt2')); | ||
console.log('trigger(evt1)', dispatcher.trigger('evt1')); | ||
console.log('trigger(evt2)', dispatcher.trigger('evt2')); | ||
console.log('tdestroy' ,dispatcher.destroy()); | ||
console.log('tdestroy', dispatcher.destroy()); | ||
console.log('trigger(evt1)' ,dispatcher.trigger('evt1')); | ||
console.log('trigger(evt2)' ,dispatcher.trigger('evt2')); | ||
console.log('trigger(evt1)', dispatcher.trigger('evt1')); | ||
console.log('trigger(evt2)', dispatcher.trigger('evt2')); | ||
@@ -47,2 +47,1 @@ | ||
console.log('dispatcher.hasListeners', dispatcher.hasListeners()); | ||
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
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
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
17
22179
19
183
2
1