All files / app/assets/javascripts/lib/utils simple_poll.js

66.66% Statements 6/9
44.44% Branches 4/9
75% Functions 3/4
62.5% Lines 5/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18      21x   21x 21x 21x             21x      
import { differenceInMilliseconds } from '~/lib/utils/datetime_utility';
 
export default (fn, { interval = 2000, timeout = 60000 } = {}) => {
  const startTime = Date.now();
 
  return new Promise((resolve, reject) => {
    const stop = (arg) => (arg instanceof Error ? reject(arg) : resolve(arg));
    const next = () => {
      if (timeout === 0 || differenceInMilliseconds(startTime) < timeout) {
        setTimeout(fn.bind(null, next, stop), interval);
      } else {
        reject(new Error('SIMPLE_POLL_TIMEOUT'));
      }
    };
    fn(next, stop);
  });
};