From 8e582b720e63f8fe51ded3871db237947aa6cd4d Mon Sep 17 00:00:00 2001 From: mrmaxm Date: Fri, 14 Jul 2023 17:42:53 +0300 Subject: [PATCH 01/16] EventHandle and option to disable EventHandler chaining --- src/core/event-handle.js | 96 +++++++++++++++++++++++++++++++++++++++ src/core/event-handler.js | 70 ++++++++++++++++++---------- 2 files changed, 143 insertions(+), 23 deletions(-) create mode 100644 src/core/event-handle.js diff --git a/src/core/event-handle.js b/src/core/event-handle.js new file mode 100644 index 00000000000..0668c296c23 --- /dev/null +++ b/src/core/event-handle.js @@ -0,0 +1,96 @@ +/** + * Event Handle that is created by {@link EventHandler} and can be used for easier removing and events management. + * @example + * const evt = obj.on('test', (a, b) => { + * console.log(a + b); + * }); + * obj.fire('test'); + * + * evt.off(); // easy way to remove this event + * obj.fire('test'); // this will not trigger an event + * @example + * // store an array of event handles + * let events = [ ]; + * + * events.push(objA.on('testA', () => { })); + * events.push(objB.on('testB', () => { })); + * + * // when needed, remove all events + * events.forEach((evt) => { + * evt.off(); + * }); + * events = [ ]; + */ +class EventHandle { + /** + * @type {EventHandler} + * @private + */ + handler; + + /** + * @type {string} + * @private + */ + name; + + /** + * @type {HandleEventCallback} + * @private + */ + callback; + + /** + * @type {object} + * @private + */ + scope; + + /** + * @type {boolean} + * @private + */ + once; + + /** + * True if event has been removed. + * @type {boolean} + */ + removed = false; + + /** + * @param {EventHandler} handler - source object of the event. + * @param {string} name - Name of the event. + * @param {HandleEventCallback} callback - Function that is called when event is fired. + * @param {object} scope - Object that is used as `this` when event is fired. + * @param {boolean} [once] - If this is a single event and will be removed after event is fired. + */ + constructor(handler, name, callback, scope, once = false) { + this.handler = handler; + this.name = name; + this.callback = callback; + this.scope = scope; + this.once = once; + } + + /** + * Remove references. + */ + destroy() { + if (this.removed) return; + this.removed = true; + this.handler = null; + this.callback = null; + this.scope = null; + } + + /** + * Remove this event from its handler. + */ + off() { + if (this.removed) return; + this.handler.off(this.name, this.callback, this.scope); + } +} + +export { EventHandle }; \ No newline at end of file diff --git a/src/core/event-handler.js b/src/core/event-handler.js index 9a1ded87709..b93848d73a7 100644 --- a/src/core/event-handler.js +++ b/src/core/event-handler.js @@ -1,3 +1,5 @@ +import { EventHandle } from './event-handle.js'; + /** * Callback used by {@link EventHandler} functions. Note the callback is limited to 8 arguments. * @@ -28,6 +30,14 @@ * ``` */ class EventHandler { + /** + * When set to true, `on` and `once` methods will return `this`. + * When set to false, `on` and `once` will return `EventHandle`. + * + * @type {boolean} + */ + static chaining = true; + /** * @type {object} * @private @@ -59,11 +69,12 @@ class EventHandler { * @param {object} [scope] - Object to use as 'this' when the event is fired, defaults to * current this. * @param {boolean} [once=false] - If true, the callback will be unbound after being fired once. + * @returns {EventHandle|null} Created {@link EventHandle} or `null` of provided arguments are invalid. * @private */ - _addCallback(name, callback, scope, once = false) { + _addCallback(name, callback, scope = this, once = false) { if (!name || typeof name !== 'string' || !callback) - return; + return null; if (!this._callbacks[name]) this._callbacks[name] = []; @@ -71,11 +82,9 @@ class EventHandler { if (this._callbackActive[name] && this._callbackActive[name] === this._callbacks[name]) this._callbackActive[name] = this._callbackActive[name].slice(); - this._callbacks[name].push({ - callback: callback, - scope: scope || this, - once: once - }); + let evt = new EventHandle(this, name, callback, scope, once); + this._callbacks[name].push(evt); + return evt; } /** @@ -86,7 +95,7 @@ class EventHandler { * the callback is limited to 8 arguments. * @param {object} [scope] - Object to use as 'this' when the event is fired, defaults to * current this. - * @returns {EventHandler} Self for chaining. + * @returns {EventHandle|EventHandler} {@link EventHandle} if `EventHandler.chaining` set to `false`, otherwise returns `this` (self) for chaining. * @example * obj.on('test', function (a, b) { * console.log(a + b); @@ -94,9 +103,8 @@ class EventHandler { * obj.fire('test', 1, 2); // prints 3 to the console */ on(name, callback, scope) { - this._addCallback(name, callback, scope, false); - - return this; + const evt = this._addCallback(name, callback, scope, false); + return EventHandler.chaining ? this : evt; } /** @@ -135,27 +143,42 @@ class EventHandler { } if (!name) { + for(let name in this._callbacks) { + const handles = this._callbacks[name]; + for(let i = 0; i < handles.length; i++) { + handles[i].destroy(); + } + } + this._callbacks = { }; } else if (!callback) { - if (this._callbacks[name]) + const handles = this._callbacks[name]; + if (handles) { + for(let i = 0; i < handles.length; i++) { + handles[i].destroy(); + } this._callbacks[name] = []; + } } else { - const events = this._callbacks[name]; - if (!events) + const handles = this._callbacks[name]; + if (!handles) return this; - let count = events.length; + let count = handles.length; for (let i = 0; i < count; i++) { - if (events[i].callback !== callback) + if (handles[i].callback !== callback) continue; - if (scope && events[i].scope !== scope) + if (scope && handles[i].scope !== scope) continue; - events[i--] = events[--count]; + handles[i].destroy(); + // potential issue with such way of removing items from an array, + // as it changes the order of event handlers + handles[i--] = handles[--count]; } - events.length = count; + handles.length = count; } return this; @@ -210,6 +233,7 @@ class EventHandler { if (this._callbackActive[name] === existingCallback) this._callbackActive[name] = this._callbackActive[name].slice(); + this._callbacks[name][ind].destroy(); this._callbacks[name].splice(ind, 1); } } @@ -229,7 +253,7 @@ class EventHandler { * the callback is limited to 8 arguments. * @param {object} [scope] - Object to use as 'this' when the event is fired, defaults to * current this. - * @returns {EventHandler} Self for chaining. + * @returns {EventHandle|EventHandler} {@link EventHandle} if `EventHandler.chaining` set to `false`, otherwise returns `this` (self) for chaining. * @example * obj.once('test', function (a, b) { * console.log(a + b); @@ -237,9 +261,9 @@ class EventHandler { * obj.fire('test', 1, 2); // prints 3 to the console * obj.fire('test', 1, 2); // not going to get handled */ - once(name, callback, scope) { - this._addCallback(name, callback, scope, true); - return this; + once(name, callback, scope = this) { + const evt = this._addCallback(name, callback, scope, true); + return EventHandler.chaining ? this : evt; } /** From fc10c4d496eb9d1c11f52902df57feff68a37cd1 Mon Sep 17 00:00:00 2001 From: mrmaxm Date: Fri, 14 Jul 2023 18:02:10 +0300 Subject: [PATCH 02/16] EventHandle.removed is readonly --- src/core/event-handle.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/event-handle.js b/src/core/event-handle.js index 0668c296c23..8edbba934bd 100644 --- a/src/core/event-handle.js +++ b/src/core/event-handle.js @@ -55,6 +55,7 @@ class EventHandle { /** * True if event has been removed. * @type {boolean} + * @readonly */ removed = false; From e46456b025552d1b6cda4a4c2dd18a0b23edfc35 Mon Sep 17 00:00:00 2001 From: mrmaxm Date: Fri, 14 Jul 2023 18:48:19 +0300 Subject: [PATCH 03/16] lint fixes and behvaiour consistency for eventhandle --- src/core/event-handle.js | 30 ++++++++++++++++++------------ src/core/event-handler.js | 9 ++++----- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/core/event-handle.js b/src/core/event-handle.js index 8edbba934bd..9215e57eee7 100644 --- a/src/core/event-handle.js +++ b/src/core/event-handle.js @@ -5,16 +5,16 @@ * console.log(a + b); * }); * obj.fire('test'); - * + * * evt.off(); // easy way to remove this event * obj.fire('test'); // this will not trigger an event * @example * // store an array of event handles * let events = [ ]; - * + * * events.push(objA.on('testA', () => { })); * events.push(objB.on('testB', () => { })); - * + * * // when needed, remove all events * events.forEach((evt) => { * evt.off(); @@ -55,9 +55,9 @@ class EventHandle { /** * True if event has been removed. * @type {boolean} - * @readonly + * @private */ - removed = false; + _removed = false; /** * @param {EventHandler} handler - source object of the event. @@ -78,20 +78,26 @@ class EventHandle { * Remove references. */ destroy() { - if (this.removed) return; - this.removed = true; - this.handler = null; - this.callback = null; - this.scope = null; + if (this._removed) return; + this._removed = true; } /** * Remove this event from its handler. */ off() { - if (this.removed) return; + if (this._removed) return; this.handler.off(this.name, this.callback, this.scope); } + + /** + * True if event has been removed. + * @type {boolean} + * @readonly + */ + get removed() { + return this._removed; + } } -export { EventHandle }; \ No newline at end of file +export { EventHandle }; diff --git a/src/core/event-handler.js b/src/core/event-handler.js index b93848d73a7..09614c4aa54 100644 --- a/src/core/event-handler.js +++ b/src/core/event-handler.js @@ -33,7 +33,6 @@ class EventHandler { /** * When set to true, `on` and `once` methods will return `this`. * When set to false, `on` and `once` will return `EventHandle`. - * * @type {boolean} */ static chaining = true; @@ -82,7 +81,7 @@ class EventHandler { if (this._callbackActive[name] && this._callbackActive[name] === this._callbacks[name]) this._callbackActive[name] = this._callbackActive[name].slice(); - let evt = new EventHandle(this, name, callback, scope, once); + const evt = new EventHandle(this, name, callback, scope, once); this._callbacks[name].push(evt); return evt; } @@ -143,9 +142,9 @@ class EventHandler { } if (!name) { - for(let name in this._callbacks) { + for (const name in this._callbacks) { const handles = this._callbacks[name]; - for(let i = 0; i < handles.length; i++) { + for (let i = 0; i < handles.length; i++) { handles[i].destroy(); } } @@ -154,7 +153,7 @@ class EventHandler { } else if (!callback) { const handles = this._callbacks[name]; if (handles) { - for(let i = 0; i < handles.length; i++) { + for (let i = 0; i < handles.length; i++) { handles[i].destroy(); } this._callbacks[name] = []; From fa4b6650c6d57e5bd8699671b79841d45cde0c72 Mon Sep 17 00:00:00 2001 From: mrmaxm Date: Fri, 14 Jul 2023 18:58:58 +0300 Subject: [PATCH 04/16] EventHandle fix types definitions --- src/core/event-handle.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/core/event-handle.js b/src/core/event-handle.js index 9215e57eee7..cd6cfc5fa3f 100644 --- a/src/core/event-handle.js +++ b/src/core/event-handle.js @@ -23,7 +23,7 @@ */ class EventHandle { /** - * @type {EventHandler} + * @type {import('./event-handler.js').EventHandler} * @private */ handler; @@ -35,7 +35,7 @@ class EventHandle { name; /** - * @type {HandleEventCallback} + * @type {import('./event-handler.js').HandleEventCallback} * @private */ callback; @@ -60,9 +60,9 @@ class EventHandle { _removed = false; /** - * @param {EventHandler} handler - source object of the event. + * @param {import('./event-handler.js').EventHandler} handler - source object of the event. * @param {string} name - Name of the event. - * @param {HandleEventCallback} callback - Function that is called when event is fired. + * @param {import('./event-handler.js').HandleEventCallback} callback - Function that is called when event is fired. * @param {object} scope - Object that is used as `this` when event is fired. * @param {boolean} [once] - If this is a single event and will be removed after event is fired. */ @@ -93,7 +93,6 @@ class EventHandle { /** * True if event has been removed. * @type {boolean} - * @readonly */ get removed() { return this._removed; From aceb84a3c757f13051080978d453cf02e7078774 Mon Sep 17 00:00:00 2001 From: mrmaxm Date: Fri, 28 Jul 2023 09:14:57 +0300 Subject: [PATCH 05/16] eventhandle some properties should be @ignore instead of @private --- src/core/event-handle.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/event-handle.js b/src/core/event-handle.js index 1c04385887c..79fafd980b1 100644 --- a/src/core/event-handle.js +++ b/src/core/event-handle.js @@ -42,7 +42,7 @@ class EventHandle { /** * @type {object} - * @private + * @ignore */ scope; From 26705b79415a77a2ca08eb59a095f6e2dda50a50 Mon Sep 17 00:00:00 2001 From: mrmaxm Date: Fri, 28 Jul 2023 09:17:56 +0300 Subject: [PATCH 06/16] eventhandle.destroy should be non-public --- src/core/event-handle.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/event-handle.js b/src/core/event-handle.js index 79fafd980b1..ef282949ae7 100644 --- a/src/core/event-handle.js +++ b/src/core/event-handle.js @@ -76,6 +76,7 @@ class EventHandle { /** * Remove references. + * @ignore */ destroy() { if (this._removed) return; From 54d9b1634467989d3ab5299a5d38e9fa3121e244 Mon Sep 17 00:00:00 2001 From: mrmaxm Date: Fri, 28 Jul 2023 11:30:11 +0300 Subject: [PATCH 07/16] ensure we debug log when subscribing to events without arguments --- src/core/event-handler.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/core/event-handler.js b/src/core/event-handler.js index fbd4f5de8e3..6119ff921eb 100644 --- a/src/core/event-handler.js +++ b/src/core/event-handler.js @@ -71,6 +71,11 @@ class EventHandler { * @ignore */ _addCallback(name, callback, scope, once) { + // #if _DEBUG + if (!name || typeof name !== 'string' || !callback) + console.warn(`EventHandler: subscribing to an event (${name}) with missing arguments`, callback); + // #endif + if (!this._callbacks.has(name)) this._callbacks.set(name, []); From c36cd0a50af9d41121ef82d85766e5fd93faae83 Mon Sep 17 00:00:00 2001 From: mrmaxm Date: Fri, 28 Jul 2023 11:34:37 +0300 Subject: [PATCH 08/16] it is possible to subscribe without callback, and we need to avoid crash in such case --- src/core/event-handler.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/core/event-handler.js b/src/core/event-handler.js index 6119ff921eb..40e6b1db9d1 100644 --- a/src/core/event-handler.js +++ b/src/core/event-handler.js @@ -260,6 +260,8 @@ class EventHandler { // eslint-disable-next-line no-unmodified-loop-condition for (let i = 0; (callbacks || this._callbackActive.get(name)) && (i < (callbacks || this._callbackActive.get(name)).length); i++) { const evt = (callbacks || this._callbackActive.get(name))[i]; + if (!evt.callback) continue; + evt.callback.call(evt.scope, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); if (evt.once) { From cb932ae49cacf74db6f6838edab5e229c9f77622 Mon Sep 17 00:00:00 2001 From: mrmaxm Date: Tue, 1 Aug 2023 19:29:39 +0300 Subject: [PATCH 09/16] lint update --- src/core/event-handler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/event-handler.js b/src/core/event-handler.js index 40e6b1db9d1..1da5e5a58cb 100644 --- a/src/core/event-handler.js +++ b/src/core/event-handler.js @@ -261,7 +261,7 @@ class EventHandler { for (let i = 0; (callbacks || this._callbackActive.get(name)) && (i < (callbacks || this._callbackActive.get(name)).length); i++) { const evt = (callbacks || this._callbackActive.get(name))[i]; if (!evt.callback) continue; - + evt.callback.call(evt.scope, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); if (evt.once) { From 587f1a21b5920e54c4d6dafad0461b1fe880a172 Mon Sep 17 00:00:00 2001 From: Maksims Mihejevs Date: Thu, 3 Aug 2023 20:23:10 +0300 Subject: [PATCH 10/16] Do not use chaining for EventHandler --- src/core/event-handler.js | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/src/core/event-handler.js b/src/core/event-handler.js index 1da5e5a58cb..0a5008d269a 100644 --- a/src/core/event-handler.js +++ b/src/core/event-handler.js @@ -30,13 +30,6 @@ import { EventHandle } from './event-handle.js'; * ``` */ class EventHandler { - /** - * When set to true, `on` and `once` methods will return `this`. - * When set to false, `on` and `once` will return `EventHandle` for easier events management. - * @type {boolean} - */ - static chaining = true; - /** * @type {Map>} * @private @@ -101,16 +94,21 @@ class EventHandler { * the callback is limited to 8 arguments. * @param {object} [scope] - Object to use as 'this' when the event is fired, defaults to * current this. - * @returns {EventHandle|EventHandler} {@link EventHandle} if `EventHandler.chaining` set to `false`, otherwise returns `this` (self) for chaining. + * @returns {EventHandle} - that can be used for removing event in the future. * @example * obj.on('test', function (a, b) { * console.log(a + b); * }); * obj.fire('test', 1, 2); // prints 3 to the console + * @example + * const evt = obj.on('test', function (a, b) { + * console.log(a + b); + * }); + * // some time later + * evt.off(); */ on(name, callback, scope = this) { - const evt = this._addCallback(name, callback, scope, false); - return EventHandler.chaining ? this : evt; + return this._addCallback(name, callback, scope, false); } /** @@ -121,7 +119,7 @@ class EventHandler { * the callback is limited to 8 arguments. * @param {object} [scope] - Object to use as 'this' when the event is fired, defaults to * current this. - * @returns {EventHandle|EventHandler} {@link EventHandle} if `EventHandler.chaining` set to `false`, otherwise returns `this` (self) for chaining. + * @returns {EventHandle} - can be used for removing event in the future. * @example * obj.once('test', function (a, b) { * console.log(a + b); @@ -130,8 +128,7 @@ class EventHandler { * obj.fire('test', 1, 2); // not going to get handled */ once(name, callback, scope = this) { - const evt = this._addCallback(name, callback, scope, true); - return EventHandler.chaining ? this : evt; + return this._addCallback(name, callback, scope, true); } /** @@ -195,9 +192,7 @@ class EventHandler { if (!callbacks) return this; - let count = callbacks.length; - - for (let i = 0; i < count; i++) { + for (let i = 0; i < callbacks.length; i++) { // remove all events with a specific name and a callback if (callbacks[i].callback !== callback) continue; @@ -207,13 +202,10 @@ class EventHandler { continue; callbacks[i].destroy(); - // potential issue with such way of removing items from an array, - // as it changes the order of event handlers - callbacks[i--] = callbacks[--count]; + callbacks.splice(i, 1); + i--; } - callbacks.length = count; - if (callbacks.length === 0) this._callbacks.delete(name); } From b0d4eb865c78b6546cf738cef498c9617d65b4f3 Mon Sep 17 00:00:00 2001 From: Maksims Mihejevs Date: Thu, 3 Aug 2023 20:32:28 +0300 Subject: [PATCH 11/16] Add on and once methods to EventHandle for backwards compatibility with deprecation messages --- src/core/event-handle.js | 16 ++++++++++++++-- src/core/event-handler.js | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/core/event-handle.js b/src/core/event-handle.js index ef282949ae7..3e0b1672ae1 100644 --- a/src/core/event-handle.js +++ b/src/core/event-handle.js @@ -1,3 +1,5 @@ +import { Debug } from '../core/debug.js'; + /** * Event Handle that is created by {@link EventHandler} and can be used for easier removing and events management. * @example @@ -50,7 +52,7 @@ class EventHandle { * @type {boolean} * @ignore */ - once; + _once; /** * True if event has been removed. @@ -71,7 +73,7 @@ class EventHandle { this.name = name; this.callback = callback; this.scope = scope; - this.once = once; + this._once = once; } /** @@ -91,6 +93,16 @@ class EventHandle { this.handler.off(this.name, this.callback, this.scope); } + on(name, callback, scope = this) { + Debug.deprecated('Using chaning with EventHandler.on is deprecated, subscribe to an event from EventHandler directly instead.'); + return this.handler._addCallback(name, callback, scope, false); + } + + once(name, callback, scope = this) { + Debug.deprecated('Using chaning with EventHandler.once is deprecated, subscribe to an event from EventHandler directly instead.'); + return this.handler._addCallback(name, callback, scope, true); + } + /** * True if event has been removed. * @type {boolean} diff --git a/src/core/event-handler.js b/src/core/event-handler.js index 0a5008d269a..adb7c344846 100644 --- a/src/core/event-handler.js +++ b/src/core/event-handler.js @@ -256,7 +256,7 @@ class EventHandler { evt.callback.call(evt.scope, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); - if (evt.once) { + if (evt._once) { // check that callback still exists because user may have unsubscribed in the event handler const existingCallback = this._callbacks.get(name); const ind = existingCallback ? existingCallback.indexOf(evt) : -1; From e03357fbb6804968fbdb0b35cf44a24915ab59b0 Mon Sep 17 00:00:00 2001 From: mrmaxm Date: Fri, 4 Aug 2023 12:35:57 +0300 Subject: [PATCH 12/16] Update src/core/event-handle.js Co-authored-by: Will Eastcott --- src/core/event-handle.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/event-handle.js b/src/core/event-handle.js index 3e0b1672ae1..d06116aa659 100644 --- a/src/core/event-handle.js +++ b/src/core/event-handle.js @@ -1,7 +1,8 @@ import { Debug } from '../core/debug.js'; /** - * Event Handle that is created by {@link EventHandler} and can be used for easier removing and events management. + * Event Handle that is created by {@link EventHandler} and can be used for easier event removal and management. + * @example * const evt = obj.on('test', (a, b) => { * console.log(a + b); From 3a614e61062869c1255d965fbd61bb32ffdd16c3 Mon Sep 17 00:00:00 2001 From: mrmaxm Date: Fri, 4 Aug 2023 12:36:10 +0300 Subject: [PATCH 13/16] Update src/core/event-handle.js Co-authored-by: Will Eastcott --- src/core/event-handle.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/event-handle.js b/src/core/event-handle.js index d06116aa659..71b26417cbc 100644 --- a/src/core/event-handle.js +++ b/src/core/event-handle.js @@ -95,7 +95,7 @@ class EventHandle { } on(name, callback, scope = this) { - Debug.deprecated('Using chaning with EventHandler.on is deprecated, subscribe to an event from EventHandler directly instead.'); + Debug.deprecated('Using chaining with EventHandler.on is deprecated, subscribe to an event from EventHandler directly instead.'); return this.handler._addCallback(name, callback, scope, false); } From a85aff900f2db10598f90d97b648a021bb4f81f2 Mon Sep 17 00:00:00 2001 From: mrmaxm Date: Fri, 4 Aug 2023 12:36:29 +0300 Subject: [PATCH 14/16] Update src/core/event-handler.js Co-authored-by: Will Eastcott --- src/core/event-handler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/event-handler.js b/src/core/event-handler.js index adb7c344846..f98581784aa 100644 --- a/src/core/event-handler.js +++ b/src/core/event-handler.js @@ -94,7 +94,7 @@ class EventHandler { * the callback is limited to 8 arguments. * @param {object} [scope] - Object to use as 'this' when the event is fired, defaults to * current this. - * @returns {EventHandle} - that can be used for removing event in the future. + * @returns {EventHandle} Can be used for removing event in the future. * @example * obj.on('test', function (a, b) { * console.log(a + b); From e2c62a51b1eb16495cab157105893ee6c8bc9f94 Mon Sep 17 00:00:00 2001 From: Maksims Mihejevs Date: Mon, 7 Aug 2023 14:14:11 +0300 Subject: [PATCH 15/16] minor jsdoc corrections --- src/core/event-handle.js | 1 - src/core/event-handler.js | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/core/event-handle.js b/src/core/event-handle.js index 71b26417cbc..e67bfdf3b24 100644 --- a/src/core/event-handle.js +++ b/src/core/event-handle.js @@ -2,7 +2,6 @@ import { Debug } from '../core/debug.js'; /** * Event Handle that is created by {@link EventHandler} and can be used for easier event removal and management. - * @example * const evt = obj.on('test', (a, b) => { * console.log(a + b); diff --git a/src/core/event-handler.js b/src/core/event-handler.js index f98581784aa..379a4998412 100644 --- a/src/core/event-handler.js +++ b/src/core/event-handler.js @@ -31,13 +31,13 @@ import { EventHandle } from './event-handle.js'; */ class EventHandler { /** - * @type {Map>} + * @type {Map>} * @private */ _callbacks = new Map(); /** - * @type {Map>} + * @type {Map>} * @private */ _callbackActive = new Map(); From 9cb72cbdd5122722c1c323f1053a780ff4bfe089 Mon Sep 17 00:00:00 2001 From: Maksims Mihejevs Date: Mon, 7 Aug 2023 14:23:35 +0300 Subject: [PATCH 16/16] remove EventHandle.destroy method --- src/core/event-handle.js | 19 ++++++++++--------- src/core/event-handler.js | 8 ++++---- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/core/event-handle.js b/src/core/event-handle.js index e67bfdf3b24..c887485a5c0 100644 --- a/src/core/event-handle.js +++ b/src/core/event-handle.js @@ -76,15 +76,6 @@ class EventHandle { this._once = once; } - /** - * Remove references. - * @ignore - */ - destroy() { - if (this._removed) return; - this._removed = true; - } - /** * Remove this event from its handler. */ @@ -103,6 +94,16 @@ class EventHandle { return this.handler._addCallback(name, callback, scope, true); } + /** + * Mark if event has been removed. + * @type {boolean} + * @internal + */ + set removed(value) { + if (!value) return; + this._removed = true; + } + /** * True if event has been removed. * @type {boolean} diff --git a/src/core/event-handler.js b/src/core/event-handler.js index 379a4998412..aabdee7b26a 100644 --- a/src/core/event-handler.js +++ b/src/core/event-handler.js @@ -174,7 +174,7 @@ class EventHandler { // remove all events for (const callbacks of this._callbacks.values()) { for (let i = 0; i < callbacks.length; i++) { - callbacks[i].destroy(); + callbacks[i].removed = true; } } this._callbacks.clear(); @@ -183,7 +183,7 @@ class EventHandler { const callbacks = this._callbacks.get(name); if (callbacks) { for (let i = 0; i < callbacks.length; i++) { - callbacks[i].destroy(); + callbacks[i].removed = true; } this._callbacks.delete(name); } @@ -201,7 +201,7 @@ class EventHandler { if (scope && callbacks[i].scope !== scope) continue; - callbacks[i].destroy(); + callbacks[i].removed = true; callbacks.splice(i, 1); i--; } @@ -267,7 +267,7 @@ class EventHandler { const callbacks = this._callbacks.get(name); if (!callbacks) continue; - callbacks[ind].destroy(); + callbacks[ind].removed = true; callbacks.splice(ind, 1); if (callbacks.length === 0)