File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change 1
1
import type { CancellationToken , Disposable } from 'vscode' ;
2
+ import { pauseOnCancelOrTimeout } from './cancellation' ;
2
3
3
4
export type PromiseOrValue < T > = Promise < T > | T ;
4
5
@@ -255,6 +256,28 @@ export function isPromise<T>(obj: PromiseLike<T> | T): obj is Promise<T> {
255
256
// return new Map(await Promise.all(promises));
256
257
// }
257
258
259
+ export type TimedResult < T > = { readonly value : T ; readonly duration : number } ;
260
+ export async function timed < T > ( promise : Promise < T > ) : Promise < TimedResult < T > > {
261
+ const start = Date . now ( ) ;
262
+ const value = await promise ;
263
+ return { value : value , duration : Date . now ( ) - start } ;
264
+ }
265
+
266
+ export async function timedWithSlowThreshold < T > (
267
+ promise : Promise < T > ,
268
+ slowThreshold : { timeout : number ; onSlow : ( duration : number ) => void } ,
269
+ ) : Promise < TimedResult < T > > {
270
+ const start = Date . now ( ) ;
271
+
272
+ const result = await pauseOnCancelOrTimeout ( promise , undefined , slowThreshold . timeout ) ;
273
+
274
+ const value = result . paused
275
+ ? await result . value . finally ( ( ) => slowThreshold . onSlow ( Date . now ( ) - start ) )
276
+ : result . value ;
277
+
278
+ return { value : value , duration : Date . now ( ) - start } ;
279
+ }
280
+
258
281
export function wait ( ms : number ) : Promise < void > {
259
282
return new Promise < void > ( resolve => setTimeout ( resolve , ms ) ) ;
260
283
}
You can’t perform that action at this time.
0 commit comments