dom-eventer
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -36,3 +36,3 @@ export default class DOMEventer { | ||
if (hit === limit) { | ||
if (limit && hit === limit) { | ||
this.remove(id); | ||
@@ -43,3 +43,9 @@ } | ||
listen(target, eventType, cb, limit) { | ||
listen(target, eventType, cb, limit, capture = true) { | ||
const args = Array.from(arguments); | ||
if (args.length < 3) { | ||
throw new Error('`target`, `eventType` and `cb` params are required'); | ||
} | ||
if (!this.isValidListener(cb)) { | ||
@@ -49,2 +55,7 @@ throw new TypeError('listener must be a function'); | ||
if (typeof args[3] === 'boolean') { | ||
capture = args[3]; | ||
limit = null; | ||
} | ||
const id = `${eventType}.${this._recentId + 1}`; | ||
@@ -67,6 +78,6 @@ this._recentId += 1; | ||
if (target.addEventListener) { | ||
target.addEventListener(eventType, wrapperCb, false); | ||
target.addEventListener(eventType, wrapperCb, capture); | ||
this._subscriptions[id].remove = () => { | ||
target.removeEventListener(eventType, wrapperCb, false); | ||
target.removeEventListener(eventType, wrapperCb, capture); | ||
}; | ||
@@ -97,2 +108,2 @@ } else if (target.attachEvent) { | ||
} | ||
} | ||
} |
@@ -1,1 +0,1 @@ | ||
!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var n=e();for(var i in n)("object"==typeof exports?exports:t)[i]=n[i]}}(this,function(){return function(t){function e(i){if(n[i])return n[i].exports;var r=n[i]={i:i,l:!1,exports:{}};return t[i].call(r.exports,r,r.exports,e),r.l=!0,r.exports}var n={};return e.m=t,e.c=n,e.i=function(t){return t},e.d=function(t,n,i){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:i})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=0)}([function(t,e,n){"use strict";function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var r=function(){function t(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(e,n,i){return n&&t(e.prototype,n),i&&t(e,i),e}}(),o=function(){function t(e){i(this,t),this._name=e,this._subscriptions={},this._recentId=Date.now()}return r(t,[{key:"use",value:function(t){var e=this;return function(){return e[t].apply(e,arguments)}}},{key:"on",value:function(){for(var t=arguments.length,e=Array(t),n=0;n<t;n++)e[n]=arguments[n];this.use("listen").apply(this,e)}},{key:"off",value:function(){for(var t=arguments.length,e=Array(t),n=0;n<t;n++)e[n]=arguments[n];this.use("remove").apply(this,e)}},{key:"once",value:function(){for(var t=arguments.length,e=Array(t),n=0;n<t;n++)e[n]=arguments[n];this.use("listen").apply(this,[].concat(e,[1]))}},{key:"isValidListener",value:function(t){return"function"==typeof t}},{key:"updateHitCount",value:function(t){if(this._subscriptions[t]){var e=this._subscriptions[t],n=e.hit,i=e.limit;this._subscriptions[t].hit+=1,n===i&&this.remove(t)}}},{key:"listen",value:function(t,e,n,i){if(!this.isValidListener(n))throw new TypeError("listener must be a function");var r=e+"."+(this._recentId+1);this._recentId+=1;var o=function(t,e){return function(){e.updateHitCount(t),n.apply(void 0,arguments)}}(r,this);return this._subscriptions[r]={limit:i,hit:0,type:e},t.addEventListener?(t.addEventListener(e,o,!1),this._subscriptions[r].remove=function(){t.removeEventListener(e,o,!1)}):t.attachEvent&&(t.attachEvent("on"+e,o),this._subscriptions[r].remove=function(){t.detachEvent("on"+e,o)}),r}},{key:"remove",value:function(t){this._subscriptions[t]&&(this._subscriptions[t].remove(),this._subscriptions[t].isRemove=!0)}},{key:"removeAll",value:function(){var t=this;Object.keys(this._subscriptions).forEach(function(e){t.remove(e)})}}]),t}();e.default=o}])}); | ||
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var n=t();for(var r in n)("object"==typeof exports?exports:e)[r]=n[r]}}(this,function(){return function(e){function t(r){if(n[r])return n[r].exports;var i=n[r]={i:r,l:!1,exports:{}};return e[r].call(i.exports,i,i.exports,t),i.l=!0,i.exports}var n={};return t.m=e,t.c=n,t.i=function(e){return e},t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,n){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var i=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),o=function(){function e(t){r(this,e),this._name=t,this._subscriptions={},this._recentId=Date.now()}return i(e,[{key:"use",value:function(e){var t=this;return function(){return t[e].apply(t,arguments)}}},{key:"on",value:function(){for(var e=arguments.length,t=Array(e),n=0;n<e;n++)t[n]=arguments[n];this.use("listen").apply(this,t)}},{key:"off",value:function(){for(var e=arguments.length,t=Array(e),n=0;n<e;n++)t[n]=arguments[n];this.use("remove").apply(this,t)}},{key:"once",value:function(){for(var e=arguments.length,t=Array(e),n=0;n<e;n++)t[n]=arguments[n];this.use("listen").apply(this,[].concat(t,[1]))}},{key:"isValidListener",value:function(e){return"function"==typeof e}},{key:"updateHitCount",value:function(e){if(this._subscriptions[e]){var t=this._subscriptions[e],n=t.hit,r=t.limit;this._subscriptions[e].hit+=1,r&&n===r&&this.remove(e)}}},{key:"listen",value:function(e,t,n,r){var i=!(arguments.length>4&&void 0!==arguments[4])||arguments[4],o=Array.from(arguments);if(o.length<3)throw new Error("`target`, `eventType` and `cb` params are required");if(!this.isValidListener(n))throw new TypeError("listener must be a function");"boolean"==typeof o[3]&&(i=o[3],r=null);var s=t+"."+(this._recentId+1);this._recentId+=1;var u=function(e,t){return function(){t.updateHitCount(e),n.apply(void 0,arguments)}}(s,this);return this._subscriptions[s]={limit:r,hit:0,type:t},e.addEventListener?(e.addEventListener(t,u,i),this._subscriptions[s].remove=function(){e.removeEventListener(t,u,i)}):e.attachEvent&&(e.attachEvent("on"+t,u),this._subscriptions[s].remove=function(){e.detachEvent("on"+t,u)}),s}},{key:"remove",value:function(e){this._subscriptions[e]&&(this._subscriptions[e].remove(),this._subscriptions[e].isRemove=!0)}},{key:"removeAll",value:function(){var e=this;Object.keys(this._subscriptions).forEach(function(t){e.remove(t)})}}]),e}();t.default=o}])}); |
{ | ||
"name": "dom-eventer", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A utility to control the occurence of DOM eventer", | ||
@@ -16,3 +16,3 @@ "main": "lib/index.js", | ||
"author": "Youchao Liu <liuyouchao111@gmail.com>", | ||
"license": "ISC", | ||
"license": "MIT", | ||
"devDependencies": { | ||
@@ -19,0 +19,0 @@ "babel-core": "^6.24.1", |
# dom-eventer | ||
A utility to control the occurence of DOM eventer | ||
## Example | ||
```js | ||
this.eventer = new DOMEventer('brand'); | ||
this.eventer.listen(window, 'scroll', this.handleScroll); | ||
this.eventer.removeAll(); | ||
``` |
7851
104
11