Worked on several approaches to this. Basically, I don’t want poller to run ajax every 30 seconds from the start of the poll - I want poller to run requests 30 seconds AFTER returning the previous request. In addition, I want to work in some strategy around the exponential deviation for failures.
Here is what I still have (Rx4):
rx.Observable.create(function(observer) {
var nextPoll = function(obs) {
rx.Observable.fromPromise(action())
.map(function (x){ return x.data; })
.subscribe(function(d) {
observer.onNext(d);
interval = initInterval;
setTimeout(function(){ nextPoll(obs); }, interval);
}, function(e) {
interval = interval < maxInterval ? interval * 2 : maxInterval;
setTimeout(function(){ nextPoll(obs); }, interval);
});
};
nextPoll(observer);
});
For the most part this does what I want. I don’t like using setTimeout, but I can’t find a better Observable approach to this (except for a one-time interval / timer with a different subscription).
, , - , , , . , . /, ajax ajax , .
, , setTimeout. , - , ! !
: - , . :
function computeInterval(error) {
if (error) {
interval = interval < maxInterval ? interval * 2 : maxInterval;
} else {
interval = initInterval;
}
return interval;
}
poller$ = rx.Observable.fromPromise(function(){ return _this.action(); })
.retryWhen(function(errors){
return errors.scan(function(acc, x) { return acc + x; }, 0)
.flatMap(function(x){
return rx.Observable.timer(computeInterval(true));
});
})
.repeatWhen(function(notification){
return notification
.scan(function(acc, x) { return acc + x; }, 0)
.flatMap(function(x){
return rx.Observable.timer(computeInterval());
});
});