(actImplementation)
| 34 | } |
| 35 | |
| 36 | function withGlobalActEnvironment(actImplementation) { |
| 37 | return callback => { |
| 38 | const previousActEnvironment = getIsReactActEnvironment() |
| 39 | setIsReactActEnvironment(true) |
| 40 | try { |
| 41 | // The return value of `act` is always a thenable. |
| 42 | let callbackNeedsToBeAwaited = false |
| 43 | const actResult = actImplementation(() => { |
| 44 | const result = callback() |
| 45 | if ( |
| 46 | result !== null && |
| 47 | typeof result === 'object' && |
| 48 | typeof result.then === 'function' |
| 49 | ) { |
| 50 | callbackNeedsToBeAwaited = true |
| 51 | } |
| 52 | return result |
| 53 | }) |
| 54 | if (callbackNeedsToBeAwaited) { |
| 55 | const thenable = actResult |
| 56 | return { |
| 57 | then: (resolve, reject) => { |
| 58 | thenable.then( |
| 59 | returnValue => { |
| 60 | setIsReactActEnvironment(previousActEnvironment) |
| 61 | resolve(returnValue) |
| 62 | }, |
| 63 | error => { |
| 64 | setIsReactActEnvironment(previousActEnvironment) |
| 65 | reject(error) |
| 66 | }, |
| 67 | ) |
| 68 | }, |
| 69 | } |
| 70 | } else { |
| 71 | setIsReactActEnvironment(previousActEnvironment) |
| 72 | return actResult |
| 73 | } |
| 74 | } catch (error) { |
| 75 | // Can't be a `finally {}` block since we don't know if we have to immediately restore IS_REACT_ACT_ENVIRONMENT |
| 76 | // or if we have to await the callback first. |
| 77 | setIsReactActEnvironment(previousActEnvironment) |
| 78 | throw error |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | const act = withGlobalActEnvironment(reactAct) |
| 84 |
no test coverage detected
searching dependent graphs…