Scenario:
- User uses filters which are combined into single stream
- When filters change, event to the backend is fired to get "cheap" data
- When "cheap" data arrives, another request, with same parameters is fired to different endpoint, that returns "expensive" data which will be used to enrich cheap data. Request should be delayed by 1 second, and only fired if user does not change any of the filters (else it should wait for 1 second)
And i'm struggling with 3) option without intermediate variables.
let filterStream = Rx.Observable
.combineLatest(
filterX,
filterY,
(filterX, filterY) => {
x: filterX,
y: filterY
}
)
.map((filters) => {
limit: 100,
s: filters.x.a,
f: filters.x.b + filters.y.c,
})
.distinctUntilChanged()
let cheapDataStream = filterStream
.flatMapLatest((filterQuery) =>
Rx.Observable.fromPromise(cheapBackendApiCall(filterQuery)))
// render cheap results
cheapDataStream
.map(result => transformForDisplay(result))
.subscribe(result => {
//render
// how do i invoke expensiveApiCall() with `filterQuery` data here?
// with a delay, and only if filterQuery has not changed?
});