Function

registerDestructor (destroyable, destructor) public

Module: @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.

import { registerDestructor } from '@ember/destroyable';

class Modal extends Component {
  @service 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.

import { registerDestructor } from '@ember/destroyable';

function unregisterResize(instance) {
  instance.resize.unregister(instance);
}

class Modal extends Component {
  @service resize;

  constructor() {
    this.resize.register(this, this.layout);

    registerDestructor(this, unregisterResize);
  }
}

© 2020 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://api.emberjs.com/ember/3.25/functions/@ember%2Fdestroyable/registerDestructor