@lijuhong1981/jsevents 1.1.14 → 1.1.16

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lijuhong1981/jsevents",
3
- "version": "1.1.14",
3
+ "version": "1.1.16",
4
4
  "description": "> TODO: description",
5
5
  "author": "lijuhong1981 <lijuhong@hotmail.com>",
6
6
  "homepage": "https://github.com/lijuhong1981/jslibs#readme",
@@ -20,7 +20,7 @@
20
20
  },
21
21
  "dependencies": {
22
22
  "@lijuhong1981/jscheck": "^1.0.14",
23
- "@lijuhong1981/jsdestroy": "^1.1.12"
23
+ "@lijuhong1981/jsdestroy": "^1.1.13"
24
24
  },
25
- "gitHead": "61a568d529682a0c657b5d130655c293d5dcab58"
25
+ "gitHead": "9291cfb2a4fb79f028630a3f52804a0b0e00aa0f"
26
26
  }
@@ -65,15 +65,14 @@ class EventDispatcher extends Destroyable {
65
65
  Check.valid('type', type);
66
66
  Check.typeOf.func('callback', callback);
67
67
 
68
- if (isString(type) && type.match(/\s+/)) {
68
+ if (isString(type) && type.match(/\s+/))
69
69
  type = type.split(/\s+/);
70
- }
71
70
 
72
- if (isArray(type)) {
71
+ if (isArray(type))
73
72
  type.forEach(element => {
74
73
  addListener(this._events, element, callback, options);
75
74
  });
76
- } else
75
+ else
77
76
  addListener(this._events, type, callback, options);
78
77
 
79
78
  return this;
@@ -88,20 +87,32 @@ class EventDispatcher extends Destroyable {
88
87
  removeEventListener(type, callback) {
89
88
  Check.valid('type', type);
90
89
 
91
- if (isString(type) && type.match(/\s+/)) {
90
+ if (isString(type) && type.match(/\s+/))
92
91
  type = type.split(/\s+/);
93
- }
94
92
 
95
- if (isArray(type)) {
93
+ if (isArray(type))
96
94
  type.forEach(element => {
97
95
  removeListener(this._events, element, callback);
98
96
  });
99
- } else
97
+ else
100
98
  removeListener(this._events, type, callback);
101
99
 
102
100
  return this;
103
101
  }
104
102
 
103
+ /**
104
+ * 移除所有事件监听
105
+ * @returns {this}
106
+ */
107
+ removeAllEventListeners() {
108
+ this._events.forEach(function (event) {
109
+ event.destroy();
110
+ });
111
+ this._events.clear();
112
+
113
+ return this;
114
+ }
115
+
105
116
  /**
106
117
  * 注册的事件数量
107
118
  * @returns {number}
@@ -137,16 +148,15 @@ class EventDispatcher extends Destroyable {
137
148
  }
138
149
 
139
150
  /**
140
- * 清除所有事件监听
141
- * @returns {this}
151
+ * 获取某类型的所有事件监听器
152
+ * @param {any} type
153
+ * @returns {Array<Function>}
142
154
  */
143
- clear() {
144
- this._events.forEach(function (event, type) {
145
- event.destroy();
146
- });
147
- this._events.clear();
155
+ getEventListeners(type) {
156
+ Check.valid('type', type);
148
157
 
149
- return this;
158
+ const event = this._events.get(type);
159
+ return event && event.listeners;
150
160
  }
151
161
 
152
162
  /**
@@ -160,7 +170,7 @@ class EventDispatcher extends Destroyable {
160
170
 
161
171
  const _event = this._events.get(type);
162
172
  if (_event)
163
- _event.raiseEvent(type, ...args);
173
+ _event.raiseEvent(...args);
164
174
  // else {
165
175
  // console.warn('Not found the type`s event.', type);
166
176
  // }
@@ -179,7 +189,8 @@ class EventDispatcher extends Destroyable {
179
189
  Check.typeOf.object('event', event);
180
190
  Check.valid('event.type', event.type);
181
191
 
182
- event.owner = event.owner || owner || this;
192
+ if (!event.owner)
193
+ event.owner = owner || this;
183
194
 
184
195
  const _event = this._events.get(event.type);
185
196
  if (_event)
@@ -195,7 +206,7 @@ class EventDispatcher extends Destroyable {
195
206
  * 执行销毁
196
207
  */
197
208
  onDestroy() {
198
- this.clear();
209
+ this.removeAllEventListeners();
199
210
  }
200
211
  };
201
212
 
@@ -16,20 +16,29 @@ class EventEmitter extends Destroyable {
16
16
  * @param {boolean} options.once 是否单次事件,可不填
17
17
  * @returns {this}
18
18
  */
19
- on(type, callback, options) {
19
+ addListener(type, callback, options) {
20
20
  this._dispatcher.addEventListener(type, callback, options);
21
21
  return this;
22
22
  }
23
23
 
24
+ /**
25
+ * @see addListener
26
+ */
27
+ on(type, callback, options) {
28
+ return this.addListener(type, callback, options);
29
+ }
30
+
24
31
  /**
25
32
  * 添加单次事件监听
26
33
  * @param {any} type 事件类型
27
34
  * @param {Function} callback 回调函数
28
35
  * @param {object} scope 回调函数<code>this</code>指针对象,可不填
29
36
  * @returns {this}
37
+ *
38
+ * @see addListener
30
39
  */
31
40
  once(type, callback, scope) {
32
- this._dispatcher.addEventListener(type, callback, {
41
+ this.addListener(type, callback, {
33
42
  once: true,
34
43
  scope: scope,
35
44
  });
@@ -42,11 +51,27 @@ class EventEmitter extends Destroyable {
42
51
  * @param {Function} callback 回调函数,不填时可移除type下所有的事件监听
43
52
  * @returns {this}
44
53
  */
45
- off(type, callback) {
54
+ removeListener(type, callback) {
46
55
  this._dispatcher.removeEventListener(type, callback);
47
56
  return this;
48
57
  }
49
58
 
59
+ /**
60
+ * @see removeListener
61
+ */
62
+ off(type, callback) {
63
+ return this.removeListener(type, callback);
64
+ }
65
+
66
+ /**
67
+ * 移除所有事件监听
68
+ * @returns {this}
69
+ */
70
+ removeAllListeners() {
71
+ this._dispatcher.removeAllEventListeners();
72
+ return this;
73
+ }
74
+
50
75
  /**
51
76
  * 判断是否已添加某类型的事件监听
52
77
  * @param {any} type 事件类型
@@ -56,6 +81,15 @@ class EventEmitter extends Destroyable {
56
81
  return this._dispatcher.hasEventListener(type);
57
82
  }
58
83
 
84
+ /**
85
+ * 获取某类型的所有事件监听器
86
+ * @param {any} type
87
+ * @returns {Array<Function>}
88
+ */
89
+ getListeners(type) {
90
+ return this._dispatcher.getEventListeners(type);
91
+ }
92
+
59
93
  /**
60
94
  * 注册的事件数量
61
95
  * @returns {number}
@@ -73,15 +107,6 @@ class EventEmitter extends Destroyable {
73
107
  return this._dispatcher.numberOfListeners(type);
74
108
  }
75
109
 
76
- /**
77
- * 清除所有事件监听
78
- * @returns {this}
79
- */
80
- clear() {
81
- this._dispatcher.clear();
82
- return this;
83
- }
84
-
85
110
  /**
86
111
  * 发送事件参数
87
112
  * @param {any} type 事件类型
@@ -100,7 +125,7 @@ class EventEmitter extends Destroyable {
100
125
  * @returns {this}
101
126
  */
102
127
  emitEvent(event, owner) {
103
- this._dispatcher.dispatchEvent(event, owner);
128
+ this._dispatcher.dispatchEvent(event, owner || this);
104
129
  return this;
105
130
  }
106
131
 
@@ -18,6 +18,19 @@ class EventSubscriber extends Destroyable {
18
18
  return this._listenersMap.size;
19
19
  }
20
20
 
21
+ /**
22
+ * 获取所有的事件监听器
23
+ * @returns {Array<Function>}
24
+ */
25
+ get listeners() {
26
+ const result = [];
27
+ const listeners = this._listenersMap.values();
28
+ for (const listener of listeners) {
29
+ result.push(listener.callback);
30
+ }
31
+ return result;
32
+ }
33
+
21
34
  /**
22
35
  * 判断事件监听器是否已存在
23
36
  * @param {Function} callback 事件监听回调函数