import initAsync from './retry-async' ; import initSync from './retry-sync' ; import initCss from './retry-css' ; import { RetryStatistics , retryCollector } from './collector' ; import { maxRetryCountProp , onRetryProp , onSuccessProp , onFailProp , domainProp , win } from './constants' ; import { Domain , DomainMap , prepareDomainMap } from './url' ; import { identity , noop } from './util' ; export type RetryFunction = ( currentUrl : string , originalUrl : string , retryCollector : null | RetryStatistics ) => string | null ; export type SuccessFunction = ( currentPath : string ) => void ; export type FailFunction = ( currentPath : string ) => void ; export interface AssetsRetryOptions { [ maxRetryCountProp ] : number ; [ onRetryProp ] ? : RetryFunction ; [ onSuccessProp ] ? : SuccessFunction ; [ onFailProp ] ? : FailFunction ; [ domainProp ] : Domain ; } export interface InnerAssetsRetryOptions { [ maxRetryCountProp ] : number ; [ onRetryProp ] : RetryFunction ; [ onSuccessProp ] : SuccessFunction ; [ onFailProp ] : FailFunction ; [ domainProp ] : DomainMap ; } export default function init ( opts = { } as any ) { try { if ( typeof opts [ domainProp ] !== 'object' ) { throw new Error ( 'opts.domain cannot be non-object.' ) ; } const optionList = [ maxRetryCountProp , onRetryProp , onSuccessProp , onFailProp , domainProp ] ; const invalidOptions = Object . keys ( opts ) . filter ( key => optionList . indexOf ( key ) === - 1 ) ; if ( invalidOptions . length > 0 ) { throw new Error ( 'option name: ' + invalidOptions . join ( ', ' ) + ' is not valid.' ) ; } const innerOpts = { [ maxRetryCountProp ] : opts [ maxRetryCountProp ] || 3 , [ onRetryProp ] : opts [ onRetryProp ] || identity , [ onSuccessProp ] : opts [ onSuccessProp ] || noop , [ onFailProp ] : opts [ onFailProp ] || noop , [ domainProp ] : prepareDomainMap ( opts [ domainProp ] ) } ; initAsync ( innerOpts ) ; initSync ( innerOpts ) ; if ( __RETRY_IMAGE__ ) { initCss ( innerOpts ) ; } return retryCollector ; } catch ( e ) { win . console && console . error ( '[assetsRetry] error captured' , e ) ; } }