Skip to content

Featrue Request: BuiltIn way to unsubscribe from observables when the component destroyed #20941

Closed

Description

@nasreddineskandrani

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[X ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Basically this post show a way with takeUntil to don't unsubscribe all observeables in ngOnDestroy.
https://netbasal.com/automagically-unsubscribe-in-angular-4487e9853a88
Using this (it's one example of lib)
https://github.com/NetanelBasal/angular2-take-until-destroy

Why not making this or something similar directly part of angular framework?
(I can make a PR if you want to move forward with this)

Activity

  1. ericmartinezr commented on Dec 11, 2017

    @ericmartinezr
    Contributor

    Is this #10185 what you're asking for?

  2. kemsky commented on Dec 12, 2017

    @kemsky

    Component inheritance works pretty well now, so it should be easy to create base class that provides automatic cleanup on destroy: unsubscribing, removing event listeners and disposing other things. Using decorator will lack compile time checks. Base class can also solve other problems: better handling for workarounds with setTimeout, provide state: initialized, destroyed, view initialized, provide support for automation and component-level logging, provide global template variables i.e. expose translation api, console object, enumerations and etc, provide lifecycle callbacks as base class methods without implementing interfaces every time.

  3. nasreddineskandrani commented on Dec 12, 2017

    @nasreddineskandrani
    Author

    @kemsky i don't ask to use decorator. It was just an example of a way that may solve it. I want angular team to add this (base class/decorator/...) to the framework to don't have to redo this each time in each project or depend on a 3rd party library with a decorator approach that (may break) or (is not optimal).

    @ericmartinezr thanks! i ll close this issue => as duplicate of #10185

    note: my coworker found this PR related to the topic:
    #20682

  4. insidewhy commented on Feb 21, 2018

    @insidewhy

    @kemsky Problem with component inheritance is you can only extend a single class, if you want to provide different features they all need to be bundled into it. Not a viable long-term solution (although we're using it to work around the lack of lifecycle observables).

  5. kemsky commented on Apr 24, 2018

    @kemsky

    @ohjames, you could try to use TypeScript mixins, but I'm not sure if it works with angular without workarounds.

  6. nasreddineskandrani commented on Aug 17, 2018

    @nasreddineskandrani
    Author

    @ohjames since angular team delayed the feature (#20682) for ng7 Ivy, you can use a custom decorator that you create your self https://netbasal.com/automagically-unsubscribe-in-angular-4487e9853a88
    personally i am using inheritance for now.

  7. dinohorvat commented on Mar 27, 2019

    @dinohorvat

    Custom decorator won't work well if you build the app with AOT. You still need to declare ngOnDestroy even if its empty.

  8. angular-automatic-lock-bot commented on Sep 14, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  9. locked and limited conversation to collaborators on Sep 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreIssues related to the framework runtime

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions