Function
registerDestructor (destroyable, destructor) public
Defined in packages/@ember/destroyable/index.ts:162
import { registerDestructor } from '@ember/destroyable'; |
- destroyable
- Object|Function
- the destroyable to register the destructor function with
- destructor
- Function
- the destructor to run when the destroyable object is destroyed
Receives a destroyable object and a destructor function, and associates the function with it. When the destroyable is destroyed with destroy, or when its parent is destroyed, the destructor function will be called.
1 2 3 4 5 6 7 8 9 10 11 |
import { registerDestructor } from ' /destroyable'; class Modal extends Component { resize; constructor() { this.resize.register(this, this.layout); registerDestructor(this, () => this.resize.unregister(this)); } } |
Multiple destructors can be associated with a given destroyable, and they can be
associated over time, allowing libraries to dynamically add destructors as needed.
registerDestructor
also returns the associated destructor function, for convenience.
The destructor function is passed a single argument, which is the destroyable itself. This allows the function to be reused multiple times for many destroyables, rather than creating a closure function per destroyable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import { registerDestructor } from ' /destroyable'; function unregisterResize(instance) { instance.resize.unregister(instance); } class Modal extends Component { resize; constructor() { this.resize.register(this, this.layout); registerDestructor(this, unregisterResize); } } |