Utility service to manage subscriptions in a declarative way using takeUntil.
To handle subscriptions in a declarative way usually you do have following code:
import { OnDestroy } from '@angular/core';
import { interval, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators'
@Component({
//...
})
export class SomeComponent implements OnDestroy {
private readonly destroy$ = new Subject();
constructor() {
interval(1000)
.pipe(takeUntil(this.destroy$))
.subscribe(// do smth.)
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
}
With NgxDestroy
you reduce a lot of boilerplate:
import { interval } from 'rxjs';
import { NgxDestroy } from '@mikelgo/ngx-destroy';
@Component({
//...
providers: [NgxDestroy]
})
export class SomeComponent {
constructor(private readonly destroy$: NgxDestroy) {
interval(1000)
.pipe(takeUntil(this.destroy$))
.subscribe(// do smth.)
}
}
npm i @code-workers.io/ngx-destroy
A demo can be found here
- ngx-destroy 13.3.0 is compatible with Angular >= 13.3.0