Last active
February 3, 2020 19:25
-
-
Save kaplan81/675730f2943daef26e6fec4fbed3dc0a to your computer and use it in GitHub Desktop.
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 { Component, OnInit } from '@angular/core'; | |
import { interval, Observable } from 'rxjs'; | |
import { takeUntil } from 'rxjs/operators'; | |
import { subscribedContainerMixin } from '@my-scope/my-ng-lib/subscribed-container.mixin'; | |
@Component({ | |
selector: 'subscribed-container', | |
templateUrl: './subscribed-container.component.html' | |
}) | |
export class SubscribedContainerComponent extends subscribedContainerMixin() implements OnInit { | |
// We use interval because it does not complete. | |
observable$: Observable<number> = interval(1000); | |
subscription$$: Subscription; | |
ngOnInit(): void { | |
this.subscription$$ = this.observable$ | |
.pipe(tap(console.log), takeUntil(this.destroyed$)) | |
.subscribe(); | |
} | |
// If you need to do something on destroy in the component class. | |
// tslint:disable-next-line: use-lifecycle-interface | |
ngOnDestroy(): void { | |
// tslint:disable-next-line: no-unused-expression no-string-literal | |
super['ngOnDestroy'] && super['ngOnDestroy'](); | |
console.log('this.subscription$$.closed in ngOnDestroy::', this.subscription$$.closed); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I just made it into a mixin. Now it is scalable. You can extend the component from several mixins at the same time and you do not need to add
super()
.