Skip to content

Instantly share code, notes, and snippets.

@Gbuomprisco
Created May 27, 2019 14:41
Show Gist options
  • Save Gbuomprisco/478427313c28083127a30c6e58c70767 to your computer and use it in GitHub Desktop.
Save Gbuomprisco/478427313c28083127a30c6e58c70767 to your computer and use it in GitHub Desktop.
@Component({
selector: 'cf-asset-pricer',
templateUrl: './asset-pricer.component.html',
styleUrls: ['./asset-pricer.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class AssetPricerComponent implements OnInit {
@Input()
public asset: string;
public price$: Observable<string>;
public trend$: Observable<Trend>;
public readonly trends = Trend;
constructor(private pricesFacade: PricesFacadeService) {}
ngOnInit() {
this.price$ = this.pricesFacade.getPriceForAsset(this.asset).pipe(
filter(Boolean),
map((price: string) => {
return parseFloat(price).toFixed(2);
}),
shareReplay(1)
);
const timer$ = this.price$.pipe(
switchMap(() => timer(2000)),
mapTo(Trend.Stale)
);
const trend$ = this.price$.pipe(
pairwise(),
filter((prices: string[]) => prices[0] !== prices[1]),
map((prices: string[]) => prices.map(parseFloat)),
map(([previous, current]: number[]) => {
return current > previous ? Trend.Up : Trend.Down;
})
);
this.trend$ = merge(trend$, timer$);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment