Skip to content

Commit

Permalink
Reduces the size of ReactDOMSharedIntenrals object representation to …
Browse files Browse the repository at this point in the history
…save on bundle size
  • Loading branch information
gnoff committed Apr 5, 2024
1 parent b1c3da8 commit bd85e05
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ export function setCurrentUpdatePriority(
// is much longer. I hope this is consistent enough to rely on across builds
IntentionallyUnusedArgument?: empty,
): void {
ReactDOMSharedInternals.up = newPriority;
ReactDOMSharedInternals.p /* currentUpdatePriority */ = newPriority;
}

export function getCurrentUpdatePriority(): EventPriority {
return ReactDOMSharedInternals.up;
return ReactDOMSharedInternals.p; /* currentUpdatePriority */
}

export function resolveUpdatePriority(): EventPriority {
const updatePriority = ReactDOMSharedInternals.up;
const updatePriority = ReactDOMSharedInternals.p; /* currentUpdatePriority */
if (updatePriority !== NoEventPriority) {
return updatePriority;
}
Expand Down
7 changes: 3 additions & 4 deletions packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ import {validateLinkPropsForStyleResource} from '../shared/ReactDOMResourceValid
import escapeSelectorAttributeValueInsideDoubleQuotes from './escapeSelectorAttributeValueInsideDoubleQuotes';

import ReactDOMSharedInternals from 'shared/ReactDOMSharedInternals';
const ReactDOMCurrentDispatcher =
ReactDOMSharedInternals.ReactDOMCurrentDispatcher;

export type Type = string;
export type Props = {
Expand Down Expand Up @@ -1922,8 +1920,9 @@ function getDocumentFromRoot(root: HoistableRoot): Document {
return root.ownerDocument || root;
}

const previousDispatcher = ReactDOMCurrentDispatcher.current;
ReactDOMCurrentDispatcher.current = {
const previousDispatcher =
ReactDOMSharedInternals.d; /* ReactDOMCurrentDispatcher */
ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */ = {
prefetchDNS,
preconnect,
preload,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ import {
} from 'react-server/src/ReactFlightServer';

import ReactDOMSharedInternals from 'shared/ReactDOMSharedInternals';
const ReactDOMCurrentDispatcher =
ReactDOMSharedInternals.ReactDOMCurrentDispatcher;

const previousDispatcher = ReactDOMCurrentDispatcher.current;
ReactDOMCurrentDispatcher.current = {
const previousDispatcher =
ReactDOMSharedInternals.d; /* ReactDOMCurrentDispatcher */
ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */ = {
prefetchDNS,
preconnect,
preload,
Expand Down
7 changes: 3 additions & 4 deletions packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,10 @@ import {getValueDescriptorExpectingObjectForWarning} from '../shared/ReactDOMRes
import {NotPending} from '../shared/ReactDOMFormActions';

import ReactDOMSharedInternals from 'shared/ReactDOMSharedInternals';
const ReactDOMCurrentDispatcher =
ReactDOMSharedInternals.ReactDOMCurrentDispatcher;

const previousDispatcher = ReactDOMCurrentDispatcher.current;
ReactDOMCurrentDispatcher.current = {
const previousDispatcher =
ReactDOMSharedInternals.d; /* ReactDOMCurrentDispatcher */
ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */ = {
prefetchDNS,
preconnect,
preload,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@
import type {HintCode, HintModel} from '../server/ReactFlightServerConfigDOM';

import ReactDOMSharedInternals from 'shared/ReactDOMSharedInternals';
const ReactDOMCurrentDispatcher =
ReactDOMSharedInternals.ReactDOMCurrentDispatcher;

import {getCrossOriginString} from './crossOriginStrings';

export function dispatchHint<Code: HintCode>(
code: Code,
model: HintModel<Code>,
): void {
const dispatcher = ReactDOMCurrentDispatcher.current;
const dispatcher = ReactDOMSharedInternals.d; /* ReactDOMCurrentDispatcher */
switch (code) {
case 'D': {
const refined = refineModel(code, model);
Expand Down Expand Up @@ -117,19 +115,21 @@ export function preinitModuleForSSR(
nonce: ?string,
crossOrigin: ?string,
) {
ReactDOMCurrentDispatcher.current.preinitModuleScript(href, {
crossOrigin: getCrossOriginString(crossOrigin),
nonce,
});
ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */
.preinitModuleScript(href, {
crossOrigin: getCrossOriginString(crossOrigin),
nonce,
});
}

export function preinitScriptForSSR(
href: string,
nonce: ?string,
crossOrigin: ?string,
) {
ReactDOMCurrentDispatcher.current.preinitScript(href, {
crossOrigin: getCrossOriginString(crossOrigin),
nonce,
});
ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */
.preinitScript(href, {
crossOrigin: getCrossOriginString(crossOrigin),
nonce,
});
}
8 changes: 4 additions & 4 deletions packages/react-dom/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ export function createRoot(
options?: CreateRootOptions,
): RootType {
if (__DEV__) {
Internals.usingClientEntryPoint = true;
Internals.c /* usingClientEntryPoint */ = true;
}
try {
return createRootImpl(container, options);
} finally {
if (__DEV__) {
Internals.usingClientEntryPoint = false;
Internals.c /* usingClientEntryPoint */ = false;
}
}
}
Expand All @@ -44,13 +44,13 @@ export function hydrateRoot(
options?: HydrateRootOptions,
): RootType {
if (__DEV__) {
Internals.usingClientEntryPoint = true;
Internals.c /* usingClientEntryPoint */ = true;
}
try {
return hydrateRootImpl(container, children, options);
} finally {
if (__DEV__) {
Internals.usingClientEntryPoint = false;
Internals.c /* usingClientEntryPoint */ = false;
}
}
}
20 changes: 8 additions & 12 deletions packages/react-dom/src/ReactDOMSharedInternals.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ import type {HostDispatcher} from './shared/ReactDOMTypes';
import {NoEventPriority} from 'react-reconciler/src/ReactEventPriorities';

type InternalsType = {
usingClientEntryPoint: boolean,
Events: [any, any, any, any, any, any],
ReactDOMCurrentDispatcher: {
current: HostDispatcher,
},
c /* usingClientEntryPoint */: boolean,
E /* Events */: [any, any, any, any, any, any],
d /* ReactDOMCurrentDispatcher */: HostDispatcher,
findDOMNode:
| null
| ((
componentOrElement: React$Component<any, any>,
) => null | Element | Text),
up /* currentUpdatePriority */: EventPriority,
p /* currentUpdatePriority */: EventPriority,
};

function noop() {}
Expand All @@ -39,13 +37,11 @@ const DefaultDispatcher: HostDispatcher = {
};

const Internals: InternalsType = {
usingClientEntryPoint: false,
Events: (null: any),
ReactDOMCurrentDispatcher: {
current: DefaultDispatcher,
},
c /* usingClientEntryPoint */: false,
E /* Events */: (null: any),
d /* ReactDOMCurrentDispatcher */: DefaultDispatcher,
findDOMNode: null,
up /* currentUpdatePriority */: NoEventPriority,
p /* currentUpdatePriority */: NoEventPriority,
};

export default Internals;
6 changes: 3 additions & 3 deletions packages/react-dom/src/client/ReactDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function createRoot(
options?: CreateRootOptions,
): RootType {
if (__DEV__) {
if (!Internals.usingClientEntryPoint && !__UMD__) {
if (!Internals.c /* usingClientEntryPoint */ && !__UMD__) {
console.error(
'You are importing createRoot from "react-dom" which is not supported. ' +
'You should instead import it from "react-dom/client".',
Expand All @@ -111,7 +111,7 @@ function hydrateRoot(
options?: HydrateRootOptions,
): RootType {
if (__DEV__) {
if (!Internals.usingClientEntryPoint && !__UMD__) {
if (!Internals.c /* usingClientEntryPoint */ && !__UMD__) {
console.error(
'You are importing hydrateRoot from "react-dom" which is not supported. ' +
'You should instead import it from "react-dom/client".',
Expand Down Expand Up @@ -173,7 +173,7 @@ export {

// Keep in sync with ReactTestUtils.js.
// This is an array for better minification.
Internals.Events = [
Internals.E /* Events */ = [
getInstanceFromNode,
getNodeFromInstance,
getFiberCurrentPropsFromNode,
Expand Down
Loading

0 comments on commit bd85e05

Please sign in to comment.