Skip to content
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

[FEATURE ember-metal-meta-destructors] #14602

Closed
wants to merge 1 commit into from
Closed

Conversation

rwjblue
Copy link
Member

@rwjblue rwjblue commented Nov 11, 2016

Introduces a mechanism to add additional work to be done at object / meta destruction time.

Example:

// app/utils/run-later.js
export default function runLater(object, functionOrName, delay) {
  let cancelId = run.later(object, functionOrName, delay);
  Ember.meta(object).addDestructor(() => run.cancel(cancelId));
}
// some component
export default Component.extend({
  stuff() { },

  actions: {
    doStuff() {
      runLater(this, this._stuff, 1000);
    }
  }
})

When this component is destroyed, the scheduled task will be canceled. A number of popular addons do similar things by monkey patching the objects willDestroy at runtime, but that is less than ideal (due to shape changes and whatnot).

This provides a mechanism to easily entangle cleanup tasks for objects from utility functions...

Introduces a mechanism to add additional work to be done at object / meta destruction time.

Example:

```js
// app/utils/run-later.js
export default function runLater(object, functionOrName, delay) {
  let cancelId = run.later(object, functionOrName, delay);
  Ember.meta(object).addDestructor(() => run.cancel(cancelId));
}
```

```js
// some component
export default Component.extend({
  stuff() { },

  actions: {
    doStuff() {
      runLater(this, this._stuff, 1000);
    }
  }
})
```

When this component is destroyed, the scheduled task will be canceled. A number of popular addons do similar things
by monkey patching the objects `willDestroy` at runtime, but that is less than ideal (due to shape changes and whatnot).

This provides a mechanism to easily entangle cleanup tasks for objects from utility functions...
@rwjblue
Copy link
Member Author

rwjblue commented Nov 11, 2016

Would love review/feedback from folks I know have been working on and around similar issues to my example above.

/cc @machty @krisselden @stefanpenner @runspired

@machty
Copy link
Contributor

machty commented Nov 11, 2016

Alternatively, wouldn't it be possible to implement destructors using ember-metal's listener APIs and just broadcast a willDestroy event? Then anyone will be able to do Ember.on('willDestroy', ()=>{}) and have all the destructors determined up front at mixin time. Not to mention people would expect there to be willDestroy events if there's an init event.

@runspired
Copy link
Contributor

I think we should go all in on cancellation tokens as the primitive for these things, and I'm hesitant to grow the API surface area instead of doubling down on building a great async story top to bottom.

But otherwise, looks good, seems forward compatible.

@pixelhandler
Copy link
Contributor

pixelhandler commented Nov 18, 2016

@rwjblue I like the idea of cancelling a scheduled task during the destroy phase. Over the past week I've had to add checks for this.isDestroying and this.isDestroyed in scheduled functions for components. During testing components are torn down much quicker and more often compared to using an app. So, with your suggestion here, the extra guarding could go away - nice.

@homu
Copy link
Contributor

homu commented Dec 18, 2016

☔ The latest upstream changes (presumably #14360) made this pull request unmergeable. Please resolve the merge conflicts.

@locks
Copy link
Contributor

locks commented Jun 18, 2019

Not applicable anymore.

@rwjblue
Copy link
Member Author

rwjblue commented Feb 10, 2021

For those that might stumble across this old PR, this was RFC'ed in emberjs/rfcs#545

@rwjblue rwjblue deleted the add-meta-destructors branch February 10, 2021 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants