@lijuhong1981/jsevents 1.1.4 → 1.1.6
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/package.json +2 -2
- package/src/EventDispatcher.js +75 -79
- package/src/EventEmitter.js +30 -17
- package/src/EventSubscriber.js +43 -24
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lijuhong1981/jsevents",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.6",
|
|
4
4
|
"description": "> TODO: description",
|
|
5
5
|
"author": "lijuhong1981",
|
|
6
6
|
"homepage": "https://github.com/lijuhong1981/jslibs#readme",
|
|
@@ -22,5 +22,5 @@
|
|
|
22
22
|
"@lijuhong1981/jscheck": "^1.0.9",
|
|
23
23
|
"@lijuhong1981/jsdestroy": "^1.1.9"
|
|
24
24
|
},
|
|
25
|
-
"gitHead": "
|
|
25
|
+
"gitHead": "d20df0ba5bb330d43c7828f8a97b41ca3860a570"
|
|
26
26
|
}
|
package/src/EventDispatcher.js
CHANGED
|
@@ -1,46 +1,41 @@
|
|
|
1
1
|
import Check from "@lijuhong1981/jscheck/src/Check.js";
|
|
2
2
|
import isArray from "@lijuhong1981/jscheck/src/isArray.js";
|
|
3
|
-
import isFunction from "@lijuhong1981/jscheck/src/isFunction.js";
|
|
4
3
|
import isString from "@lijuhong1981/jscheck/src/isString.js";
|
|
5
4
|
import Destroyable from "@lijuhong1981/jsdestroy/src/Destroyable.js";
|
|
5
|
+
import Event from "./EventSubscriber.js";
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
let listeners = listenersMap.get(type);
|
|
21
|
-
if (!listeners) {
|
|
22
|
-
listeners = [];
|
|
23
|
-
listenersMap.set(type, listeners);
|
|
24
|
-
}
|
|
25
|
-
if (!hasListener(listeners, callback)) {
|
|
26
|
-
listeners.push({
|
|
27
|
-
type: type,
|
|
28
|
-
callback: callback,
|
|
29
|
-
options: options,
|
|
30
|
-
});
|
|
7
|
+
/**
|
|
8
|
+
* Description
|
|
9
|
+
* @param {Map<any, Event>} events
|
|
10
|
+
* @param {any} type
|
|
11
|
+
* @param {Function} callback
|
|
12
|
+
* @param {object} options
|
|
13
|
+
* @returns {void}
|
|
14
|
+
*/
|
|
15
|
+
function addListener(events, type, callback, options) {
|
|
16
|
+
let event = events.get(type);
|
|
17
|
+
if (!event) {
|
|
18
|
+
event = new Event();
|
|
19
|
+
events.set(type, event);
|
|
31
20
|
}
|
|
21
|
+
event.addEventListener(callback, options);
|
|
32
22
|
}
|
|
33
23
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Description
|
|
26
|
+
* @param {Map<any, Event>} events
|
|
27
|
+
* @param {any} type
|
|
28
|
+
* @param {Function} callback
|
|
29
|
+
* @returns {void}
|
|
30
|
+
*/
|
|
31
|
+
function removeListener(events, type, callback) {
|
|
32
|
+
const event = events.get(type);
|
|
33
|
+
if (event) {
|
|
34
|
+
if (callback) {
|
|
35
|
+
event.removeEventListener(callback);
|
|
41
36
|
} else {
|
|
42
|
-
|
|
43
|
-
|
|
37
|
+
event.destroy();
|
|
38
|
+
events.delete(type);
|
|
44
39
|
}
|
|
45
40
|
}
|
|
46
41
|
}
|
|
@@ -51,16 +46,19 @@ function removeListener(listenersMap, type, callback) {
|
|
|
51
46
|
class EventDispatcher extends Destroyable {
|
|
52
47
|
constructor() {
|
|
53
48
|
super();
|
|
54
|
-
|
|
49
|
+
/**
|
|
50
|
+
* @type {Map<any, Event>}
|
|
51
|
+
*/
|
|
52
|
+
this._events = new Map();
|
|
55
53
|
}
|
|
56
54
|
|
|
57
55
|
/**
|
|
58
56
|
* 添加事件监听
|
|
59
|
-
* @param {
|
|
57
|
+
* @param {any} type 事件类型
|
|
60
58
|
* @param {Function} callback 回调函数
|
|
61
|
-
* @param {
|
|
62
|
-
* @param {
|
|
63
|
-
* @param {
|
|
59
|
+
* @param {object} options 事件配置项,可不填
|
|
60
|
+
* @param {object} options.scope 回调函数<code>this</code>指针对象,可不填
|
|
61
|
+
* @param {boolean} options.once 是否单次事件,可不填
|
|
64
62
|
* @returns {this}
|
|
65
63
|
*/
|
|
66
64
|
addEventListener(type, callback, options) {
|
|
@@ -73,17 +71,17 @@ class EventDispatcher extends Destroyable {
|
|
|
73
71
|
|
|
74
72
|
if (isArray(type)) {
|
|
75
73
|
type.forEach(element => {
|
|
76
|
-
addListener(this.
|
|
74
|
+
addListener(this._events, element, callback, options);
|
|
77
75
|
});
|
|
78
76
|
} else
|
|
79
|
-
addListener(this.
|
|
77
|
+
addListener(this._events, type, callback, options);
|
|
80
78
|
|
|
81
79
|
return this;
|
|
82
80
|
}
|
|
83
81
|
|
|
84
82
|
/**
|
|
85
83
|
* 移除事件监听
|
|
86
|
-
* @param {
|
|
84
|
+
* @param {any} type 事件类型
|
|
87
85
|
* @param {Function} callback 回调函数,不填时可移除type下所有的事件监听
|
|
88
86
|
* @returns {this}
|
|
89
87
|
*/
|
|
@@ -96,38 +94,46 @@ class EventDispatcher extends Destroyable {
|
|
|
96
94
|
|
|
97
95
|
if (isArray(type)) {
|
|
98
96
|
type.forEach(element => {
|
|
99
|
-
removeListener(this.
|
|
97
|
+
removeListener(this._events, element, callback);
|
|
100
98
|
});
|
|
101
99
|
} else
|
|
102
|
-
removeListener(this.
|
|
100
|
+
removeListener(this._events, type, callback);
|
|
103
101
|
|
|
104
102
|
return this;
|
|
105
103
|
}
|
|
106
104
|
|
|
105
|
+
/**
|
|
106
|
+
* 注册的事件数量
|
|
107
|
+
* @returns {number}
|
|
108
|
+
*/
|
|
109
|
+
get numberOfEvents() {
|
|
110
|
+
return this._events.size;
|
|
111
|
+
}
|
|
112
|
+
|
|
107
113
|
/**
|
|
108
114
|
* 获取某类型事件监听器数量
|
|
109
|
-
* @param {
|
|
110
|
-
* @returns {
|
|
115
|
+
* @param {any} type 事件类型
|
|
116
|
+
* @returns {number} 监听器数量
|
|
111
117
|
*/
|
|
112
|
-
|
|
118
|
+
numberOfListeners(type) {
|
|
113
119
|
Check.valid('type', type);
|
|
114
120
|
|
|
115
|
-
const
|
|
121
|
+
const event = this._events.get(type);
|
|
116
122
|
|
|
117
|
-
return
|
|
123
|
+
return event ? event.numberOfListeners : 0;
|
|
118
124
|
}
|
|
119
125
|
|
|
120
126
|
/**
|
|
121
127
|
* 判断是否已添加某类型的事件监听器
|
|
122
|
-
* @param {
|
|
123
|
-
* @returns {
|
|
128
|
+
* @param {string|object} type 事件类型
|
|
129
|
+
* @returns {boolean} 判断结果
|
|
124
130
|
*/
|
|
125
131
|
hasEventListener(type) {
|
|
126
132
|
Check.valid('type', type);
|
|
127
133
|
|
|
128
|
-
const
|
|
134
|
+
const event = this._events.get(type);
|
|
129
135
|
|
|
130
|
-
return
|
|
136
|
+
return event && event.numberOfListeners > 0;
|
|
131
137
|
}
|
|
132
138
|
|
|
133
139
|
/**
|
|
@@ -135,33 +141,28 @@ class EventDispatcher extends Destroyable {
|
|
|
135
141
|
* @returns {this}
|
|
136
142
|
*/
|
|
137
143
|
clear() {
|
|
138
|
-
this.
|
|
139
|
-
|
|
144
|
+
this._events.forEach(function (event, type) {
|
|
145
|
+
event.destroy();
|
|
140
146
|
});
|
|
141
|
-
this.
|
|
147
|
+
this._events.clear();
|
|
142
148
|
|
|
143
149
|
return this;
|
|
144
150
|
}
|
|
145
151
|
|
|
146
152
|
/**
|
|
147
153
|
* 发送事件参数
|
|
148
|
-
* @param {
|
|
154
|
+
* @param {string|object} type 事件类型
|
|
149
155
|
* @param {...any} ...args 事件参数
|
|
150
156
|
* @returns {this}
|
|
151
157
|
*/
|
|
152
158
|
dispatch(type, ...args) {
|
|
153
159
|
Check.valid('type', type);
|
|
154
160
|
|
|
155
|
-
const
|
|
156
|
-
if (
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
if (listener.options.once) {
|
|
161
|
-
const index = listeners.indexOf(listener);
|
|
162
|
-
listeners.splice(index, 1);
|
|
163
|
-
}
|
|
164
|
-
});
|
|
161
|
+
const event = this._events.get(type);
|
|
162
|
+
if (event)
|
|
163
|
+
event.raiseEvent(type, ...args);
|
|
164
|
+
else {
|
|
165
|
+
console.warn('Not found the ' + type + ' type`s event.');
|
|
165
166
|
}
|
|
166
167
|
|
|
167
168
|
return this;
|
|
@@ -169,8 +170,8 @@ class EventDispatcher extends Destroyable {
|
|
|
169
170
|
|
|
170
171
|
/**
|
|
171
172
|
* 发送事件对象
|
|
172
|
-
* @param {
|
|
173
|
-
* @param {
|
|
173
|
+
* @param {object} event 事件对象
|
|
174
|
+
* @param {object} owner 事件对象所有者,可不填
|
|
174
175
|
* @returns {this}
|
|
175
176
|
*/
|
|
176
177
|
dispatchEvent(event, owner) {
|
|
@@ -179,16 +180,11 @@ class EventDispatcher extends Destroyable {
|
|
|
179
180
|
|
|
180
181
|
event.owner = event.owner || owner || this;
|
|
181
182
|
|
|
182
|
-
const
|
|
183
|
-
if (
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
if (listener.options.once) {
|
|
188
|
-
const index = listeners.indexOf(listener);
|
|
189
|
-
listeners.splice(index, 1);
|
|
190
|
-
}
|
|
191
|
-
});
|
|
183
|
+
const event = this._events.get(event.type);
|
|
184
|
+
if (event)
|
|
185
|
+
event.raiseEvent(event);
|
|
186
|
+
else {
|
|
187
|
+
console.warn('Not found the ' + type + ' type`s event.');
|
|
192
188
|
}
|
|
193
189
|
|
|
194
190
|
return this;
|
package/src/EventEmitter.js
CHANGED
|
@@ -9,31 +9,36 @@ class EventEmitter extends Destroyable {
|
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* 添加事件监听
|
|
12
|
-
* @param {
|
|
12
|
+
* @param {any} type 事件类型
|
|
13
13
|
* @param {Function} callback 回调函数
|
|
14
|
+
* @param {object} options 事件配置项,可不填
|
|
15
|
+
* @param {object} options.scope 回调函数<code>this</code>指针对象,可不填
|
|
16
|
+
* @param {boolean} options.once 是否单次事件,可不填
|
|
14
17
|
* @returns {this}
|
|
15
18
|
*/
|
|
16
|
-
on(type, callback) {
|
|
17
|
-
this._dispatcher.addEventListener(type, callback);
|
|
19
|
+
on(type, callback, options) {
|
|
20
|
+
this._dispatcher.addEventListener(type, callback, options);
|
|
18
21
|
return this;
|
|
19
22
|
}
|
|
20
23
|
|
|
21
24
|
/**
|
|
22
25
|
* 添加单次事件监听
|
|
23
|
-
* @param {
|
|
24
|
-
* @param {Function} callback
|
|
26
|
+
* @param {any} type 事件类型
|
|
27
|
+
* @param {Function} callback 回调函数
|
|
28
|
+
* @param {object} scope 回调函数<code>this</code>指针对象,可不填
|
|
25
29
|
* @returns {this}
|
|
26
30
|
*/
|
|
27
|
-
once(type, callback) {
|
|
31
|
+
once(type, callback, scope) {
|
|
28
32
|
this._dispatcher.addEventListener(type, callback, {
|
|
29
|
-
once: true
|
|
33
|
+
once: true,
|
|
34
|
+
scope: scope,
|
|
30
35
|
});
|
|
31
36
|
return this;
|
|
32
37
|
}
|
|
33
38
|
|
|
34
39
|
/**
|
|
35
40
|
* 移除事件监听
|
|
36
|
-
* @param {
|
|
41
|
+
* @param {any} type 事件类型
|
|
37
42
|
* @param {Function} callback 回调函数,不填时可移除type下所有的事件监听
|
|
38
43
|
* @returns {this}
|
|
39
44
|
*/
|
|
@@ -44,20 +49,28 @@ class EventEmitter extends Destroyable {
|
|
|
44
49
|
|
|
45
50
|
/**
|
|
46
51
|
* 判断是否已添加某类型的事件监听
|
|
47
|
-
* @param {
|
|
48
|
-
* @returns {
|
|
52
|
+
* @param {any} type 事件类型
|
|
53
|
+
* @returns {boolean} 判断结果
|
|
49
54
|
*/
|
|
50
55
|
hasListener(type) {
|
|
51
56
|
return this._dispatcher.hasEventListener(type);
|
|
52
57
|
}
|
|
53
58
|
|
|
59
|
+
/**
|
|
60
|
+
* 注册的事件数量
|
|
61
|
+
* @returns {number}
|
|
62
|
+
*/
|
|
63
|
+
get numberOfEvents() {
|
|
64
|
+
return this._dispatcher.numberOfEvents;
|
|
65
|
+
}
|
|
66
|
+
|
|
54
67
|
/**
|
|
55
68
|
* 获取某类型事件监听器数量
|
|
56
|
-
* @param {
|
|
57
|
-
* @returns {
|
|
69
|
+
* @param {any} type 事件类型
|
|
70
|
+
* @returns {number} 监听器数量
|
|
58
71
|
*/
|
|
59
|
-
|
|
60
|
-
return this._dispatcher.
|
|
72
|
+
numberOfListeners(type) {
|
|
73
|
+
return this._dispatcher.numberOfListeners(type);
|
|
61
74
|
}
|
|
62
75
|
|
|
63
76
|
/**
|
|
@@ -71,7 +84,7 @@ class EventEmitter extends Destroyable {
|
|
|
71
84
|
|
|
72
85
|
/**
|
|
73
86
|
* 发送事件参数
|
|
74
|
-
* @param {
|
|
87
|
+
* @param {any} type 事件类型
|
|
75
88
|
* @param {...any} ...args 事件参数
|
|
76
89
|
* @returns {this}
|
|
77
90
|
*/
|
|
@@ -82,8 +95,8 @@ class EventEmitter extends Destroyable {
|
|
|
82
95
|
|
|
83
96
|
/**
|
|
84
97
|
* 发送事件对象
|
|
85
|
-
* @param {
|
|
86
|
-
* @param {
|
|
98
|
+
* @param {object} event 事件对象
|
|
99
|
+
* @param {object} owner 事件对象所有者,可不填
|
|
87
100
|
* @returns {this}
|
|
88
101
|
*/
|
|
89
102
|
emitEvent(event, owner) {
|
package/src/EventSubscriber.js
CHANGED
|
@@ -7,7 +7,6 @@ import Destroyable from "@lijuhong1981/jsdestroy/src/Destroyable.js";
|
|
|
7
7
|
class EventSubscriber extends Destroyable {
|
|
8
8
|
constructor() {
|
|
9
9
|
super();
|
|
10
|
-
this._listeners = [];
|
|
11
10
|
this._listenersMap = new Map();
|
|
12
11
|
}
|
|
13
12
|
|
|
@@ -16,13 +15,13 @@ class EventSubscriber extends Destroyable {
|
|
|
16
15
|
* @returns {Number}
|
|
17
16
|
*/
|
|
18
17
|
get numberOfListeners() {
|
|
19
|
-
return this.
|
|
18
|
+
return this._listenersMap.size;
|
|
20
19
|
}
|
|
21
20
|
|
|
22
21
|
/**
|
|
23
22
|
* 判断事件监听器是否已存在
|
|
24
23
|
* @param {Function} callback 事件监听回调函数
|
|
25
|
-
* @returns {
|
|
24
|
+
* @returns {boolean}
|
|
26
25
|
*/
|
|
27
26
|
hasEventListener(callback) {
|
|
28
27
|
return this._listenersMap.has(callback);
|
|
@@ -31,9 +30,9 @@ class EventSubscriber extends Destroyable {
|
|
|
31
30
|
/**
|
|
32
31
|
* 添加事件监听
|
|
33
32
|
* @param {Function} callback 回调函数
|
|
34
|
-
* @param {
|
|
35
|
-
* @param {
|
|
36
|
-
* @param {
|
|
33
|
+
* @param {object} options 事件配置项,可不填
|
|
34
|
+
* @param {object} options.scope 回调函数<code>this</code>指针对象,可不填
|
|
35
|
+
* @param {boolean} options.once 是否单次事件,可不填
|
|
37
36
|
* @returns {Function} 移除函数,调用该函数可直接移除事件监听
|
|
38
37
|
*/
|
|
39
38
|
addEventListener(callback, options = {}) {
|
|
@@ -49,7 +48,6 @@ class EventSubscriber extends Destroyable {
|
|
|
49
48
|
this.removeEventListener(callback);
|
|
50
49
|
}
|
|
51
50
|
};
|
|
52
|
-
this._listeners.push(listener);
|
|
53
51
|
this._listenersMap.set(callback, listener);
|
|
54
52
|
} else {
|
|
55
53
|
listener.options = options;
|
|
@@ -61,20 +59,41 @@ class EventSubscriber extends Destroyable {
|
|
|
61
59
|
/**
|
|
62
60
|
* 移除事件监听
|
|
63
61
|
* @param {Function} callback 回调函数
|
|
64
|
-
* @returns {
|
|
62
|
+
* @returns {boolean}
|
|
65
63
|
*/
|
|
66
64
|
removeEventListener(callback) {
|
|
67
65
|
Check.typeOf.func('callback', callback);
|
|
68
66
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
this._listeners.splice(i, 1);
|
|
72
|
-
this._listenersMap.delete(callback);
|
|
73
|
-
break;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
67
|
+
return this._listenersMap.delete(callback);
|
|
68
|
+
}
|
|
76
69
|
|
|
77
|
-
|
|
70
|
+
/**
|
|
71
|
+
* @see addEventListener
|
|
72
|
+
*/
|
|
73
|
+
on(callback, options) {
|
|
74
|
+
return this.addEventListener(callback, options);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @see removeEventListener
|
|
79
|
+
*/
|
|
80
|
+
off(callback) {
|
|
81
|
+
return this.removeEventListener(callback);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 添加一次事件监听
|
|
86
|
+
* @param {Function} callback 回调函数
|
|
87
|
+
* @param {object} scope 回调函数<code>this</code>指针对象,可不填
|
|
88
|
+
* @returns {Function} 移除函数,调用该函数可直接移除事件监听
|
|
89
|
+
*
|
|
90
|
+
* @see addEventListener
|
|
91
|
+
*/
|
|
92
|
+
once(callback, scope) {
|
|
93
|
+
return this.addEventListener(callback, {
|
|
94
|
+
once: true,
|
|
95
|
+
scope: scope,
|
|
96
|
+
});
|
|
78
97
|
}
|
|
79
98
|
|
|
80
99
|
/**
|
|
@@ -82,7 +101,6 @@ class EventSubscriber extends Destroyable {
|
|
|
82
101
|
* @returns {this}
|
|
83
102
|
*/
|
|
84
103
|
clear() {
|
|
85
|
-
this._listeners.length = 0;
|
|
86
104
|
this._listenersMap.clear();
|
|
87
105
|
return this;
|
|
88
106
|
}
|
|
@@ -93,14 +111,15 @@ class EventSubscriber extends Destroyable {
|
|
|
93
111
|
* @returns {this}
|
|
94
112
|
*/
|
|
95
113
|
raiseEvent(...args) {
|
|
96
|
-
const listeners = this.
|
|
97
|
-
|
|
98
|
-
listener.
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
114
|
+
const listeners = this._listenersMap.values();
|
|
115
|
+
for (const listener of listeners) {
|
|
116
|
+
const options = listener.options;
|
|
117
|
+
const callback = listener.callback;
|
|
118
|
+
callback.apply(options.scope, arguments);
|
|
119
|
+
if (options.once) {
|
|
120
|
+
this._listenersMap.delete(callback);
|
|
102
121
|
}
|
|
103
|
-
}
|
|
122
|
+
}
|
|
104
123
|
|
|
105
124
|
return this;
|
|
106
125
|
}
|