Last active
April 19, 2019 01:49
-
-
Save imskojs/0620978d450f423c3fb02d654b10409b to your computer and use it in GitHub Desktop.
Stream Cell Operator Generalized Pattern.
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 { Observable, Subject, BehaviorSubject, merge } from 'rxjs'; | |
import { map, takeUntil } from 'rxjs/operators' | |
@Component({ | |
selector: 'stream-cell-operator-basics', | |
styleUrls: ['./stream-cell-operator-basics.component.css'], | |
template: | |
`<button (click)="redButtonA$.next()"> | |
red | |
</button> | |
<button (click)="greenButtonA$.next()"> | |
green | |
</button> | |
<h1>lbl: {{ labelV$ | async}}</h1>`, | |
}) | |
export class StreamCellOperatorBasicsComponent implements OnInit, OnDestroy{ | |
redButtonA$ = new Subject<void>(); | |
greenButtonA$ = new Subject<void>(); | |
labelV$ = new BehaviorSubject<string>(''); | |
red$: Observable<string> | |
green$: Observable<string> | |
color$: Observable<string> | |
private unsub$ = new Subject<void>(); | |
constructor() { | |
this.red$ = this.SButtonRed.pipe( map(u => 'red') ); | |
this.green$ = this.SButtonGreen.pipe( map(u => 'green') ); | |
this.color$ = merge(this.red$, this.green$); | |
} | |
ngOnInit(): void { | |
this.color$.pipe(takeUntil(this.unsub$)) | |
.subscribe(this.labelV$); | |
} | |
ngOnDestroy(): void { | |
this._unsubscribe.next(); | |
this._unsubscribe.complete(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment