Files
react/packages/react-dom/src/shared/ReactDOMFlushSync.js
T
Josh Story 9007fdc8f1 [DOM] Shrink ReactDOMSharedInternals source representation (#28771)
Stacked on #28751 

ReactDOMSharedInternals uses properties of considerable length to model
mutuable state. These properties are not mangled during minification and
contribute a not insigificant amount to the uncompressed bundle size and
to a lesser degree compressed bundle size.

This change rewrites the DOMInternals in a way that shortens property
names so we can have smaller builds.
It also treats the entire object as a mutable container rather than
having different mutable sub objects.

The same treatment should be given to ReactSharedInternals
2024-04-08 13:39:39 -07:00

68 lines
2.3 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {BatchConfig} from 'react/src/ReactCurrentBatchConfig';
import {disableLegacyMode} from 'shared/ReactFeatureFlags';
import {DiscreteEventPriority} from 'react-reconciler/src/ReactEventPriorities';
import ReactSharedInternals from 'shared/ReactSharedInternals';
const ReactCurrentBatchConfig: BatchConfig =
ReactSharedInternals.ReactCurrentBatchConfig;
import ReactDOMSharedInternals from 'shared/ReactDOMSharedInternals';
declare function flushSyncImpl<R>(fn: () => R): R;
declare function flushSyncImpl(void): void;
function flushSyncImpl<R>(fn: (() => R) | void): R | void {
const previousTransition = ReactCurrentBatchConfig.transition;
const previousUpdatePriority =
ReactDOMSharedInternals.p; /* ReactDOMCurrentUpdatePriority */
try {
ReactCurrentBatchConfig.transition = null;
ReactDOMSharedInternals.p /* ReactDOMCurrentUpdatePriority */ =
DiscreteEventPriority;
if (fn) {
return fn();
} else {
return undefined;
}
} finally {
ReactCurrentBatchConfig.transition = previousTransition;
ReactDOMSharedInternals.p /* ReactDOMCurrentUpdatePriority */ =
previousUpdatePriority;
const wasInRender =
ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */
.flushSyncWork();
if (__DEV__) {
if (wasInRender) {
console.error(
'flushSync was called from inside a lifecycle method. React cannot ' +
'flush when React is already rendering. Consider moving this call to ' +
'a scheduler task or micro task.',
);
}
}
}
}
declare function flushSyncErrorInBuildsThatSupportLegacyMode<R>(fn: () => R): R;
declare function flushSyncErrorInBuildsThatSupportLegacyMode(void): void;
function flushSyncErrorInBuildsThatSupportLegacyMode() {
// eslint-disable-next-line react-internal/prod-error-codes
throw new Error(
'Expected this build of React to not support legacy mode but it does. This is a bug in React.',
);
}
export const flushSync: typeof flushSyncImpl = disableLegacyMode
? flushSyncImpl
: flushSyncErrorInBuildsThatSupportLegacyMode;