-
Notifications
You must be signed in to change notification settings - Fork 1.9k
EventHandle and option to disable EventHandler chaining. #5481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
8e582b7
fc10c4d
e46456b
fa4b665
438ac8b
aceb84a
26705b7
54d9b16
c36cd0a
cb932ae
587f1a2
b0d4eb8
e03357f
3a614e6
a85aff9
e2c62a5
9cb72cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /** | ||
| * 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 {import('./event-handler.js').EventHandler} | ||
| * @private | ||
| */ | ||
| handler; | ||
|
|
||
| /** | ||
| * @type {string} | ||
| * @private | ||
| */ | ||
| name; | ||
|
|
||
| /** | ||
| * @type {import('./event-handler.js').HandleEventCallback} | ||
| * @ignore | ||
| */ | ||
| callback; | ||
|
|
||
| /** | ||
| * @type {object} | ||
| * @ignore | ||
| */ | ||
| scope; | ||
|
|
||
| /** | ||
| * @type {boolean} | ||
| * @ignore | ||
| */ | ||
| once; | ||
|
|
||
| /** | ||
| * True if event has been removed. | ||
| * @type {boolean} | ||
| * @private | ||
| */ | ||
| _removed = false; | ||
|
|
||
| /** | ||
| * @param {import('./event-handler.js').EventHandler} handler - source object of the event. | ||
|
Maksims marked this conversation as resolved.
|
||
| * @param {string} name - Name of the event. | ||
| * @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. | ||
| */ | ||
| constructor(handler, name, callback, scope, once = false) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we hide the constructor from the docs? Presumably end users would never construct the |
||
| this.handler = handler; | ||
| this.name = name; | ||
| this.callback = callback; | ||
| this.scope = scope; | ||
| this.once = once; | ||
| } | ||
|
|
||
| /** | ||
| * Remove references. | ||
| * @ignore | ||
| */ | ||
| destroy() { | ||
| if (this._removed) return; | ||
| this._removed = true; | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't a destroyed handle
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Every other destroy() {
// remove any outstanding listeners
this._registry.off("load", this._onLoad);
this._registry.off("error", this._onError);
// ...
}Why is this a special case? It also doesn't really do anything at all right now, is it a draft?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Events are "detached" rather than "destroyed". This PR does not change that and follows an existing pattern. |
||
|
|
||
| /** | ||
| * Remove this event from its handler. | ||
| */ | ||
| off() { | ||
| if (this._removed) return; | ||
| this.handler.off(this.name, this.callback, this.scope); | ||
| } | ||
|
|
||
| /** | ||
| * True if event has been removed. | ||
| * @type {boolean} | ||
| */ | ||
| get removed() { | ||
| return this._removed; | ||
| } | ||
|
Comment on lines
+107
to
+113
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need all this extra getter code which kinda does nothing? Why not simple and short: /**
* True if event has been removed.
* @type {boolean}
* @readonly
*/
removed = false;If I get your intention right, you just want to have it readable and not writable?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I just realized we still lack proper types to express this idiom: microsoft/TypeScript#37487
~~ destroy() {
this.handler?.off(this.name, this.callback, this.scope);
this.handler = null;
}~~
Edit: outdated / do-not-use / etc. |
||
| } | ||
|
|
||
| export { EventHandle }; | ||
Uh oh!
There was an error while loading. Please reload this page.