microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/promise.ts
117lines · modeblame
2f10b3adunknown10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
f338085detatanova4 years ago | 4 | import { CancellationTokenSource } from "vscode"; |
| 5 | | |
2f10b3adunknown10 years ago | 6 | /** |
| 7 | * Utilities for working with promises. | |
| 8 | */ | |
34ec0375Daniel10 years ago | 9 | export class PromiseUtil { |
0d77292aJiglioNero4 years ago | 10 | public static async forEach<T>( |
| 11 | sources: T[], | |
| 12 | promiseGenerator: (source: T) => Promise<void>, | |
| 13 | ): Promise<void> { | |
| 14 | await Promise.all( | |
34472878RedMickey5 years ago | 15 | sources.map(source => { |
52f3873ddigeff10 years ago | 16 | return promiseGenerator(source); |
34472878RedMickey5 years ago | 17 | }), |
0d77292aJiglioNero4 years ago | 18 | ); |
52f3873ddigeff10 years ago | 19 | } |
2f10b3adunknown10 years ago | 20 | /** |
| 21 | * Retries an operation a given number of times. For each retry, a condition is checked. | |
| 22 | * If the condition is not satisfied after the maximum number of retries, and error is thrown. | |
| 23 | * Otherwise, the result of the operation is returned once the condition is satisfied. | |
| 24 | * | |
| 25 | * @param operation - the function to execute. | |
| 26 | * @param condition - the condition to check between iterations. | |
| 27 | * @param maxRetries - the maximum number of retries. | |
| 28 | * @param delay - time between iterations, in milliseconds. | |
| 29 | * @param failure - error description. | |
| 30 | */ | |
0d77292aJiglioNero4 years ago | 31 | public static retryAsync<T>( |
34472878RedMickey5 years ago | 32 | operation: () => Promise<T>, |
| 33 | condition: (result: T) => boolean | Promise<boolean>, | |
| 34 | maxRetries: number, | |
| 35 | delay: number, | |
| 36 | failure: string, | |
f338085detatanova4 years ago | 37 | cancellationTokenSource?: CancellationTokenSource, |
34472878RedMickey5 years ago | 38 | ): Promise<T> { |
f338085detatanova4 years ago | 39 | return this.retryAsyncIteration( |
| 40 | operation, | |
| 41 | condition, | |
| 42 | maxRetries, | |
| 43 | 0, | |
| 44 | delay, | |
| 45 | failure, | |
| 46 | cancellationTokenSource, | |
| 47 | ); | |
2f10b3adunknown10 years ago | 48 | } |
| 49 | | |
0d77292aJiglioNero4 years ago | 50 | public static async reduce<T>( |
34472878RedMickey5 years ago | 51 | sources: T[] | Promise<T[]>, |
| 52 | generateAsyncOperation: (value: T) => Promise<void>, | |
| 53 | ): Promise<void> { | |
0d77292aJiglioNero4 years ago | 54 | let arraySources: T[]; |
ce5e88eeYuri Skorokhodov5 years ago | 55 | if (sources instanceof Promise) { |
0d77292aJiglioNero4 years ago | 56 | arraySources = await sources; |
ce5e88eeYuri Skorokhodov5 years ago | 57 | } else { |
0d77292aJiglioNero4 years ago | 58 | arraySources = sources; |
ce5e88eeYuri Skorokhodov5 years ago | 59 | } |
0d77292aJiglioNero4 years ago | 60 | |
| 61 | return arraySources.reduce(async (previousReduction: Promise<void>, newSource: T) => { | |
| 62 | await previousReduction; | |
| 63 | return generateAsyncOperation(newSource); | |
| 64 | }, Promise.resolve()); | |
bc6696cbdigeff10 years ago | 65 | } |
| 66 | | |
259c018fYuri Skorokhodov5 years ago | 67 | public static async delay(duration: number): Promise<void> { |
d9c9ddcbRedMickey6 years ago | 68 | return new Promise<void>(resolve => setTimeout(resolve, duration)); |
| 69 | } | |
| 70 | | |
4bb0956eRedMickey5 years ago | 71 | public static promiseCacheDecorator<T>( |
| 72 | func: (...args: any[]) => Promise<T>, | |
| 73 | context: Record<string, any> | null = null, | |
| 74 | ): (...args: any[]) => Promise<T> { | |
| 75 | let promise: Promise<T>; | |
| 76 | return (...args: any[]): Promise<T> => { | |
0d77292aJiglioNero4 years ago | 77 | if (!promise) { |
4bb0956eRedMickey5 years ago | 78 | promise = func.apply(context, args); |
| 79 | } | |
0d77292aJiglioNero4 years ago | 80 | return promise; |
4bb0956eRedMickey5 years ago | 81 | }; |
| 82 | } | |
| 83 | | |
0d77292aJiglioNero4 years ago | 84 | private static async retryAsyncIteration<T>( |
34472878RedMickey5 years ago | 85 | operation: () => Promise<T>, |
| 86 | condition: (result: T) => boolean | Promise<boolean>, | |
| 87 | maxRetries: number, | |
| 88 | iteration: number, | |
| 89 | delay: number, | |
| 90 | failure: string, | |
f338085detatanova4 years ago | 91 | cancellationTokenSource?: CancellationTokenSource, |
34472878RedMickey5 years ago | 92 | ): Promise<T> { |
0d77292aJiglioNero4 years ago | 93 | const result = await operation(); |
| 94 | const conditionResult = await condition(result); | |
| 95 | if (conditionResult) { | |
| 96 | return result; | |
| 97 | } | |
b7451aefRedMickey6 years ago | 98 | |
0d77292aJiglioNero4 years ago | 99 | if ( |
| 100 | iteration < maxRetries && | |
| 101 | !(cancellationTokenSource && cancellationTokenSource.token.isCancellationRequested) | |
| 102 | ) { | |
| 103 | await PromiseUtil.delay(delay); | |
| 104 | return this.retryAsyncIteration( | |
| 105 | operation, | |
| 106 | condition, | |
| 107 | maxRetries, | |
| 108 | iteration + 1, | |
| 109 | delay, | |
| 110 | failure, | |
| 111 | cancellationTokenSource, | |
| 112 | ); | |
| 113 | } | |
b7451aefRedMickey6 years ago | 114 | |
0d77292aJiglioNero4 years ago | 115 | throw new Error(failure); |
b7451aefRedMickey6 years ago | 116 | } |
34472878RedMickey5 years ago | 117 | } |