Comparing version 0.6.3-beta.0 to 1.0.0
@@ -1,11 +0,16 @@ | ||
declare type SupportElement = HTMLElement | SVGElement; | ||
export interface Listener { | ||
declare type SupportTarget = HTMLElement | SVGElement; | ||
interface Listener { | ||
(...payload: any): void; | ||
target?: SupportElement; | ||
target?: SupportTarget; | ||
} | ||
export interface ListenersMap { | ||
interface ListenersMap { | ||
[propName: string]: Listener[]; | ||
} | ||
export default class AnyEvent { | ||
callbackMap: ListenersMap; | ||
interface EmitBeforeHook { | ||
(options?: { | ||
target?: SupportTarget; | ||
}): boolean; | ||
} | ||
export default class { | ||
listenersMap: ListenersMap; | ||
target(el: HTMLElement): { | ||
@@ -20,3 +25,3 @@ on: (eventName: string, listener: Listener) => void; | ||
on(eventName: string, listener: Listener, { target }?: { | ||
target?: SupportElement; | ||
target?: SupportTarget; | ||
}): void; | ||
@@ -36,4 +41,8 @@ /** | ||
*/ | ||
emit(eventName: string, payload?: any): boolean; | ||
emit(eventName: string, payload?: any, beforeHook?: EmitBeforeHook): void; | ||
/** | ||
* 事件拦截器 | ||
* @param hook 钩子函数 | ||
*/ | ||
/** | ||
* 销毁实例 | ||
@@ -40,0 +49,0 @@ */ |
@@ -7,7 +7,7 @@ 'use strict'; | ||
var AnyEvent = (function () { | ||
function AnyEvent() { | ||
this.callbackMap = {}; | ||
var default_1 = (function () { | ||
function default_1() { | ||
this.listenersMap = {}; | ||
} | ||
AnyEvent.prototype.target = function (el) { | ||
default_1.prototype.target = function (el) { | ||
var _this = this; | ||
@@ -20,6 +20,6 @@ return { | ||
}; | ||
AnyEvent.prototype.on = function (eventName, listener, _a) { | ||
default_1.prototype.on = function (eventName, listener, _a) { | ||
var target = (_a === void 0 ? {} : _a).target; | ||
if (void 0 === this.callbackMap[eventName]) { | ||
this.callbackMap[eventName] = []; | ||
if (void 0 === this.listenersMap[eventName]) { | ||
this.listenersMap[eventName] = []; | ||
} | ||
@@ -29,9 +29,9 @@ if (void 0 !== target) { | ||
} | ||
this.callbackMap[eventName].push(listener); | ||
this.listenersMap[eventName].push(listener); | ||
}; | ||
AnyEvent.prototype.off = function (eventName, listener) { | ||
var listeners = this.callbackMap[eventName]; | ||
default_1.prototype.off = function (eventName, listener) { | ||
var listeners = this.listenersMap[eventName]; | ||
if (void 0 !== listeners) { | ||
if (void 0 === listener) { | ||
delete this.callbackMap[eventName]; | ||
delete this.listenersMap[eventName]; | ||
} | ||
@@ -44,22 +44,14 @@ else { | ||
}; | ||
AnyEvent.prototype.emit = function (eventName, payload) { | ||
default_1.prototype.emit = function (eventName, payload, beforeHook) { | ||
var e_1, _a; | ||
var listeners = this.callbackMap[eventName]; | ||
var targets = (payload || {}).targets; | ||
if (beforeHook === void 0) { beforeHook = function () { return true; }; } | ||
var listeners = this.listenersMap[eventName]; | ||
if (void 0 !== listeners && 0 < listeners.length) { | ||
var _loop_1 = function (listener) { | ||
var currentTarget = listener.target; | ||
if (void 0 !== currentTarget | ||
&& void 0 !== targets | ||
&& targets.every(function (target) { return currentTarget.contains(target); })) { | ||
listener(payload); | ||
} | ||
else if (void 0 === currentTarget) { | ||
listener(payload); | ||
} | ||
}; | ||
try { | ||
for (var listeners_1 = tslib.__values(listeners), listeners_1_1 = listeners_1.next(); !listeners_1_1.done; listeners_1_1 = listeners_1.next()) { | ||
var listener = listeners_1_1.value; | ||
_loop_1(listener); | ||
var target = listener.target; | ||
if (beforeHook({ target: target })) { | ||
listener(payload); | ||
} | ||
} | ||
@@ -74,14 +66,10 @@ } | ||
} | ||
return true; | ||
} | ||
else { | ||
return false; | ||
} | ||
}; | ||
AnyEvent.prototype.destroy = function () { | ||
this.callbackMap = {}; | ||
default_1.prototype.destroy = function () { | ||
this.listenersMap = {}; | ||
}; | ||
return AnyEvent; | ||
return default_1; | ||
}()); | ||
exports.default = AnyEvent; | ||
exports.default = default_1; |
import { __values } from 'tslib'; | ||
var AnyEvent = (function () { | ||
function AnyEvent() { | ||
this.callbackMap = {}; | ||
var default_1 = (function () { | ||
function default_1() { | ||
this.listenersMap = {}; | ||
} | ||
AnyEvent.prototype.target = function (el) { | ||
default_1.prototype.target = function (el) { | ||
var _this = this; | ||
@@ -15,6 +15,6 @@ return { | ||
}; | ||
AnyEvent.prototype.on = function (eventName, listener, _a) { | ||
default_1.prototype.on = function (eventName, listener, _a) { | ||
var target = (_a === void 0 ? {} : _a).target; | ||
if (void 0 === this.callbackMap[eventName]) { | ||
this.callbackMap[eventName] = []; | ||
if (void 0 === this.listenersMap[eventName]) { | ||
this.listenersMap[eventName] = []; | ||
} | ||
@@ -24,9 +24,9 @@ if (void 0 !== target) { | ||
} | ||
this.callbackMap[eventName].push(listener); | ||
this.listenersMap[eventName].push(listener); | ||
}; | ||
AnyEvent.prototype.off = function (eventName, listener) { | ||
var listeners = this.callbackMap[eventName]; | ||
default_1.prototype.off = function (eventName, listener) { | ||
var listeners = this.listenersMap[eventName]; | ||
if (void 0 !== listeners) { | ||
if (void 0 === listener) { | ||
delete this.callbackMap[eventName]; | ||
delete this.listenersMap[eventName]; | ||
} | ||
@@ -39,22 +39,14 @@ else { | ||
}; | ||
AnyEvent.prototype.emit = function (eventName, payload) { | ||
default_1.prototype.emit = function (eventName, payload, beforeHook) { | ||
var e_1, _a; | ||
var listeners = this.callbackMap[eventName]; | ||
var targets = (payload || {}).targets; | ||
if (beforeHook === void 0) { beforeHook = function () { return true; }; } | ||
var listeners = this.listenersMap[eventName]; | ||
if (void 0 !== listeners && 0 < listeners.length) { | ||
var _loop_1 = function (listener) { | ||
var currentTarget = listener.target; | ||
if (void 0 !== currentTarget | ||
&& void 0 !== targets | ||
&& targets.every(function (target) { return currentTarget.contains(target); })) { | ||
listener(payload); | ||
} | ||
else if (void 0 === currentTarget) { | ||
listener(payload); | ||
} | ||
}; | ||
try { | ||
for (var listeners_1 = __values(listeners), listeners_1_1 = listeners_1.next(); !listeners_1_1.done; listeners_1_1 = listeners_1.next()) { | ||
var listener = listeners_1_1.value; | ||
_loop_1(listener); | ||
var target = listener.target; | ||
if (beforeHook({ target: target })) { | ||
listener(payload); | ||
} | ||
} | ||
@@ -69,14 +61,10 @@ } | ||
} | ||
return true; | ||
} | ||
else { | ||
return false; | ||
} | ||
}; | ||
AnyEvent.prototype.destroy = function () { | ||
this.callbackMap = {}; | ||
default_1.prototype.destroy = function () { | ||
this.listenersMap = {}; | ||
}; | ||
return AnyEvent; | ||
return default_1; | ||
}()); | ||
export default AnyEvent; | ||
export default default_1; |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var tslib=require("tslib"),AnyEvent=function(){function t(){this.callbackMap={}}return t.prototype.target=function(t){var e=this;return{on:function(r,a){e.on(r,a,{target:t})}}},t.prototype.on=function(t,e,r){var a=(void 0===r?{}:r).target;void 0===this.callbackMap[t]&&(this.callbackMap[t]=[]),void 0!==a&&(e.target=a),this.callbackMap[t].push(e)},t.prototype.off=function(t,e){var r=this.callbackMap[t];if(void 0!==r)if(void 0===e)delete this.callbackMap[t];else{var a=r.findIndex((function(t){return t===e}));r.splice(a,1)}},t.prototype.emit=function(t,e){var r,a,n=this.callbackMap[t],o=(e||{}).targets;if(void 0!==n&&0<n.length){var i=function(t){var r=t.target;void 0!==r&&void 0!==o&&o.every((function(t){return r.contains(t)}))?t(e):void 0===r&&t(e)};try{for(var c=tslib.__values(n),l=c.next();!l.done;l=c.next()){i(l.value)}}catch(t){r={error:t}}finally{try{l&&!l.done&&(a=c.return)&&a.call(c)}finally{if(r)throw r.error}}return!0}return!1},t.prototype.destroy=function(){this.callbackMap={}},t}();exports.default=AnyEvent; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var tslib=require("tslib"),default_1=function(){function t(){this.listenersMap={}}return t.prototype.target=function(t){var e=this;return{on:function(r,i){e.on(r,i,{target:t})}}},t.prototype.on=function(t,e,r){var i=(void 0===r?{}:r).target;void 0===this.listenersMap[t]&&(this.listenersMap[t]=[]),void 0!==i&&(e.target=i),this.listenersMap[t].push(e)},t.prototype.off=function(t,e){var r=this.listenersMap[t];if(void 0!==r)if(void 0===e)delete this.listenersMap[t];else{var i=r.findIndex((function(t){return t===e}));r.splice(i,1)}},t.prototype.emit=function(t,e,r){var i,n;void 0===r&&(r=function(){return!0});var o=this.listenersMap[t];if(void 0!==o&&0<o.length)try{for(var s=tslib.__values(o),a=s.next();!a.done;a=s.next()){var l=a.value;r({target:l.target})&&l(e)}}catch(t){i={error:t}}finally{try{a&&!a.done&&(n=s.return)&&n.call(s)}finally{if(i)throw i.error}}},t.prototype.destroy=function(){this.listenersMap={}},t}();exports.default=default_1; |
{ | ||
"name": "any-event", | ||
"version": "0.6.3-beta.0", | ||
"version": "1.0.0", | ||
"description": "小巧的事件模型", | ||
@@ -22,3 +22,3 @@ "main": "dist/index.js", | ||
"sideEffects": false, | ||
"gitHead": "8d5f17faa86ccfb5091086bb38012659af614dec" | ||
"gitHead": "960b6a0c10c62c957c46616256532789c8080069" | ||
} |
@@ -0,0 +0,0 @@ # any-event [![NPM Version][npm-image]][npm-url] [![npm bundle size (minified + gzip)][size-image]][size-url] [![codecov][codecov-image]][codecov-url] [![CircleCI][ci-image]][ci-url] |
Sorry, the diff of this file is not supported yet
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
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
12303
184