Skip to content

Commit e46456b

Browse files
committed
lint fixes and behvaiour consistency for eventhandle
1 parent fc10c4d commit e46456b

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

src/core/event-handle.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
* console.log(a + b);
66
* });
77
* obj.fire('test');
8-
*
8+
*
99
* evt.off(); // easy way to remove this event
1010
* obj.fire('test'); // this will not trigger an event
1111
* @example
1212
* // store an array of event handles
1313
* let events = [ ];
14-
*
14+
*
1515
* events.push(objA.on('testA', () => { }));
1616
* events.push(objB.on('testB', () => { }));
17-
*
17+
*
1818
* // when needed, remove all events
1919
* events.forEach((evt) => {
2020
* evt.off();
@@ -55,9 +55,9 @@ class EventHandle {
5555
/**
5656
* True if event has been removed.
5757
* @type {boolean}
58-
* @readonly
58+
* @private
5959
*/
60-
removed = false;
60+
_removed = false;
6161

6262
/**
6363
* @param {EventHandler} handler - source object of the event.
@@ -78,20 +78,26 @@ class EventHandle {
7878
* Remove references.
7979
*/
8080
destroy() {
81-
if (this.removed) return;
82-
this.removed = true;
83-
this.handler = null;
84-
this.callback = null;
85-
this.scope = null;
81+
if (this._removed) return;
82+
this._removed = true;
8683
}
8784

8885
/**
8986
* Remove this event from its handler.
9087
*/
9188
off() {
92-
if (this.removed) return;
89+
if (this._removed) return;
9390
this.handler.off(this.name, this.callback, this.scope);
9491
}
92+
93+
/**
94+
* True if event has been removed.
95+
* @type {boolean}
96+
* @readonly
97+
*/
98+
get removed() {
99+
return this._removed;
100+
}
95101
}
96102

97-
export { EventHandle };
103+
export { EventHandle };

src/core/event-handler.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class EventHandler {
3333
/**
3434
* When set to true, `on` and `once` methods will return `this`.
3535
* When set to false, `on` and `once` will return `EventHandle`.
36-
*
3736
* @type {boolean}
3837
*/
3938
static chaining = true;
@@ -82,7 +81,7 @@ class EventHandler {
8281
if (this._callbackActive[name] && this._callbackActive[name] === this._callbacks[name])
8382
this._callbackActive[name] = this._callbackActive[name].slice();
8483

85-
let evt = new EventHandle(this, name, callback, scope, once);
84+
const evt = new EventHandle(this, name, callback, scope, once);
8685
this._callbacks[name].push(evt);
8786
return evt;
8887
}
@@ -143,9 +142,9 @@ class EventHandler {
143142
}
144143

145144
if (!name) {
146-
for(let name in this._callbacks) {
145+
for (const name in this._callbacks) {
147146
const handles = this._callbacks[name];
148-
for(let i = 0; i < handles.length; i++) {
147+
for (let i = 0; i < handles.length; i++) {
149148
handles[i].destroy();
150149
}
151150
}
@@ -154,7 +153,7 @@ class EventHandler {
154153
} else if (!callback) {
155154
const handles = this._callbacks[name];
156155
if (handles) {
157-
for(let i = 0; i < handles.length; i++) {
156+
for (let i = 0; i < handles.length; i++) {
158157
handles[i].destroy();
159158
}
160159
this._callbacks[name] = [];

0 commit comments

Comments
 (0)