(actImplementation: ReactAct)
| 19 | } |
| 20 | |
| 21 | function withGlobalActEnvironment(actImplementation: ReactAct) { |
| 22 | return (callback: Parameters<ReactAct>[0]) => { |
| 23 | const previousActEnvironment = getIsReactActEnvironment(); |
| 24 | setIsReactActEnvironment(true); |
| 25 | |
| 26 | try { |
| 27 | // The return value of `act` is always a thenable. |
| 28 | let callbackNeedsToBeAwaited = false; |
| 29 | const actResult = actImplementation(() => { |
| 30 | const result = callback(); |
| 31 | // @ts-expect-error TS is too strict here |
| 32 | if (result !== null && typeof result === 'object' && typeof result.then === 'function') { |
| 33 | callbackNeedsToBeAwaited = true; |
| 34 | } |
| 35 | return result; |
| 36 | }); |
| 37 | |
| 38 | if (callbackNeedsToBeAwaited) { |
| 39 | const thenable = actResult; |
| 40 | return { |
| 41 | then: (resolve: (value: never) => never, reject: (value: never) => never) => { |
| 42 | // eslint-disable-next-line promise/catch-or-return, promise/prefer-await-to-then |
| 43 | thenable.then( |
| 44 | // eslint-disable-next-line promise/always-return |
| 45 | (returnValue) => { |
| 46 | setIsReactActEnvironment(previousActEnvironment); |
| 47 | resolve(returnValue as never); |
| 48 | }, |
| 49 | (error) => { |
| 50 | setIsReactActEnvironment(previousActEnvironment); |
| 51 | reject(error as never); |
| 52 | }, |
| 53 | ); |
| 54 | }, |
| 55 | }; |
| 56 | } else { |
| 57 | setIsReactActEnvironment(previousActEnvironment); |
| 58 | return actResult; |
| 59 | } |
| 60 | } catch (error) { |
| 61 | // Can't be a `finally {}` block since we don't know if we have to immediately restore IS_REACT_ACT_ENVIRONMENT |
| 62 | // or if we have to await the callback first. |
| 63 | setIsReactActEnvironment(previousActEnvironment); |
| 64 | throw error; |
| 65 | } |
| 66 | }; |
| 67 | } |
| 68 | |
| 69 | const _act = withGlobalActEnvironment(reactAct) as ReactAct; |
| 70 |
no test coverage detected
searching dependent graphs…