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 {getStackByFiberInDevAndProd} from './ReactFiberComponentStack';
    
  14. import getComponentNameFromFiber from 'react-reconciler/src/getComponentNameFromFiber';
    
  15. 
    
  16. const ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;
    
  17. 
    
  18. export let current: Fiber | null = null;
    
  19. export let isRendering: boolean = false;
    
  20. 
    
  21. export function getCurrentFiberOwnerNameInDevOrNull(): string | null {
    
  22.   if (__DEV__) {
    
  23.     if (current === null) {
    
  24.       return null;
    
  25.     }
    
  26.     const owner = current._debugOwner;
    
  27.     if (owner !== null && typeof owner !== 'undefined') {
    
  28.       return getComponentNameFromFiber(owner);
    
  29.     }
    
  30.   }
    
  31.   return null;
    
  32. }
    
  33. 
    
  34. function getCurrentFiberStackInDev(): string {
    
  35.   if (__DEV__) {
    
  36.     if (current === null) {
    
  37.       return '';
    
  38.     }
    
  39.     // Safe because if current fiber exists, we are reconciling,
    
  40.     // and it is guaranteed to be the work-in-progress version.
    
  41.     return getStackByFiberInDevAndProd(current);
    
  42.   }
    
  43.   return '';
    
  44. }
    
  45. 
    
  46. export function resetCurrentFiber() {
    
  47.   if (__DEV__) {
    
  48.     ReactDebugCurrentFrame.getCurrentStack = null;
    
  49.     current = null;
    
  50.     isRendering = false;
    
  51.   }
    
  52. }
    
  53. 
    
  54. export function setCurrentFiber(fiber: Fiber | null) {
    
  55.   if (__DEV__) {
    
  56.     ReactDebugCurrentFrame.getCurrentStack =
    
  57.       fiber === null ? null : getCurrentFiberStackInDev;
    
  58.     current = fiber;
    
  59.     isRendering = false;
    
  60.   }
    
  61. }
    
  62. 
    
  63. export function getCurrentFiber(): Fiber | null {
    
  64.   if (__DEV__) {
    
  65.     return current;
    
  66.   }
    
  67.   return null;
    
  68. }
    
  69. 
    
  70. export function setIsRendering(rendering: boolean) {
    
  71.   if (__DEV__) {
    
  72.     isRendering = rendering;
    
  73.   }
    
  74. }
    
  75. 
    
  76. export function getIsRendering(): void | boolean {
    
  77.   if (__DEV__) {
    
  78.     return isRendering;
    
  79.   }
    
  80. }