typescript - RxJS delay at least X milliseconds -
i'm trying implement following behavior in rxjs:
- fire event
- call http api
- when api returns, either:
- wait until @ least x milliseconds have passed since firing event
- return if x milliseconds have passed since firing event
this useful ux, because if call takes 1ms, i'd show loading icon @ least 100ms.
i haven't found way implement either delay
, throttle
, debounce
or variations.
this.eventthatfires .switchmap(data => { let starttime = date.now(); return this.callhttpservice(data) .delay(new date(starttime + 1000)); })
i assumed worked, using absolute date seems time difference current time, , not schedule delay absolute time.
edit:
it seems there's no built-in operator works describe. created because i'll using lot throughout application:
import { observable } "rxjs/observable"; function delayatleast<t>(this: observable<t>, delay: number): observable<t> { return observable.combinelatest( observable.timer(delay), this) .map(([_, i]) => i); } observable.prototype.delayatleast = delayatleast; declare module "rxjs/observable" { interface observable<t> { delayatleast: typeof delayatleast; } }
effectively delay
date same delay
number, difference delay duration computed difference of specified date , current time.
you can use delaywhen
operator compute delay when value emitted:
this.eventthatfires .switchmap(data => { let starttime = date.now(); return this.callhttpservice(data) .delaywhen(() => rx.observable.timer(500 + starttime - date.now())) })
Comments
Post a Comment