microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/promise.ts
234lines · 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 | | |
ab0238b7RedMickey4 years ago | 4 | import { CancellationTokenSource, Disposable } from "vscode"; |
f338085detatanova4 years ago | 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 | | |
ab0238b7RedMickey4 years ago | 71 | public static waitUntil<T>( |
| 72 | condition: () => Promise<T | null> | T | null, | |
| 73 | interval: number = 1000, | |
| 74 | timeout?: number, | |
| 75 | ): Promise<T | null> { | |
| 76 | return new Promise(async resolve => { | |
| 77 | let rejectTimeout: NodeJS.Timeout | undefined; | |
| 78 | let сheckInterval: NodeJS.Timeout | undefined; | |
| 79 | | |
| 80 | if (timeout) { | |
| 81 | rejectTimeout = setTimeout(() => { | |
| 82 | cleanup(); | |
| 83 | resolve(null); | |
| 84 | }, timeout); | |
| 85 | } | |
| 86 | | |
| 87 | const cleanup = () => { | |
| 88 | if (rejectTimeout) { | |
| 89 | clearTimeout(rejectTimeout); | |
| 90 | } | |
| 91 | if (сheckInterval) { | |
| 92 | clearInterval(сheckInterval); | |
| 93 | } | |
| 94 | }; | |
| 95 | | |
| 96 | const tryToResolve = async (): Promise<boolean> => { | |
| 97 | const result = await condition(); | |
| 98 | if (result) { | |
| 99 | cleanup(); | |
| 100 | resolve(result); | |
| 101 | } | |
| 102 | return !!result; | |
| 103 | }; | |
| 104 | | |
| 105 | const resolved = await tryToResolve(); | |
| 106 | if (resolved) { | |
| 107 | return; | |
| 108 | } | |
| 109 | | |
| 110 | сheckInterval = setInterval(async () => { | |
| 111 | await tryToResolve(); | |
| 112 | }, interval); | |
| 113 | }); | |
| 114 | } | |
| 115 | | |
4bb0956eRedMickey5 years ago | 116 | public static promiseCacheDecorator<T>( |
| 117 | func: (...args: any[]) => Promise<T>, | |
| 118 | context: Record<string, any> | null = null, | |
| 119 | ): (...args: any[]) => Promise<T> { | |
| 120 | let promise: Promise<T>; | |
| 121 | return (...args: any[]): Promise<T> => { | |
0d77292aJiglioNero4 years ago | 122 | if (!promise) { |
4bb0956eRedMickey5 years ago | 123 | promise = func.apply(context, args); |
| 124 | } | |
0d77292aJiglioNero4 years ago | 125 | return promise; |
4bb0956eRedMickey5 years ago | 126 | }; |
| 127 | } | |
| 128 | | |
0d77292aJiglioNero4 years ago | 129 | private static async retryAsyncIteration<T>( |
34472878RedMickey5 years ago | 130 | operation: () => Promise<T>, |
| 131 | condition: (result: T) => boolean | Promise<boolean>, | |
| 132 | maxRetries: number, | |
| 133 | iteration: number, | |
| 134 | delay: number, | |
| 135 | failure: string, | |
f338085detatanova4 years ago | 136 | cancellationTokenSource?: CancellationTokenSource, |
34472878RedMickey5 years ago | 137 | ): Promise<T> { |
0d77292aJiglioNero4 years ago | 138 | const result = await operation(); |
| 139 | const conditionResult = await condition(result); | |
| 140 | if (conditionResult) { | |
| 141 | return result; | |
| 142 | } | |
b7451aefRedMickey6 years ago | 143 | |
0d77292aJiglioNero4 years ago | 144 | if ( |
| 145 | iteration < maxRetries && | |
| 146 | !(cancellationTokenSource && cancellationTokenSource.token.isCancellationRequested) | |
| 147 | ) { | |
| 148 | await PromiseUtil.delay(delay); | |
| 149 | return this.retryAsyncIteration( | |
| 150 | operation, | |
| 151 | condition, | |
| 152 | maxRetries, | |
| 153 | iteration + 1, | |
| 154 | delay, | |
| 155 | failure, | |
| 156 | cancellationTokenSource, | |
| 157 | ); | |
| 158 | } | |
b7451aefRedMickey6 years ago | 159 | |
0d77292aJiglioNero4 years ago | 160 | throw new Error(failure); |
b7451aefRedMickey6 years ago | 161 | } |
34472878RedMickey5 years ago | 162 | } |
ab0238b7RedMickey4 years ago | 163 | |
| 164 | export class Delayer<T> implements Disposable { | |
| 165 | private timeout: any; | |
| 166 | private completionPromise: Promise<any> | null; | |
| 167 | private doResolve: ((value?: any | Promise<any>) => void) | null; | |
| 168 | private doReject: ((err: any) => void) | null; | |
| 169 | private task: { (): T | Promise<T> } | null; | |
| 170 | | |
| 171 | constructor() { | |
| 172 | this.timeout = null; | |
| 173 | this.completionPromise = null; | |
| 174 | this.doResolve = null; | |
| 175 | this.doReject = null; | |
| 176 | this.task = null; | |
| 177 | } | |
| 178 | | |
| 179 | public runWihtDelay(task: { (): T | Promise<T> }, delay: number): Promise<T> { | |
| 180 | this.task = task; | |
| 181 | this.cancelTimeout(); | |
| 182 | | |
| 183 | if (!this.completionPromise) { | |
| 184 | this.completionPromise = new Promise((resolve, reject) => { | |
| 185 | this.doResolve = resolve; | |
| 186 | this.doReject = reject; | |
| 187 | }).then(() => { | |
| 188 | this.completionPromise = null; | |
| 189 | this.doResolve = null; | |
| 190 | if (this.task) { | |
| 191 | const task = this.task; | |
| 192 | this.task = null; | |
| 193 | return task(); | |
| 194 | } | |
| 195 | return undefined; | |
| 196 | }); | |
| 197 | } | |
| 198 | | |
| 199 | this.timeout = setTimeout(() => { | |
| 200 | this.timeout = null; | |
| 201 | if (this.doResolve) { | |
| 202 | this.doResolve(null); | |
| 203 | } | |
| 204 | }, delay); | |
| 205 | | |
| 206 | return this.completionPromise; | |
| 207 | } | |
| 208 | | |
| 209 | public isRunning(): boolean { | |
| 210 | return this.timeout !== null; | |
| 211 | } | |
| 212 | | |
| 213 | public cancel(): void { | |
| 214 | this.cancelTimeout(); | |
| 215 | | |
| 216 | if (this.completionPromise) { | |
| 217 | if (this.doReject) { | |
| 218 | this.doReject(new Error("Canceled")); | |
| 219 | } | |
| 220 | this.completionPromise = null; | |
| 221 | } | |
| 222 | } | |
| 223 | | |
| 224 | public dispose(): void { | |
| 225 | this.cancel(); | |
| 226 | } | |
| 227 | | |
| 228 | private cancelTimeout(): void { | |
| 229 | if (this.timeout !== null) { | |
| 230 | clearTimeout(this.timeout); | |
| 231 | this.timeout = null; | |
| 232 | } | |
| 233 | } | |
| 234 | } |