@lijuhong1981/jsevents 1.0.0 → 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/package.json +1 -1
- package/src/Event.js +9 -7
- package/src/EventDispatcher.js +5 -3
package/package.json
CHANGED
package/src/Event.js
CHANGED
|
@@ -20,7 +20,7 @@ class Event extends Destroyable {
|
|
|
20
20
|
* @param {Function} callback 事件监听回调函数
|
|
21
21
|
* @returns {Boolean}
|
|
22
22
|
*/
|
|
23
|
-
|
|
23
|
+
hasListener(callback) {
|
|
24
24
|
for (let i = 0, length = this._listeners.length; i < length; i++) {
|
|
25
25
|
if (this._listeners[i].callback === callback) {
|
|
26
26
|
return true;
|
|
@@ -32,10 +32,12 @@ class Event extends Destroyable {
|
|
|
32
32
|
/**
|
|
33
33
|
* 添加事件监听
|
|
34
34
|
* @param {Function} callback 回调函数
|
|
35
|
-
* @param {Object} options
|
|
35
|
+
* @param {Object} options 事件配置项,可不填
|
|
36
|
+
* @param {Object} options.scope 回调函数<code>this</code>指针对象,可不填
|
|
37
|
+
* @param {Boolean} options.once 是否单次事件,可不填
|
|
36
38
|
* @returns {Function} 移除函数,调用该函数可直接移除事件监听
|
|
37
39
|
*/
|
|
38
|
-
|
|
40
|
+
addListener(callback, options = {}) {
|
|
39
41
|
Check.typeOf.func('callback', callback);
|
|
40
42
|
|
|
41
43
|
let listener;
|
|
@@ -51,7 +53,7 @@ class Event extends Destroyable {
|
|
|
51
53
|
callback: callback,
|
|
52
54
|
options: options,
|
|
53
55
|
removeFunc: () => {
|
|
54
|
-
this.
|
|
56
|
+
this.removeListener(callback);
|
|
55
57
|
}
|
|
56
58
|
};
|
|
57
59
|
this._listeners.push(listener);
|
|
@@ -65,7 +67,7 @@ class Event extends Destroyable {
|
|
|
65
67
|
* @param {Function} callback 回调函数
|
|
66
68
|
* @returns {this}
|
|
67
69
|
*/
|
|
68
|
-
|
|
70
|
+
removeListener(callback) {
|
|
69
71
|
Check.typeOf.func('callback', callback);
|
|
70
72
|
|
|
71
73
|
for (let i = 0, length = this._listeners.length; i < length; i++) {
|
|
@@ -89,13 +91,13 @@ class Event extends Destroyable {
|
|
|
89
91
|
|
|
90
92
|
/**
|
|
91
93
|
* 发送事件
|
|
92
|
-
* @param {...any} ...args 事件参数
|
|
94
|
+
* @param {...any} ...args 事件参数
|
|
93
95
|
* @returns {this}
|
|
94
96
|
*/
|
|
95
97
|
raiseEvent(...args) {
|
|
96
98
|
const listeners = this._listeners.slice();
|
|
97
99
|
listeners.forEach(listener => {
|
|
98
|
-
listener.callback(...args);
|
|
100
|
+
listener.callback.call(listener.options.scope, ...args);
|
|
99
101
|
if (listener.options.once) {
|
|
100
102
|
const index = this._listeners.indexOf(listener);
|
|
101
103
|
this._listeners.splice(index, 1);
|
package/src/EventDispatcher.js
CHANGED
|
@@ -55,7 +55,9 @@ class EventDispatcher extends Destroyable {
|
|
|
55
55
|
* 添加事件监听
|
|
56
56
|
* @param {String|Object} type 事件类型
|
|
57
57
|
* @param {Function} callback 回调函数
|
|
58
|
-
* @param {Object} options
|
|
58
|
+
* @param {Object} options 事件配置项,可不填
|
|
59
|
+
* @param {Object} options.scope 回调函数<code>this</code>指针对象,可不填
|
|
60
|
+
* @param {Boolean} options.once 是否单次事件,可不填
|
|
59
61
|
* @returns {this}
|
|
60
62
|
*/
|
|
61
63
|
addEventListener(type, callback, options) {
|
|
@@ -125,7 +127,7 @@ class EventDispatcher extends Destroyable {
|
|
|
125
127
|
if (listeners && listeners.length > 0) {
|
|
126
128
|
const _listeners = listeners.slice();
|
|
127
129
|
_listeners.forEach(listener => {
|
|
128
|
-
listener.callback(type, ...args);
|
|
130
|
+
listener.callback.call(listener.options.scope, type, ...args);
|
|
129
131
|
if (listener.options.once) {
|
|
130
132
|
const index = listeners.indexOf(listener);
|
|
131
133
|
listeners.splice(index, 1);
|
|
@@ -152,7 +154,7 @@ class EventDispatcher extends Destroyable {
|
|
|
152
154
|
if (listeners && listeners.length > 0) {
|
|
153
155
|
const _listeners = listeners.slice();
|
|
154
156
|
_listeners.forEach(listener => {
|
|
155
|
-
listener.callback(event);
|
|
157
|
+
listener.callback.call(listener.options.scope, event);
|
|
156
158
|
if (listener.options.once) {
|
|
157
159
|
const index = listeners.indexOf(listener);
|
|
158
160
|
listeners.splice(index, 1);
|