Last active
June 29, 2019 12:43
-
-
Save Gbuomprisco/2fb919442da1b68bb052474da76ff35e 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
@Component({ | |
selector: 'my-app', | |
templateUrl: './app.component.html', | |
styleUrls: ['./app.component.css'], | |
changeDetection: ChangeDetectionStrategy.OnPush | |
}) | |
export class AppComponent { | |
public price$: Observable<string | undefined>; | |
public currency$ = new BehaviorSubject<string | undefined>(undefined); | |
constructor(private api: PriceApiService) { } | |
ngOnInit() { | |
this.price$ = this.currency$.pipe( | |
mergeMap((currency: string | undefined) => { | |
return currency ? this.api.getPrice(currency) : of(undefined); | |
}), | |
shareReplay(1) | |
); | |
} | |
onCryptoSelected(currency: string) { | |
if (this.currency$.value) { | |
this.api.unsubscribe(); | |
} | |
this.currency$.next(currency); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment