@lijuhong1981/jsevents 1.1.1 → 1.1.3
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/index.js +2 -2
- package/package.json +3 -3
- package/src/EventDispatcher.js +3 -5
- package/src/EventEmitter.js +6 -8
- package/src/{EventRaiser.js → EventSubscriber.js} +19 -23
package/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import EventDispatcher from "./src/EventDispatcher.js";
|
|
2
2
|
import EventEmitter from "./src/EventEmitter.js";
|
|
3
|
-
import
|
|
3
|
+
import EventSubscriber from "./src/EventSubscriber.js";
|
|
4
4
|
|
|
5
5
|
export {
|
|
6
6
|
EventDispatcher,
|
|
7
7
|
EventEmitter,
|
|
8
|
-
|
|
8
|
+
EventSubscriber,
|
|
9
9
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lijuhong1981/jsevents",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"description": "> TODO: description",
|
|
5
5
|
"author": "lijuhong1981",
|
|
6
6
|
"homepage": "https://github.com/lijuhong1981/jslibs#readme",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"@lijuhong1981/jscheck": "^1.0.9",
|
|
23
|
-
"@lijuhong1981/jsdestroy": "^1.1.
|
|
23
|
+
"@lijuhong1981/jsdestroy": "^1.1.8"
|
|
24
24
|
},
|
|
25
|
-
"gitHead": "
|
|
25
|
+
"gitHead": "611e658c8c0e2b72dbea60cbcd50ec6dc2d0b497"
|
|
26
26
|
}
|
package/src/EventDispatcher.js
CHANGED
|
@@ -109,7 +109,7 @@ class EventDispatcher extends Destroyable {
|
|
|
109
109
|
* @param {String|Object} type 事件类型
|
|
110
110
|
* @returns {Number} 监听器数量
|
|
111
111
|
*/
|
|
112
|
-
|
|
112
|
+
getEventListenersCount(type) {
|
|
113
113
|
Check.valid('type', type);
|
|
114
114
|
|
|
115
115
|
const listeners = this._listenersMap.get(type);
|
|
@@ -195,12 +195,10 @@ class EventDispatcher extends Destroyable {
|
|
|
195
195
|
}
|
|
196
196
|
|
|
197
197
|
/**
|
|
198
|
-
*
|
|
199
|
-
* @returns {this}
|
|
198
|
+
* 执行销毁
|
|
200
199
|
*/
|
|
201
|
-
|
|
200
|
+
onDestroy() {
|
|
202
201
|
this.clear();
|
|
203
|
-
return super.destroy();
|
|
204
202
|
}
|
|
205
203
|
};
|
|
206
204
|
|
package/src/EventEmitter.js
CHANGED
|
@@ -56,8 +56,8 @@ class EventEmitter extends Destroyable {
|
|
|
56
56
|
* @param {String|Object} type 事件类型
|
|
57
57
|
* @returns {Number} 监听器数量
|
|
58
58
|
*/
|
|
59
|
-
|
|
60
|
-
return this._dispatcher.
|
|
59
|
+
listenersCount(type) {
|
|
60
|
+
return this._dispatcher.getEventListenersCount(type);
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
/**
|
|
@@ -86,18 +86,16 @@ class EventEmitter extends Destroyable {
|
|
|
86
86
|
* @param {Object} owner 事件对象所有者,可不填
|
|
87
87
|
* @returns {this}
|
|
88
88
|
*/
|
|
89
|
-
emitEvent(event) {
|
|
90
|
-
this._dispatcher.dispatchEvent(event);
|
|
89
|
+
emitEvent(event, owner) {
|
|
90
|
+
this._dispatcher.dispatchEvent(event, owner);
|
|
91
91
|
return this;
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
/**
|
|
95
|
-
*
|
|
96
|
-
* @returns {this}
|
|
95
|
+
* 执行销毁
|
|
97
96
|
*/
|
|
98
|
-
|
|
97
|
+
onDestroy() {
|
|
99
98
|
this._dispatcher.destroy();
|
|
100
|
-
return super.destroy();
|
|
101
99
|
}
|
|
102
100
|
};
|
|
103
101
|
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import Check from "@lijuhong1981/jscheck/src/Check.js";
|
|
2
2
|
import Destroyable from "@lijuhong1981/jsdestroy/src/Destroyable.js";
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
/**
|
|
5
|
+
* A generic utility class for managing subscribers for a particular event. This class is usually instantiated inside of a container class and exposed as a property for others to subscribe to.
|
|
6
|
+
*/
|
|
7
|
+
class EventSubscriber extends Destroyable {
|
|
5
8
|
constructor() {
|
|
6
9
|
super();
|
|
7
10
|
this._listeners = [];
|
|
11
|
+
this._listenersMap = new Map();
|
|
8
12
|
}
|
|
9
13
|
|
|
10
14
|
/**
|
|
@@ -20,13 +24,8 @@ class EventRaiser extends Destroyable {
|
|
|
20
24
|
* @param {Function} callback 事件监听回调函数
|
|
21
25
|
* @returns {Boolean}
|
|
22
26
|
*/
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if (this._listeners[i].callback === callback) {
|
|
26
|
-
return true;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return false;
|
|
27
|
+
hasEventListener(callback) {
|
|
28
|
+
return this._listenersMap.has(callback);
|
|
30
29
|
}
|
|
31
30
|
|
|
32
31
|
/**
|
|
@@ -37,26 +36,23 @@ class EventRaiser extends Destroyable {
|
|
|
37
36
|
* @param {Boolean} options.once 是否单次事件,可不填
|
|
38
37
|
* @returns {Function} 移除函数,调用该函数可直接移除事件监听
|
|
39
38
|
*/
|
|
40
|
-
|
|
39
|
+
addEventListener(callback, options = {}) {
|
|
41
40
|
Check.typeOf.func('callback', callback);
|
|
42
41
|
|
|
43
|
-
let listener;
|
|
44
|
-
for (let i = 0, length = this._listeners.length; i < length; i++) {
|
|
45
|
-
if (this._listeners[i].callback === callback) {
|
|
46
|
-
listener = this._listeners[i];
|
|
47
|
-
break;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
42
|
+
let listener = this._listenersMap.get(callback);
|
|
50
43
|
|
|
51
44
|
if (!listener) {
|
|
52
45
|
listener = {
|
|
53
46
|
callback: callback,
|
|
54
47
|
options: options,
|
|
55
48
|
removeFunc: () => {
|
|
56
|
-
this.
|
|
49
|
+
this.removeEventListener(callback);
|
|
57
50
|
}
|
|
58
51
|
};
|
|
59
52
|
this._listeners.push(listener);
|
|
53
|
+
this._listenersMap.set(callback, listener);
|
|
54
|
+
} else {
|
|
55
|
+
listener.options = options;
|
|
60
56
|
}
|
|
61
57
|
|
|
62
58
|
return listener.removeFunc;
|
|
@@ -67,12 +63,13 @@ class EventRaiser extends Destroyable {
|
|
|
67
63
|
* @param {Function} callback 回调函数
|
|
68
64
|
* @returns {this}
|
|
69
65
|
*/
|
|
70
|
-
|
|
66
|
+
removeEventListener(callback) {
|
|
71
67
|
Check.typeOf.func('callback', callback);
|
|
72
68
|
|
|
73
69
|
for (let i = 0, length = this._listeners.length; i < length; i++) {
|
|
74
70
|
if (this._listeners[i].callback === callback) {
|
|
75
71
|
this._listeners.splice(i, 1);
|
|
72
|
+
this._listenersMap.delete(callback);
|
|
76
73
|
break;
|
|
77
74
|
}
|
|
78
75
|
}
|
|
@@ -86,6 +83,7 @@ class EventRaiser extends Destroyable {
|
|
|
86
83
|
*/
|
|
87
84
|
clear() {
|
|
88
85
|
this._listeners.length = 0;
|
|
86
|
+
this._listenersMap.clear();
|
|
89
87
|
return this;
|
|
90
88
|
}
|
|
91
89
|
|
|
@@ -108,13 +106,11 @@ class EventRaiser extends Destroyable {
|
|
|
108
106
|
}
|
|
109
107
|
|
|
110
108
|
/**
|
|
111
|
-
*
|
|
112
|
-
* @returns {this}
|
|
109
|
+
* 执行销毁
|
|
113
110
|
*/
|
|
114
|
-
|
|
111
|
+
onDestroy() {
|
|
115
112
|
this.clear();
|
|
116
|
-
return super.destroy();
|
|
117
113
|
}
|
|
118
114
|
};
|
|
119
115
|
|
|
120
|
-
export default
|
|
116
|
+
export default EventSubscriber;
|