Last active
December 2, 2021 20:23
-
-
Save estellepicq/e94d530f5d4ef5d14be7d597e9cf763a to your computer and use it in GitHub Desktop.
Generic unsubscriber for angular components
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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