@fluidframework/test-utils 0.54.2 → 0.56.0-49831

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,47 +5,51 @@
5
5
 
6
6
  export const defaultTimeoutDurationMs = 250;
7
7
 
8
- export interface TimeoutWithError{
8
+ export interface TimeoutWithError {
9
9
  durationMs?: number;
10
10
  reject?: true;
11
11
  errorMsg?: string;
12
- }
13
- export interface TimeoutWithValue<T = void>{
12
+ }
13
+ export interface TimeoutWithValue<T = void> {
14
14
  durationMs?: number;
15
15
  reject: false;
16
16
  value: T;
17
- }
17
+ }
18
18
 
19
- // eslint-disable-next-line prefer-arrow/prefer-arrow-functions
20
19
  export async function timeoutAwait<T = void>(
21
20
  promise: PromiseLike<T>,
22
21
  timeoutOptions: TimeoutWithError | TimeoutWithValue<T> = {},
23
22
  ) {
24
- return Promise.race([promise, timeoutPromise<T>(()=>{}, timeoutOptions)]);
23
+ return Promise.race([promise, timeoutPromise<T>(() => { }, timeoutOptions)]);
25
24
  }
26
25
 
27
- export async function timeoutPromise<T = void>(
26
+ export async function timeoutPromise<T = void>(
28
27
  executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void,
29
28
  timeoutOptions: TimeoutWithError | TimeoutWithValue<T> = {},
30
29
  ): Promise<T> {
30
+ const timeout =
31
+ timeoutOptions.durationMs !== undefined
32
+ && Number.isFinite(timeoutOptions.durationMs)
33
+ && timeoutOptions.durationMs > 0
34
+ ? timeoutOptions.durationMs : defaultTimeoutDurationMs;
31
35
  // create the timeout error outside the async task, so it's callstack includes
32
36
  // the original call site, this makes it easier to debug
33
37
  const err = timeoutOptions.reject === false
34
38
  ? undefined
35
- : new Error(timeoutOptions.errorMsg ?? "Timeout");
36
- return new Promise<T>((res,rej)=>{
37
- const timeout = setTimeout(
38
- ()=>timeoutOptions.reject === false ? res(timeoutOptions.value) : rej(err),
39
- timeoutOptions.durationMs ?? defaultTimeoutDurationMs);
39
+ : new Error(`${timeoutOptions.errorMsg ?? "Timed out"}(${timeout}ms)`);
40
+ return new Promise<T>((resolve,reject)=>{
41
+ const timer = setTimeout(
42
+ ()=>timeoutOptions.reject === false ? resolve(timeoutOptions.value) : reject(err),
43
+ timeout);
40
44
 
41
45
  executor(
42
46
  (value) => {
43
- clearTimeout(timeout);
44
- res(value);
47
+ clearTimeout(timer);
48
+ resolve(value);
45
49
  },
46
50
  (reason) => {
47
- clearTimeout(timeout);
48
- rej(reason);
51
+ clearTimeout(timer);
52
+ reject(reason);
49
53
  });
50
54
  });
51
55
  }