Skip to content

Instantly share code, notes, and snippets.

@estellepicq
Last active December 2, 2021 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save estellepicq/e94d530f5d4ef5d14be7d597e9cf763a to your computer and use it in GitHub Desktop.
Save estellepicq/e94d530f5d4ef5d14be7d597e9cf763a to your computer and use it in GitHub Desktop.
Generic unsubscriber for angular components
import { ReplaySubject } from 'rxjs';
import { OnDestroy, Component } from '@angular/core';
@Component({
template: '',
})
export class ObservableDestroyComponent implements OnDestroy {
protected destroyed$: ReplaySubject<void> = new ReplaySubject(1);
ngOnDestroy(): void {
this.destroyed$.next();
this.destroyed$.complete();
}
}
export class ViewComponent extends ObservableDestroyComponent {
constructor(
private readonly dataService: DataService
) {
super();
this.dataService.data$.pipe(
takeUntil(this.destroyed$)
).subscribe(data => this.data = data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment