srvmanger 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/EventManger.d.ts +84 -0
- package/EventManger.js +103 -0
- package/package.json +15 -0
- package/readme.md +30 -0
package/EventManger.d.ts
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
interface CallType {
|
2
|
+
callBack: Function,
|
3
|
+
caller: object
|
4
|
+
}
|
5
|
+
/**
|
6
|
+
* 管理事件函数的类
|
7
|
+
* @property List - 事件集
|
8
|
+
* @function add - 添加事件执行
|
9
|
+
* @function contains -判断是否存在该执行函数
|
10
|
+
* @function clear - 清除列表
|
11
|
+
* @function remove - 删除执行函数
|
12
|
+
* @function fire -派发调用
|
13
|
+
*/
|
14
|
+
declare class CallList {
|
15
|
+
protected List: CallType[];
|
16
|
+
/**
|
17
|
+
* 添加事件执行
|
18
|
+
* @param callBack - 回调函数
|
19
|
+
* @param caller - 默认参数
|
20
|
+
*/
|
21
|
+
public add(callBack: Function, caller: object): void;
|
22
|
+
/**
|
23
|
+
* 判断是否存在该执行函数
|
24
|
+
* @param callBack - 回调函数
|
25
|
+
* @param caller - 默认参数
|
26
|
+
* @returns true or false
|
27
|
+
*/
|
28
|
+
public contains(callBack: Function, caller: object): boolean;
|
29
|
+
/**
|
30
|
+
* 清除列表
|
31
|
+
*/
|
32
|
+
public clear(): void;
|
33
|
+
/**
|
34
|
+
* 删除执行函数
|
35
|
+
* @param callBack - 回调函数
|
36
|
+
* @param caller - 默认参数
|
37
|
+
* @returns true or false
|
38
|
+
*/
|
39
|
+
public remove(callBack: Function, caller: object): boolean;
|
40
|
+
/**
|
41
|
+
* 派发调用
|
42
|
+
* @param data - 数据集
|
43
|
+
*/
|
44
|
+
public fire(...data: any[]): void;
|
45
|
+
}
|
46
|
+
|
47
|
+
/**
|
48
|
+
* 事件派发类
|
49
|
+
* @property eventList - 事件集
|
50
|
+
* @function add - 添加派发事件
|
51
|
+
* @function remove - 移除派发事件
|
52
|
+
* @function fire - 派发事件
|
53
|
+
* @function clear - 清除事件
|
54
|
+
*/
|
55
|
+
export default class EventManger {
|
56
|
+
constructor();
|
57
|
+
protected eventList: Map<string, CallList>;
|
58
|
+
/**
|
59
|
+
* 添加派发事件
|
60
|
+
* @param key - 键值
|
61
|
+
* @param func - 回调函数
|
62
|
+
* @param caller - 默认参数
|
63
|
+
* @example e.add('test', (a: any, b: any) => { console.log(a, b) }, 'sss' or undefined);
|
64
|
+
*/
|
65
|
+
public add(key: string, func: Function, caller: any): void;
|
66
|
+
/**
|
67
|
+
* 移除派发事件
|
68
|
+
* @param key - 键值
|
69
|
+
* @param func - 回调函数
|
70
|
+
* @param caller - 默认参数
|
71
|
+
*/
|
72
|
+
public remove(key: string, func: Function, caller: any): void;
|
73
|
+
/**
|
74
|
+
* 派发事件
|
75
|
+
* @param key - 键值
|
76
|
+
* @param data - 数据
|
77
|
+
* @example e.fire('test', 1, 2) or e.fire('test', 1)
|
78
|
+
*/
|
79
|
+
public fire(key: string, ...data: any[]): void;
|
80
|
+
/**
|
81
|
+
* 清除事件
|
82
|
+
*/
|
83
|
+
public clear(): void;
|
84
|
+
}
|
package/EventManger.js
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
var CallList = (function () {
|
4
|
+
function CallList() {
|
5
|
+
this.List = [];
|
6
|
+
}
|
7
|
+
CallList.prototype.add = function (callBack, caller) {
|
8
|
+
this.List.push({ callBack: callBack, caller: caller });
|
9
|
+
};
|
10
|
+
CallList.prototype.contains = function (callBack, caller) {
|
11
|
+
return this.List.findIndex(function (itm) {
|
12
|
+
return itm.callBack === callBack && itm.caller === caller;
|
13
|
+
}) >= 0;
|
14
|
+
};
|
15
|
+
CallList.prototype.clear = function () {
|
16
|
+
this.List = [];
|
17
|
+
};
|
18
|
+
CallList.prototype.remove = function (callBack, caller) {
|
19
|
+
var index = this.List.findIndex(function (itm) {
|
20
|
+
return itm.callBack === callBack && itm.caller === caller;
|
21
|
+
});
|
22
|
+
if (index < 0) {
|
23
|
+
return false;
|
24
|
+
}
|
25
|
+
else {
|
26
|
+
this.List.splice(index, 1);
|
27
|
+
return true;
|
28
|
+
}
|
29
|
+
};
|
30
|
+
CallList.prototype.fire = function () {
|
31
|
+
var data = [];
|
32
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
33
|
+
data[_i] = arguments[_i];
|
34
|
+
}
|
35
|
+
for (var i = 0; i < this.List.length; i++) {
|
36
|
+
var val = this.List[i];
|
37
|
+
if (N(val) || N(val.callBack)) {
|
38
|
+
this.List.splice(i, 1);
|
39
|
+
}
|
40
|
+
else {
|
41
|
+
if (!N(val.caller)) {
|
42
|
+
var call = val.callBack.bind(null, val.caller);
|
43
|
+
call.apply(void 0, data);
|
44
|
+
}
|
45
|
+
else {
|
46
|
+
val.callBack.apply(val, data);
|
47
|
+
}
|
48
|
+
i++;
|
49
|
+
}
|
50
|
+
}
|
51
|
+
};
|
52
|
+
return CallList;
|
53
|
+
}());
|
54
|
+
function N(v) {
|
55
|
+
return -1 != [null, undefined].indexOf(v);
|
56
|
+
}
|
57
|
+
var EventManger = (function () {
|
58
|
+
function EventManger() {
|
59
|
+
this.eventList = new Map();
|
60
|
+
}
|
61
|
+
EventManger.prototype.add = function (key, func, caller) {
|
62
|
+
if (caller === void 0) { caller = null; }
|
63
|
+
if (this.eventList.has(key)) {
|
64
|
+
var notify = this.eventList.get(key);
|
65
|
+
if (!(notify === null || notify === void 0 ? void 0 : notify.contains(func, caller))) {
|
66
|
+
notify === null || notify === void 0 ? void 0 : notify.add(func, caller);
|
67
|
+
}
|
68
|
+
}
|
69
|
+
else {
|
70
|
+
var notify = new CallList();
|
71
|
+
notify.add(func, caller);
|
72
|
+
this.eventList.set(key, notify);
|
73
|
+
}
|
74
|
+
};
|
75
|
+
EventManger.prototype.remove = function (key, func, caller) {
|
76
|
+
if (caller === void 0) { caller = null; }
|
77
|
+
if (N(func)) {
|
78
|
+
this.eventList.delete(key);
|
79
|
+
return;
|
80
|
+
}
|
81
|
+
if (this.eventList.has(key)) {
|
82
|
+
var notify = this.eventList.get(key);
|
83
|
+
if (notify === null || notify === void 0 ? void 0 : notify.contains(func, caller)) {
|
84
|
+
notify.remove(func, caller);
|
85
|
+
}
|
86
|
+
}
|
87
|
+
};
|
88
|
+
EventManger.prototype.fire = function (key) {
|
89
|
+
var data = [];
|
90
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
91
|
+
data[_i - 1] = arguments[_i];
|
92
|
+
}
|
93
|
+
if (this.eventList.has(key)) {
|
94
|
+
var notify = this.eventList.get(key);
|
95
|
+
notify === null || notify === void 0 ? void 0 : notify.fire.apply(notify, data);
|
96
|
+
}
|
97
|
+
};
|
98
|
+
EventManger.prototype.clear = function () {
|
99
|
+
this.eventList.clear();
|
100
|
+
};
|
101
|
+
return EventManger;
|
102
|
+
}());
|
103
|
+
exports.default = EventManger;
|
package/package.json
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
{
|
2
|
+
"name": "srvmanger",
|
3
|
+
"version": "1.0.1",
|
4
|
+
"description": "",
|
5
|
+
"main": "EventManger.js",
|
6
|
+
"scripts": {
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
8
|
+
},
|
9
|
+
"keywords": [],
|
10
|
+
"author": "lionzyp",
|
11
|
+
"license": "ISC",
|
12
|
+
"types": [
|
13
|
+
"EventManger.d.ts"
|
14
|
+
]
|
15
|
+
}
|
package/readme.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# EventManger 事件订阅派发
|
2
|
+
## Author lionzyp
|
3
|
+
### 使用说明
|
4
|
+
|
5
|
+
#### 添加派发事件
|
6
|
+
#### add(key: string, func: Function, caller: any = null): void
|
7
|
+
#### @param key - 键值
|
8
|
+
#### @param func - 回调函数
|
9
|
+
#### @param caller - 默认参数
|
10
|
+
#### @example e.add('test', (a: any, b: any) => { console.log(a, b) }, 'sss' or undefined);
|
11
|
+
|
12
|
+
#### 移除派发事件
|
13
|
+
#### remove(key: string, func: Function, caller: any = null): void;
|
14
|
+
#### @param key - 键值
|
15
|
+
#### @param func - 回调函数
|
16
|
+
#### @param caller - 默认参数
|
17
|
+
|
18
|
+
#### 派发事件
|
19
|
+
#### fire(key: string, ...data: any[]): void;
|
20
|
+
#### @param key - 键值
|
21
|
+
#### @param data - 数据
|
22
|
+
#### @example e.fire('test', 1, 2) or e.fire('test', 1)
|
23
|
+
|
24
|
+
#### 清除事件
|
25
|
+
#### clear(): void;
|
26
|
+
|
27
|
+
#### Example
|
28
|
+
#### const EM = new EventManger();
|
29
|
+
#### EM.add('test', (a: any) => { console.log(a) });
|
30
|
+
#### EM.fire('test', 1);
|