All Projects → ngneat → Until Destroy

ngneat / Until Destroy

Licence: mit
🦊 RxJS operator that unsubscribe from observables on destroy

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Until Destroy

Batcave
Batcave client is chat app built with Electron and Angular2, Socket.io with RxJS.
Stars: ✭ 114 (-89.36%)
Mutual labels:  rxjs, angular2
ngx-redux-core
The modern redux integration for Angular 6+
Stars: ✭ 32 (-97.01%)
Mutual labels:  angular2, decorators
Ayanami
🍭 A better way to react with state
Stars: ✭ 129 (-87.96%)
Mutual labels:  rxjs, decorators
Blog
lizhonghui's blog
Stars: ✭ 109 (-89.82%)
Mutual labels:  rxjs, angular2
Ngx Auto Unsubscribe
Class decorator that will automatically unsubscribe from observables
Stars: ✭ 337 (-68.53%)
Mutual labels:  rxjs, decorators
Blog Angular
Angular 笔记
Stars: ✭ 238 (-77.78%)
Mutual labels:  rxjs, angular2
ng-observe
Angular reactivity streamlined...
Stars: ✭ 65 (-93.93%)
Mutual labels:  rxjs, angular2
angular2-instagram
🔥Instagram like photo filter playground built with Angular2 (Web | Desktop)
Stars: ✭ 91 (-91.5%)
Mutual labels:  rxjs, angular2
ng-radio
RxJS-based message bus service for Angular2
Stars: ✭ 12 (-98.88%)
Mutual labels:  rxjs, angular2
bind-observable
Provides a typescript decorator which binds class properties to observable companion properties.
Stars: ✭ 15 (-98.6%)
Mutual labels:  rxjs, decorators
Dugong
Minimal State Store Manager for React Apps using RxJS
Stars: ✭ 10 (-99.07%)
Mutual labels:  rxjs, decorators
Ngx Restangular
Restangular for Angular 2 and higher versions
Stars: ✭ 787 (-26.52%)
Mutual labels:  rxjs, angular2
Angular2 Take Until Destroy
Declarative way to unsubscribe from observables when the component destroyed
Stars: ✭ 40 (-96.27%)
Mutual labels:  rxjs, angular2
Rxloop
rxloop = Redux + redux-observable (Inspired by dva)
Stars: ✭ 44 (-95.89%)
Mutual labels:  rxjs
Ng2 Sweetalert2
A sweetalert2 service for angular2.
Stars: ✭ 49 (-95.42%)
Mutual labels:  angular2
Rxemitter
RxEmitter = 🐟Rxjs + 🐡eventBus.
Stars: ✭ 43 (-95.99%)
Mutual labels:  rxjs
Evolui
A tiny reactive user interface library, built on top of RxJs.
Stars: ✭ 43 (-95.99%)
Mutual labels:  rxjs
Ngautocomplete
Light-weight autocomplete component for Angular.
Stars: ✭ 52 (-95.14%)
Mutual labels:  angular2
Moleculer Decorators
decorators for moleculer
Stars: ✭ 47 (-95.61%)
Mutual labels:  decorators
Ember Arg Types
Runtime type checking & defaulting for glimmer component arguments powered by prop-types & decorators
Stars: ✭ 43 (-95.99%)
Mutual labels:  decorators

🦁 Unsubscribe For Pros

A neat way to unsubscribe from observables when the component destroyed

@ngneat/until-destroy npm

Table of contents

Angular 10 Compatibility

@ngneat/[email protected]+ is compatible only with Angular starting from 10.0.5 version. @ngneat/[email protected] is compatible with Angular versions below 10.0.5. If you have noticed that unsubscribing does not work after some update then check the version of Angular and @ngneat/until-destroy first of all.

Use with Ivy

npm install @ngneat/until-destroy
# Or if you use yarn
yarn add @ngneat/until-destroy
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';

@UntilDestroy()
@Component({})
export class InboxComponent {
  ngOnInit() {
    interval(1000)
      .pipe(untilDestroyed(this))
      .subscribe();
  }
}

You can set the checkProperties option to true if you want to unsubscribe from subscriptions properties automatically:

@UntilDestroy({ checkProperties: true })
@Component({})
export class HomeComponent {
  // We'll dispose it on destroy
  subscription = fromEvent(document, 'mousemove').subscribe();
}

You can set the arrayName property if you want to unsubscribe from each subscription in the specified array.

@UntilDestroy({ arrayName: 'subscriptions' })
@Component({})
export class HomeComponent {
  subscriptions = [
    fromEvent(document, 'click').subscribe(),
    fromEvent(document, 'mousemove').subscribe()
  ];

  // You can still use the operator
  ngOnInit() {
    interval(1000).pipe(untilDestroyed(this));
  }
}

You can set the blackList property if you don't want to unsubscribe from one or more subscriptions.

@UntilDestroy({ checkProperties: true, blackList: ['subscription1'] })
@Component({})
export class HomeComponent {
  // subscription1 will not be unsubscribed upon component destruction
  subscription1: Subscription;
  // subscription2 will be unsubscribed upon component destruction
  subscription2: Subscription;

  constructor() {
    this.subscription1 = new Subject().subscribe();
    this.subscription2 = new Subject().subscribe();
  }
}

Use with Non-Singleton Services

@UntilDestroy()
@Injectable()
export class InboxService {
  constructor() {
    interval(1000)
      .pipe(untilDestroyed(this))
      .subscribe();
  }
}

@Component({
  providers: [InboxService]
})
export class InboxComponent {
  constructor(inboxService: InboxService) {}
}

All options, described above, are also applicable to providers.

Use with View Engine (Pre Ivy)

npm install ngx-take-until-destroy
# Or if you use yarn
yarn add ngx-take-until-destroy
import { untilDestroyed } from 'ngx-take-until-destroy';

@Component({})
export class InboxComponent implements OnDestroy {
  ngOnInit() {
    interval(1000)
      .pipe(untilDestroyed(this))
      .subscribe(val => console.log(val));
  }

  // This method must be present, even if empty.
  ngOnDestroy() {
    // To protect you, we'll throw an error if it doesn't exist.
  }
}

Use with Any Class

import { untilDestroyed } from 'ngx-take-until-destroy';

export class Widget {
  constructor() {
    interval(1000)
      .pipe(untilDestroyed(this, 'destroy'))
      .subscribe(console.log);
  }

  // The name needs to be the same as the second parameter
  destroy() {}
}

Migration from View Engine to Ivy

To make it easier for you to migrate, we've built a script that will update the imports path, and add the decorator for you.

npx @ngneat/until-destroy --base my/path

base defaults to ./src/app.

You can use --removeOnDestroy flag for empty OnDestroy methods removing.

npx @ngneat/until-destroy --removeOnDestroy

Potential Pitfalls

  • The order of decorators is important, make sure to put @UntilDestroy() before the @Component() decorator.
  • When using overrideComponent in unit tests remember that it overrides metadata and component definition. Invoke UntilDestroy()(YourComponent); to reapply the decorator. See here for an example.

ESLint Rules

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Netanel Basal

💻 📖 🤔

Artur Androsovych

💻 💡 🤔 🚇

Krzysztof Karol

🖋

Alex Malkevich

💻

Khaled Shaaban

💻

kmathy

💻

Dmitrii Korostelev

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].