Last active
December 29, 2016 21:06
-
-
Save jsayol/4992d0e7af72e0f7652f893fd7909e06 to your computer and use it in GitHub Desktop.
This file contains hidden or 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, ElementRef, OnDestroy, Renderer } from '@angular/core'; | |
import { Observable } from 'rxjs/Observable'; | |
import 'rxjs/add/observable/timer'; | |
import 'rxjs/add/observable/merge'; | |
import 'rxjs/add/operator/takeWhile'; | |
import 'rxjs/add/operator/mapTo'; | |
@Component({ | |
selector: 'blink', | |
template: `<ng-content></ng-content>` | |
}) | |
export class BlinkComponent implements OnInit, OnDestroy { | |
private element: Element; | |
private blinker$: Observable<string>; | |
private active: boolean = true; | |
constructor(private renderer: Renderer, elementRef: ElementRef) { | |
this.element = elementRef.nativeElement; | |
const show$ = Observable.timer(0, 1000); | |
const hide$ = Observable.timer(750, 1000); | |
this.blinker$ = Observable.merge( | |
show$.mapTo('visible'), | |
hide$.mapTo('hidden') | |
); | |
} | |
private set visibility(value: string) { | |
this.renderer.setElementStyle(this.element, 'visibility', value); | |
} | |
ngOnInit() { | |
this.blinker$ | |
.takeWhile(() => this.active) | |
.subscribe((newVisiblity: string) => this.visibility = newVisiblity); | |
} | |
ngOnDestroy() { | |
this.active = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment