All files / src/implementation backoff.ts

72.55% Statements 37/51
41.67% Branches 10/24
88.89% Functions 8/9
72% Lines 36/50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132                                                            5x                   28x   28x 28x 28x     29x   28x     15x 15x 15x         29x 28x 28x       15x 15x     15x 14x 14x   1x 1x 1x 1x                             28x     1x     1x 1x     1x 1x 1x   1x 1x             28x 28x       28x                   5x 1x    
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
/**
 * @fileoverview Provides a method for running a function with exponential
 * backoff.
 */
type id = (p1: boolean) => void;
 
export { id };
 
/**
 * @param f May be invoked
 *     before the function returns.
 * @param callback Get all the arguments passed to the function
 *     passed to f, including the initial boolean.
 */
export function start(
  f: (
    p1: (success: boolean, ...rest: any[]) => void,
    canceled: boolean
  ) => void,
  callback: Function,
  timeout: number
): id {
  // TODO(andysoto): make this code cleaner (probably refactor into an actual
  // type instead of a bunch of functions with state shared in the closure)
  let waitSeconds = 1;
  // Would type this as "number" but that doesn't work for Node so ¯\_(ツ)_/¯
  let timeoutId: any = null;
  let hitTimeout = false;
  let cancelState = 0;
 
  function canceled() {
    return cancelState === 2;
  }
  let triggeredCallback = false;
 
  function triggerCallback() {
    Eif (!triggeredCallback) {
      triggeredCallback = true;
      callback.apply(null, arguments);
    }
  }
 
  function callWithDelay(millis: number): void {
    timeoutId = setTimeout(function() {
      timeoutId = null;
      f(handler, canceled());
    }, millis);
  }
 
  function handler(success: boolean, ...var_args: any[]): void {
    Iif (triggeredCallback) {
      return;
    }
    if (success) {
      triggerCallback.apply(null, arguments);
      return;
    }
    let mustStop = canceled() || hitTimeout;
    Eif (mustStop) {
      triggerCallback.apply(null, arguments);
      return;
    }
    if (waitSeconds < 64) {
      /* TODO(andysoto): don't back off so quickly if we know we're offline. */
      waitSeconds *= 2;
    }
    let waitMillis;
    if (cancelState === 1) {
      cancelState = 2;
      waitMillis = 0;
    } else {
      waitMillis = (waitSeconds + Math.random()) * 1000;
    }
    callWithDelay(waitMillis);
  }
  let stopped = false;
 
  function stop(wasTimeout: boolean): void {
    Iif (stopped) {
      return;
    }
    stopped = true;
    Iif (triggeredCallback) {
      return;
    }
    Eif (timeoutId !== null) {
      Eif (!wasTimeout) {
        cancelState = 2;
      }
      clearTimeout(timeoutId);
      callWithDelay(0);
    } else {
      if (!wasTimeout) {
        cancelState = 1;
      }
    }
  }
  callWithDelay(0);
  setTimeout(function() {
    hitTimeout = true;
    stop(true);
  }, timeout);
  return stop;
}
 
/**
 * Stops the retry loop from repeating.
 * If the function is currently "in between" retries, it is invoked immediately
 * with the second parameter as "true". Otherwise, it will be invoked once more
 * after the current invocation finishes iff the current invocation would have
 * triggered another retry.
 */
export function stop(id: id) {
  id(false);
}