Skip to content

Commit

Permalink
add Disposable.addDisposerAction(), phetsims/axon#455
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Jan 6, 2025
1 parent a4e0aa7 commit 319c524
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion js/nodes/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
*/

import BooleanProperty, { BooleanPropertyOptions } from '../../../axon/js/BooleanProperty.js';
import { DisposerOptions } from '../../../axon/js/Disposable.js';
import EnabledProperty, { EnabledPropertyOptions } from '../../../axon/js/EnabledProperty.js';
import Property, { PropertyOptions } from '../../../axon/js/Property.js';
import TEmitter from '../../../axon/js/TEmitter.js';
Expand Down Expand Up @@ -2337,7 +2338,7 @@ class Node extends ParallelDOM {
* - cursor {string|null}: If node.cursor is null, any non-null cursor of an input listener will effectively
* "override" it. NOTE: this can be implemented as an es5 getter, if the cursor can change
*/
public addInputListener( listener: TInputListener ): this {
public addInputListener( listener: TInputListener, options?: DisposerOptions ): this {
assert && assert( !_.includes( this._inputListeners, listener ), 'Input listener already registered on this Node' );
assert && assert( listener !== null, 'Input listener cannot be null' );
assert && assert( listener !== undefined, 'Input listener cannot be undefined' );
Expand All @@ -2353,7 +2354,11 @@ class Node extends ParallelDOM {
if ( listener.hotkeys ) {
hotkeyManager.updateHotkeysFromInputListenerChange( this );
}

// If a disposer is specified, then automatically remove this input listener when the disposer is disposed.
options?.disposer && this.addDisposerAction( 'inputListener', listener, options.disposer, () => this.removeInputListener( listener ) );
}

return this;
}

Expand All @@ -2375,6 +2380,9 @@ class Node extends ParallelDOM {
if ( listener.hotkeys ) {
hotkeyManager.updateHotkeysFromInputListenerChange( this );
}

// undo addDisposerAction (see above)
this.removeDisposerAction( 'inputListener', listener );
}

return this;
Expand Down

0 comments on commit 319c524

Please sign in to comment.