Skip to content

Instantly share code, notes, and snippets.

@malte-wessel
Last active December 14, 2018 09:47
Show Gist options
  • Save malte-wessel/a51c38611a0f9def80a9102d8efc7032 to your computer and use it in GitHub Desktop.
Save malte-wessel/a51c38611a0f9def80a9102d8efc7032 to your computer and use it in GitHub Desktop.
Polling with Observables / RxJs
import { Observable } from 'rxjs/Observable';
import { fromPromise } from 'rxjs/observable/fromPromise';
import 'rxjs/add/operator/delay';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/expand';
import 'rxjs/add/operator/takeWhile';
import 'rxjs/add/operator/catch';
const defaultIsComplete = () => false;
const defaultKeepIntervalLow = () => false;
const defaultIntervals = [0, 1000, 1500, 1500, 2000, 2000, 4000, 6000];
const defaultRequest = () => {
throw new Error('The `request` option is required to start polling.');
};
const poll = (params, options = {}) => {
const {
isComplete = defaultIsComplete,
request = defaultRequest,
keepIntervalLow = defaultKeepIntervalLow,
maxAttemps = 15,
maxPolls = 15,
intervals = defaultIntervals,
} = options;
const initialState = {
params,
data: null,
status: 'started',
i: 0,
attemps: 0,
error: null,
intervalIndex: 0,
delay: 0
};
const updateDelay = state => {
const { status, intervalIndex, attemps } = state;
if (status === 'retry') {
return {
...state,
intervalIndex: 0,
delay: Math.pow(2, attemps) * 250
};
}
const intervalIndexNext = keepIntervalLow(state)
? state.intervalIndex
: Math.min(intervalIndex + 1, intervals.length - 1);
return {
...state,
intervalIndex: intervalIndexNext,
delay: intervals[intervalIndex]
};
};
return Observable
.of(initialState)
.expand(state => {
const { status, attemps, i } = state;
if (status === 'completed' || status === 'error') {
return Observable.of({
...state,
status: 'kill'
});
}
if (status === 'retry' && attemps >= maxAttemps) {
return Observable.of({
...state,
status: 'error'
});
}
if (isComplete(state) || i >= maxPolls) {
return Observable.of({
...state,
status: 'completed'
});
}
const stateNext = updateDelay(state);
return Observable
.of(stateNext)
.delay(stateNext.delay)
.mergeMap(state =>
fromPromise(request(state))
.map(data => ({
...state,
data,
status: 'progress',
i: state.i + 1,
attemps: 0,
error: null
}))
.catch(error => Observable.of({
...state,
data: null,
status: 'retry',
attemps: state.attemps + 1,
error
}))
);
})
.takeWhile(({ status }) => status !== 'kill');
};
export default poll;
@siddharthpal
Copy link

Hi @malte,

Excellent stuff!!!. I needed something like this. I was not able to achieve. For reference please find the below link:
https://stackoverflow.com/questions/51864945/retrying-a-polling-service-with-n-seconds-delay

Do you think we can achieve this with your implementation? Note: We have a pool of polling resources. At one point of time we can poll multiple resources till some condition and on any poll response we have to sync UI respectively.

Thanks in advance
Siddharth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment