Repository: facebook/react
File: packages/react-dom/src/client/ReactDOMDefaultTransitionIndicator.js (JavaScript)

Code before:
          return new Promise(resolve => (pendingResolve = resolve));
        },
        focusReset: 'manual',
        scroll: 'manual',
      });
    }
  }

  function handleNavigateComplete() {
    const wasRunning = pendingResolve !== null;
    if (pendingResolve !== null) {
      // If this was not our navigation completing, we were probably cancelled.
      // We'll start a new one below.
      pendingResolve();
      pendingResolve = null;
    }
    if (!isCancelled) {
      // Some other navigation completed but we should still be running.
      // Start another fake one to keep the loading indicator going.
      if (wasRunning) {
        // Restart sync to make it not janky if it was already running
        startFakeNavigation();
      } else {
        // Since it hasn't started yet, we want to delay it up to a bit.
        // There needs to be an async gap to work around https://issues.chromium.org/u/1/issues/419746417.
        const fakeNavigationDelay = Math.max(
          0,
          defaultTransitionIndicatorDelay - (performance.now() - scheduledAt),
        );
        setTimeout(startFakeNavigation, fakeNavigationDelay);
      }
    }
  }

  // $FlowFixMe
  navigation.addEventListener('navigate', handleNavigate);
  // $FlowFixMe
  navigation.addEventListener('navigatesuccess', handleNavigateComplete);
  // $FlowFixMe
  navigation.addEventListener('navigateerror', handleNavigateComplete);

  function startFakeNavigation() {
    if (isCancelled) {
      // We already stopped this Transition.
      return;
    }
    if (navigation.transition) {
      // There is an on-going Navigation already happening. Let's wait for it to
      // finish before starting our fake one.
      return;
    }

Review comment (performance):
I usually don't trust `Math.max` because it can be patched it is not automatically optimized like syntax can be. This is not a hot path but I try to avoid it as a pattern so that people don't copy paste it elsewhere.

Code after:
  }

  function handleNavigateComplete() {
    if (pendingResolve !== null) {
      // If this was not our navigation completing, we were probably cancelled.
      // We'll start a new one below.
      pendingResolve();
      pendingResolve = null;
    }
    if (!isCancelled) {
      // Some other navigation completed but we should still be running.
      // Start another fake one to keep the loading indicator going.
      // There needs to be an async gap to work around https://issues.chromium.org/u/1/issues/419746417.
      setTimeout(startFakeNavigation, 20);
    }
  }

  // $FlowFixMe
  navigation.addEventListener('navigate', handleNavigate);
  // $FlowFixMe
  navigation.addEventListener('navigatesuccess', handleNavigateComplete);
  // $FlowFixMe
  navigation.addEventListener('navigateerror', handleNavigateComplete);

  function startFakeNavigation() {
    if (isCancelled) {
      // We already stopped this Transition.
      return;
    }
    if (navigation.transition) {
      // There is an on-going Navigation already happening. Let's wait for it to
      // finish before starting our fake one.
      return;
    }
    // Trigger a fake navigation to the same page
    const currentEntry = navigation.currentEntry;
    if (currentEntry && currentEntry.url != null) {
      navigation.navigate(currentEntry.url, {
        state: currentEntry.getState(),
        info: 'react-transition', // indicator to routers to ignore this navigation
        history: 'replace',
      });
    }
  }

  // Delay the start a bit in case this is a fast Transition.
  setTimeout(startFakeNavigation, 100);

  return function () {
    isCancelled = true;
    // $FlowFixMe