Giter Club home page Giter Club logo

angular2-take-until-destroy's Introduction

Not maintained, use ngx-take-until-destroy

Angular 2+ - Unsubscribe for pros

Declarative way to unsubscribe from observables when the component destroyed

Installation

npm install angular2-take-until-destroy --save

Usage

import { TakeUntilDestroy } from "angular2-take-until-destroy";

@Component({
  selector: 'app-inbox',
  templateUrl: './inbox.component.html'
})
@TakeUntilDestroy
export class InboxComponent implements OnDestroy {
  componentDestroy;
  constructor( ) {
    const timer$ = Observable.interval(1000)
      .takeUntil(this.componentDestroy())
      .subscribe(val => console.log(val))
  }

  // If you work with AOT this method must be present, even if empty! 
  // Otherwise 'ng build --prod' will optimize away any calls to ngOnDestroy, 
  // even if the method is added by the @TakeUntilDestroy decorator
  ngOnDestroy() {
    // You can also do whatever you need here
  }

}

angular2-take-until-destroy's People

Contributors

danmana avatar efidiles avatar netanelbasal avatar patrickjs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

angular2-take-until-destroy's Issues

good job!

good job!
I was looking for a way to easily remove my store subscribers ...

Less intrusive method of tracking subscriptions?

I found this library because it was mentioned on SO:

https://stackoverflow.com/questions/38008334/angular-rxjs-when-should-i-unsubscribe-from-subscription

I really liked the idea of tackling this problem with an unobtrusive approach like decorators, but the approach you've laid out still seems like too much boilerplate (all the takeUntils). What if the decorator instead used AOP and wrapped subscribe calls, then you'd just need the decorator and the ngOnDestroy method? Did you consider such an approach?

AoT build error

When I try to build app with usage of AoT using Angular 5 and Angular CLI 1.5 I'm getting this error from angular2-take-until-destroy:
ERROR in ./node_modules/rxjs/observable/BoundCallbackObservable.js

From what I understand it may the problem of incorrect import from rxjs in library.

Destroys subscriptions in all instances

If there are multiple instances of the same component and one of those instances is destroyed, the current version of this decorator will kill the subscriptions across all instances even though they have not been destroyed.

I fixed the problem here but was unable to provide a PR as you only share your dist and not your source.

import { Subject } from "rxjs/Subject";
export function TakeUntilDestroy(constructor) {
    var original = constructor.prototype.ngOnDestroy;
    constructor.prototype._takeUntilDestroy$ = new Subject();
    constructor.prototype.componentDestroy = function () {
        return this._takeUntilDestroy$.asObservable();
    };
    constructor.prototype.ngOnDestroy = function () {
        original && typeof original === 'function' && original.apply(this, arguments);
        this._takeUntilDestroy$.complete();
    };
}
//# sourceMappingURL=angular2-take-until-destroy.js.map

It would be really helpful to get this in ๐Ÿ™

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.