Skip to content

Instantly share code, notes, and snippets.

@kaplan81
Last active February 4, 2020 08:46
Show Gist options
  • Save kaplan81/2a25472f5b139746a15eb4aedddef197 to your computer and use it in GitHub Desktop.
Save kaplan81/2a25472f5b139746a15eb4aedddef197 to your computer and use it in GitHub Desktop.
import { ChangeDetectionStrategy, Component, OnDestroy, OnInit } from '@angular/core';
import { interval, Observable, Subscription } from 'rxjs';
import { takeUntil, tap } from 'rxjs/operators';
import { ondestroy } from '../_app/decorators/ondestroy.decorator';
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: './decorator.component.html',
})
export class DecoratorComponent implements OnInit, OnDestroy {
observable$: Observable<number> = interval(1000);
subscription$$: Subscription;
ngOnInit(): void {
this.subscription$$ = this.observable$
.pipe(tap(console.log), takeUntil((this as any).destroyed$))
.subscribe();
}
@ondestroy()
ngOnDestroy(): void {
// Any extra actions:
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