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CAAC,CAAC;SACnE;QAED,IAAI,CAAC,OAAO,GAAI,UAAU,CAAC;YACzB,KAAI,CAAC,OAAO,GAAG,SAAS,CAAC;YACzB,KAAI,CAAC,GAAG,EAAE,CAAC;QACb,CAAC,EAAE,KAAK,CAAmB,CAAC;IAC9B,CAAC;IACH,yBAAC;AAAD,CAAC,AA/ID,IA+IC;AAED;IAA+B,6BAAU;IAIvC,mBAAY,OAA2B;QAAvC,YACE,iBAAO,SAMR;QALO,IAAA,KAAsB,OAAO,IAAK,EAAwB,EAAxD,QAAQ,cAAA,EAAE,KAAK,WAAyC,CAAC;QACjE,KAAI,CAAC,QAAQ;YACX,OAAO,KAAK,KAAK,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC;QAClE,KAAI,CAAC,OAAO;YACV,OAAO,QAAQ,KAAK,UAAU,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,kBAAkB,CAAC,QAAQ,CAAC,CAAC;;IAC7E,CAAC;IAEM,2BAAO,GAAd,UACE,SAAoB,EACpB,QAAkB;QAElB,IAAM,SAAS,GAAG,IAAI,kBAAkB,CACtC,SAAS,EACT,QAAQ,EACR,IAAI,CAAC,QAAQ,EACb,IAAI,CAAC,OAAO,CACb,CAAC;QACF,SAAS,CAAC,KAAK,EAAE,CAAC;QAElB,OAAO,IAAI,UAAU,CAAC,UAAA,QAAQ;YAC5B,SAAS,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;YAC9B,OAAO;gBACL,SAAS,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;YAClC,CAAC,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;IACH,gBAAC;AAAD,CAAC,AAhCD,CAA+B,UAAU,GAgCxC","sourcesContent":["import { ApolloLink, Operation, FetchResult, NextLink } from '../core';\nimport { Observable, Observer, ObservableSubscription } from '../../utilities';\nimport {\n DelayFunction,\n DelayFunctionOptions,\n buildDelayFunction,\n} from './delayFunction';\nimport {\n RetryFunction,\n RetryFunctionOptions,\n buildRetryFunction,\n} from './retryFunction';\n\nexport namespace RetryLink {\n export interface Options {\n /**\n * Configuration for the delay strategy to use, or a custom delay strategy.\n */\n delay?: DelayFunctionOptions | DelayFunction;\n\n /**\n * Configuration for the retry strategy to use, or a custom retry strategy.\n */\n attempts?: RetryFunctionOptions | RetryFunction;\n }\n}\n\n/**\n * Tracking and management of operations that may be (or currently are) retried.\n */\nclass RetryableOperation<TValue = any> {\n private retryCount: number = 0;\n private values: any[] = [];\n private error: any;\n private complete = false;\n private canceled = false;\n private observers: (Observer<TValue> | null)[] = [];\n private currentSubscription: ObservableSubscription | null = null;\n private timerId: number | undefined;\n\n constructor(\n private operation: Operation,\n private nextLink: NextLink,\n private delayFor: DelayFunction,\n private retryIf: RetryFunction,\n ) {}\n\n /**\n * Register a new observer for this operation.\n *\n * If the operation has previously emitted other events, they will be\n * immediately triggered for the observer.\n */\n public subscribe(observer: Observer<TValue>) {\n if (this.canceled) {\n throw new Error(\n `Subscribing to a retryable link that was canceled is not supported`,\n );\n }\n this.observers.push(observer);\n\n // If we've already begun, catch this observer up.\n for (const value of this.values) {\n observer.next!(value);\n }\n\n if (this.complete) {\n observer.complete!();\n } else if (this.error) {\n observer.error!(this.error);\n }\n }\n\n /**\n * Remove a previously registered observer from this operation.\n *\n * If no observers remain, the operation will stop retrying, and unsubscribe\n * from its downstream link.\n */\n public unsubscribe(observer: Observer<TValue>) {\n const index = this.observers.indexOf(observer);\n if (index < 0) {\n throw new Error(\n `RetryLink BUG! Attempting to unsubscribe unknown observer!`,\n );\n }\n // Note that we are careful not to change the order of length of the array,\n // as we are often mid-iteration when calling this method.\n this.observers[index] = null;\n\n // If this is the last observer, we're done.\n if (this.observers.every(o => o === null)) {\n this.cancel();\n }\n }\n\n /**\n * Start the initial request.\n */\n public start() {\n if (this.currentSubscription) return; // Already started.\n\n this.try();\n }\n\n /**\n * Stop retrying for the operation, and cancel any in-progress requests.\n */\n public cancel() {\n if (this.currentSubscription) {\n this.currentSubscription.unsubscribe();\n }\n clearTimeout(this.timerId);\n this.timerId = undefined;\n this.currentSubscription = null;\n this.canceled = true;\n }\n\n private try() {\n this.currentSubscription = this.nextLink(this.operation).subscribe({\n next: this.onNext,\n error: this.onError,\n complete: this.onComplete,\n });\n }\n\n private onNext = (value: any) => {\n this.values.push(value);\n for (const observer of this.observers) {\n if (!observer) continue;\n observer.next!(value);\n }\n };\n\n private onComplete = () => {\n this.complete = true;\n for (const observer of this.observers) {\n if (!observer) continue;\n observer.complete!();\n }\n };\n\n private onError = async (error: any) => {\n this.retryCount += 1;\n\n // Should we retry?\n const shouldRetry = await this.retryIf(\n this.retryCount,\n this.operation,\n error,\n );\n if (shouldRetry) {\n this.scheduleRetry(this.delayFor(this.retryCount, this.operation, error));\n return;\n }\n\n this.error = error;\n for (const observer of this.observers) {\n if (!observer) continue;\n observer.error!(error);\n }\n };\n\n private scheduleRetry(delay: number) {\n if (this.timerId) {\n throw new Error(`RetryLink BUG! Encountered overlapping retries`);\n }\n\n this.timerId = (setTimeout(() => {\n this.timerId = undefined;\n this.try();\n }, delay) as any) as number;\n }\n}\n\nexport class RetryLink extends ApolloLink {\n private delayFor: DelayFunction;\n private retryIf: RetryFunction;\n\n constructor(options?: RetryLink.Options) {\n super();\n const { attempts, delay } = options || ({} as RetryLink.Options);\n this.delayFor =\n typeof delay === 'function' ? delay : buildDelayFunction(delay);\n this.retryIf =\n typeof attempts === 'function' ? attempts : buildRetryFunction(attempts);\n }\n\n public request(\n operation: Operation,\n nextLink: NextLink,\n ): Observable<FetchResult> {\n const retryable = new RetryableOperation(\n operation,\n nextLink,\n this.delayFor,\n this.retryIf,\n );\n retryable.start();\n\n return new Observable(observer => {\n retryable.subscribe(observer);\n return () => {\n retryable.unsubscribe(observer);\n };\n });\n }\n}\n"]}
\No newline at end of file