microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/services/remoteConfigHelper.ts
37lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import { CancellationTokenSource } from "vscode"; |
| 5 | import { PromiseUtil } from "../../common/node/promise"; |
| 6 | import { Request } from "../../common/node/request"; |
| 7 | |
| 8 | // eslint-disable-next-line @typescript-eslint/no-empty-interface |
| 9 | export interface IConfig {} |
| 10 | |
| 11 | export async function downloadConfig<T extends IConfig | IConfig[]>( |
| 12 | endpointURL: string, |
| 13 | ): Promise<T> { |
| 14 | const resString = await Request.request(endpointURL, false, true); |
| 15 | return JSON.parse(resString); |
| 16 | } |
| 17 | |
| 18 | export async function retryDownloadConfig<T extends IConfig | IConfig[]>( |
| 19 | endpointURL: string, |
| 20 | cancellationTokenSource: CancellationTokenSource, |
| 21 | retryCount = 60, |
| 22 | ): Promise<T> { |
| 23 | return PromiseUtil.retryAsync( |
| 24 | async () => { |
| 25 | try { |
| 26 | return (await downloadConfig<T>(endpointURL)) as T; |
| 27 | } catch (err) { |
| 28 | throw err; |
| 29 | } |
| 30 | }, |
| 31 | (config: any) => !!config, |
| 32 | retryCount, |
| 33 | 2000, |
| 34 | `Could not download remote config from ${endpointURL}`, |
| 35 | cancellationTokenSource, |
| 36 | ); |
| 37 | } |
| 38 | |