Skip to content

Instantly share code, notes, and snippets.

@Gbuomprisco
Last active June 29, 2019 12:43
Show Gist options
  • Save Gbuomprisco/2fb919442da1b68bb052474da76ff35e to your computer and use it in GitHub Desktop.
Save Gbuomprisco/2fb919442da1b68bb052474da76ff35e to your computer and use it in GitHub Desktop.
@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