| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | import type {Fiber} from './ReactInternalTypes'; |
| 11 | |
| 12 | import ReactSharedInternals from 'shared/ReactSharedInternals'; |
| 13 | import {getOwnerStackByFiberInDev} from './ReactFiberComponentStack'; |
| 14 | import {getComponentNameFromOwner} from 'react-reconciler/src/getComponentNameFromFiber'; |
| 15 | |
| 16 | export let current: Fiber | null = null; |
| 17 | export let isRendering: boolean = false; |
| 18 | |
| 19 | export function getCurrentFiberOwnerNameInDevOrNull(): string | null { |
| 20 | if (__DEV__) { |
| 21 | if (current === null) { |
| 22 | return null; |
| 23 | } |
| 24 | const owner = current._debugOwner; |
| 25 | if (owner != null) { |
| 26 | return getComponentNameFromOwner(owner); |
| 27 | } |
| 28 | } |
| 29 | return null; |
| 30 | } |
| 31 | |
| 32 | function getCurrentFiberStackInDev(): string { |
| 33 | if (__DEV__) { |
| 34 | if (current === null) { |
| 35 | return ''; |
| 36 | } |
| 37 | // Safe because if current fiber exists, we are reconciling, |
| 38 | // and it is guaranteed to be the work-in-progress version. |
| 39 | // TODO: The above comment is not actually true. We might be |
| 40 | // in a commit phase or preemptive set state callback. |
| 41 | return getOwnerStackByFiberInDev(current); |
| 42 | } |
| 43 | return ''; |
| 44 | } |
| 45 | |
| 46 | export function runWithFiberInDEV<A0, A1, A2, A3, A4, T>( |
| 47 | fiber: null | Fiber, |
| 48 | callback: (A0, A1, A2, A3, A4) => T, |
| 49 | arg0: A0, |
| 50 | arg1: A1, |
| 51 | arg2: A2, |
| 52 | arg3: A3, |
| 53 | arg4: A4, |
| 54 | ): T { |
| 55 | if (__DEV__) { |
| 56 | const previousFiber = current; |
| 57 | setCurrentFiber(fiber); |
| 58 | try { |
| 59 | if (fiber !== null && fiber._debugTask) { |
| 60 | return fiber._debugTask.run( |
| 61 | callback.bind(null, arg0, arg1, arg2, arg3, arg4), |
| 62 | ); |
| 63 | } |
| 64 | return callback(arg0, arg1, arg2, arg3, arg4); |
| 65 | } finally { |
| 66 | setCurrentFiber(previousFiber); |
| 67 | } |
| 68 | } |
| 69 | // These errors should never make it into a build so we don't need to encode them in codes.json |
| 70 | // eslint-disable-next-line react-internal/prod-error-codes |
| 71 | throw new Error( |
| 72 | 'runWithFiberInDEV should never be called in production. This is a bug in React.', |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | export function resetCurrentFiber() { |
| 77 | if (__DEV__) { |
| 78 | ReactSharedInternals.getCurrentStack = null; |
| 79 | isRendering = false; |
| 80 | } |
| 81 | current = null; |
| 82 | } |
| 83 | |
| 84 | export function setCurrentFiber(fiber: Fiber | null) { |
| 85 | if (__DEV__) { |
| 86 | ReactSharedInternals.getCurrentStack = |
| 87 | fiber === null ? null : getCurrentFiberStackInDev; |
| 88 | isRendering = false; |
| 89 | } |
| 90 | current = fiber; |
| 91 | } |
| 92 | |
| 93 | export function setIsRendering(rendering: boolean) { |
| 94 | if (__DEV__) { |
| 95 | isRendering = rendering; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | export function getIsRendering(): void | boolean { |
| 100 | if (__DEV__) { |
| 101 | return isRendering; |
| 102 | } |
| 103 | } |