Reputation: 365
I have an obeservable sequence, where each time a call realease event occurs I map that event to an http request that returns some call logs that I then filter and need to get a count form. The problem is that even though the mapped obserbable that returns the json from the http request and the sequence has completed the orginal fromEvent Observable has not terminated so count will not emit anything. I need a count of the filtered amount each time the http request comes back not sure how to do this.
var seq = Rx.Observable
.fromEvent(call realease event)
.flatMap(agentObj =>
Rx.Observable.fromPromise(callLogHelper.getUserCallLogs(agentObj.agentId))
)
.flatMap(logs => Rx.Observable.fromArray(logs))
.filter(log => log.callTime >= 210)
.count();
I tried a few methods that very helpful people offered including user3743222 , which i am very thankful for. These included scan, sample, expand and using the idx argument in map and filter. The main issue i keep getting is that these methods return a continuous running total and because the http request can have duplicate data from the last time it was called the running count needs to be reset each time the http request is returned.
I ended up just mapping the filtered values from the request in the flatMap as simple array operations and then just returning the length. I feel like this is a hacky solution tho
var seq = Rx.Observable
.fromEvent(call realease event)
.flatMap(agentObj =>
Rx.Observable.fromPromise(callLogHelper.getUserCallLogs(agentObj.agentId))
)
.flatMap((logs) => {
var filteredLogs = logs.filter(log => log.callTime >= 210);
return Rx.Observable.of(filteredLogs.length);
})
Upvotes: 2
Views: 1887
Reputation: 18663
Based on the solution you came up with it would seem simpler to just do:
var seq = Rx.Observable
.fromEvent(call realease event)
.flatMap(
agentObj => callLogHelper.getUserCallLogs(agentObj.agentId),
(ao, logs) => logs.filter(log => log.callTime >= 210).length
);
Upvotes: 2
Reputation: 18665
One way to do this is to use the scan
operator. In practice, it would be something like :
var seq = Rx.Observable
.fromEvent(call realease event)
.flatMap(flatMap(agentObj =>
Rx.Observable.fromPromise(callLogHelper.getUserCallLogs(agentObj.agentId))
))
.flatMap(logs => Rx.Observable.fromArray(logs))
.filter(log => log.callTime >= 210)
.scan (function (count, _){return ++count},0);
Upvotes: 0