retryStrategy.ts 827 B

12345678910111213141516171819202122
  1. import { WebRequest } from './webRequest';
  2. /**
  3. * Class used to define a retry strategy when error happens while loading assets
  4. */
  5. export class RetryStrategy {
  6. /**
  7. * Function used to defines an exponential back off strategy
  8. * @param maxRetries defines the maximum number of retries (3 by default)
  9. * @param baseInterval defines the interval between retries
  10. * @returns the strategy function to use
  11. */
  12. public static ExponentialBackoff(maxRetries = 3, baseInterval = 500) {
  13. return (url: string, request: WebRequest, retryIndex: number): number => {
  14. if (request.status !== 0 || retryIndex >= maxRetries || url.indexOf("file:") !== -1) {
  15. return -1;
  16. }
  17. return Math.pow(2, retryIndex) * baseInterval;
  18. };
  19. }
  20. }