event-helper
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -1,150 +0,160 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* 自定义事件 事件名 支持以数组的方式 对应多个事件 | ||
*/ | ||
var EventHelper = /** @class */ (function () { | ||
function EventHelper() { | ||
this.handlers = {}; | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
} | ||
EventHelper.create = function () { | ||
return new EventHelper(); | ||
}; | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports"], factory); | ||
} | ||
})(function (require, exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* 判断事件是否存在 | ||
* @param name | ||
* 自定义事件 事件名 支持以数组的方式 对应多个事件 | ||
*/ | ||
EventHelper.prototype.has = function (name) { | ||
return this.handlers[name] && this.handlers[name].length > 0; | ||
}; | ||
/** | ||
* 监听事件 | ||
* @param name | ||
* @param action | ||
*/ | ||
EventHelper.prototype.on = function (name, action) { | ||
var _this = this; | ||
//初始化对应事件名的队列 | ||
//支持多事件名绑定 | ||
var names = [].concat(name); | ||
names.forEach(function (name) { | ||
var actions = _this.handlers[name] = _this.handlers[name] || []; | ||
actions.push(action); | ||
//在事件被绑定的时候进行触发 事件绑定时事件 | ||
_this.emit('__event-on-' + name, action); | ||
}); | ||
return this; | ||
}; | ||
/** | ||
* 删除此事件的历史队列,重新绑定事件 | ||
* @param name | ||
* @param action | ||
*/ | ||
EventHelper.prototype.first = function (name, action) { | ||
var _this = this; | ||
//支持多事件名 | ||
var names = [].concat(name); | ||
names.forEach(function (name) { | ||
_this.off(name).on(name, action); | ||
}); | ||
return this; | ||
}; | ||
/** | ||
* 一次性绑定事件触发后自动删除 | ||
* @param name | ||
* @param action | ||
*/ | ||
EventHelper.prototype.once = function (name, action) { | ||
var _this = this; | ||
//支持多事件名 | ||
var names = [].concat(name); | ||
names.forEach(function (name) { | ||
var EventHelper = /** @class */ (function () { | ||
function EventHelper() { | ||
this.handlers = {}; | ||
} | ||
EventHelper.create = function () { | ||
return new EventHelper(); | ||
}; | ||
/** | ||
* 判断事件是否存在 | ||
* @param name | ||
*/ | ||
EventHelper.prototype.has = function (name) { | ||
return this.handlers[name] && this.handlers[name].length > 0; | ||
}; | ||
/** | ||
* 监听事件 | ||
* @param name | ||
* @param action | ||
*/ | ||
EventHelper.prototype.on = function (name, action) { | ||
var _this = this; | ||
//初始化对应事件名的队列 | ||
var actions = _this.handlers[name] = _this.handlers[name] || []; | ||
var _action = function () { | ||
var params = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
params[_i] = arguments[_i]; | ||
} | ||
action.apply(null, params); | ||
_this.off(name, _action); | ||
}; | ||
actions.push(_action); | ||
}); | ||
return this; | ||
}; | ||
/** | ||
* | ||
* @param name | ||
* @param params | ||
*/ | ||
EventHelper.prototype.emit = function (name) { | ||
var _this = this; | ||
var params = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
params[_i - 1] = arguments[_i]; | ||
} | ||
//初始化对应事件名的队列 | ||
//支持多事件名 | ||
var names = [].concat(name); | ||
var result; | ||
names.forEach(function (name) { | ||
var actions = _this.handlers[name]; | ||
if (_this._checkActions(actions)) { | ||
for (var _i = 0, actions_1 = actions; _i < actions_1.length; _i++) { | ||
var action = actions_1[_i]; | ||
result = action.apply(null, params); | ||
} | ||
//支持多事件名绑定 | ||
var names = [].concat(name); | ||
names.forEach(function (name) { | ||
var actions = _this.handlers[name] = _this.handlers[name] || []; | ||
actions.push(action); | ||
//在事件被绑定的时候进行触发 事件绑定时事件 | ||
_this.emit('__event-on-' + name, action); | ||
}); | ||
return this; | ||
}; | ||
/** | ||
* 删除此事件的历史队列,重新绑定事件 | ||
* @param name | ||
* @param action | ||
*/ | ||
EventHelper.prototype.first = function (name, action) { | ||
var _this = this; | ||
//支持多事件名 | ||
var names = [].concat(name); | ||
names.forEach(function (name) { | ||
_this.off(name).on(name, action); | ||
}); | ||
return this; | ||
}; | ||
/** | ||
* 一次性绑定事件触发后自动删除 | ||
* @param name | ||
* @param action | ||
*/ | ||
EventHelper.prototype.once = function (name, action) { | ||
var _this = this; | ||
//支持多事件名 | ||
var names = [].concat(name); | ||
names.forEach(function (name) { | ||
//初始化对应事件名的队列 | ||
var actions = _this.handlers[name] = _this.handlers[name] || []; | ||
var _action = function () { | ||
var params = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
params[_i] = arguments[_i]; | ||
} | ||
action.apply(null, params); | ||
_this.off(name, _action); | ||
}; | ||
actions.push(_action); | ||
}); | ||
return this; | ||
}; | ||
/** | ||
* | ||
* @param name | ||
* @param params | ||
*/ | ||
EventHelper.prototype.emit = function (name) { | ||
var _this = this; | ||
var params = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
params[_i - 1] = arguments[_i]; | ||
} | ||
else { | ||
//在事件被绑定的时候进行触发 | ||
_this.once('__event-on-' + name, function (_action) { | ||
_action.apply(null, params); | ||
}); | ||
} | ||
}); | ||
return result; | ||
}; | ||
/** | ||
* 挂载事件 不传action则删除对应事件名的整个队列 | ||
* @param name | ||
* @param action | ||
*/ | ||
EventHelper.prototype.off = function (name, action) { | ||
var _this = this; | ||
//支持多事件名 | ||
var names = [].concat(name); | ||
names.forEach(function (name) { | ||
var actions = _this.handlers[name]; | ||
if (_this._checkActions(actions)) { | ||
if (!action) { | ||
//删除对应事件名的全部事件 | ||
actions.splice(0, actions.length); | ||
//初始化对应事件名的队列 | ||
//支持多事件名 | ||
var names = [].concat(name); | ||
var result; | ||
names.forEach(function (name) { | ||
var actions = _this.handlers[name]; | ||
if (_this._checkActions(actions)) { | ||
for (var _i = 0, actions_1 = actions; _i < actions_1.length; _i++) { | ||
var action = actions_1[_i]; | ||
result = action.apply(null, params); | ||
} | ||
} | ||
else { | ||
//删除单个事件 | ||
var index = 0; | ||
for (var _i = 0, actions_2 = actions; _i < actions_2.length; _i++) { | ||
var _action = actions_2[_i]; | ||
if (action === _action) { | ||
break; | ||
//在事件被绑定的时候进行触发 | ||
_this.once('__event-on-' + name, function (_action) { | ||
_action.apply(null, params); | ||
}); | ||
} | ||
}); | ||
return result; | ||
}; | ||
/** | ||
* 挂载事件 不传action则删除对应事件名的整个队列 | ||
* @param name | ||
* @param action | ||
*/ | ||
EventHelper.prototype.off = function (name, action) { | ||
var _this = this; | ||
//支持多事件名 | ||
var names = [].concat(name); | ||
names.forEach(function (name) { | ||
var actions = _this.handlers[name]; | ||
if (_this._checkActions(actions)) { | ||
if (!action) { | ||
//删除对应事件名的全部事件 | ||
actions.splice(0, actions.length); | ||
} | ||
else { | ||
//删除单个事件 | ||
var index = 0; | ||
for (var _i = 0, actions_2 = actions; _i < actions_2.length; _i++) { | ||
var _action = actions_2[_i]; | ||
if (action === _action) { | ||
break; | ||
} | ||
index++; | ||
} | ||
index++; | ||
actions.splice(index, 1); | ||
} | ||
actions.splice(index, 1); | ||
} | ||
} | ||
}); | ||
return this; | ||
}; | ||
/** | ||
* 检测事件队列 | ||
* @param actions | ||
*/ | ||
EventHelper.prototype._checkActions = function (actions) { | ||
return actions && Array.isArray(actions); | ||
}; | ||
return EventHelper; | ||
}()); | ||
exports.default = EventHelper; | ||
}); | ||
return this; | ||
}; | ||
/** | ||
* 检测事件队列 | ||
* @param actions | ||
*/ | ||
EventHelper.prototype._checkActions = function (actions) { | ||
return actions && Array.isArray(actions); | ||
}; | ||
return EventHelper; | ||
}()); | ||
exports.default = EventHelper; | ||
}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "event-helper", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "a evnet handle write by typescript", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
8535
159
1