Skip to content

Commit

Permalink
Consolidate all symbols in a single file (facebook#11629)
Browse files Browse the repository at this point in the history
* Consolidate all symbols in a single file

This reduces the code duplication as we have quite a few now.

* Record sizes
  • Loading branch information
gaearon authored and Ethan-Arrowood committed Dec 8, 2017
1 parent 2d9b015 commit 77e22a2
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 181 deletions.
14 changes: 2 additions & 12 deletions packages/react-call-return/src/ReactCallReturn.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,9 @@
* @flow
*/

import type {ReactCall, ReactNodeList, ReactReturn} from 'shared/ReactTypes';
import {REACT_CALL_TYPE, REACT_RETURN_TYPE} from 'shared/ReactSymbols';

// The Symbol used to tag the special React types. If there is no native Symbol
// nor polyfill, then a plain number is used for performance.
var REACT_CALL_TYPE;
var REACT_RETURN_TYPE;
if (typeof Symbol === 'function' && Symbol.for) {
REACT_CALL_TYPE = Symbol.for('react.call');
REACT_RETURN_TYPE = Symbol.for('react.return');
} else {
REACT_CALL_TYPE = 0xeac8;
REACT_RETURN_TYPE = 0xeac9;
}
import type {ReactCall, ReactNodeList, ReactReturn} from 'shared/ReactTypes';

type CallHandler<T> = (props: T, returns: Array<mixed>) => ReactNodeList;

Expand Down
7 changes: 1 addition & 6 deletions packages/react-dom/src/server/ReactPartialRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import warning from 'fbjs/lib/warning';
import checkPropTypes from 'prop-types/checkPropTypes';
import describeComponentFrame from 'shared/describeComponentFrame';
import {ReactDebugCurrentFrame} from 'shared/ReactGlobalSharedState';
import {REACT_FRAGMENT_TYPE} from 'shared/ReactSymbols';

import {
createMarkupForCustomAttribute,
Expand All @@ -41,12 +42,6 @@ import {validateProperties as validateARIAProperties} from '../shared/ReactDOMIn
import {validateProperties as validateInputProperties} from '../shared/ReactDOMNullInputValuePropHook';
import {validateProperties as validateUnknownProperties} from '../shared/ReactDOMUnknownPropertyHook';

var REACT_FRAGMENT_TYPE =
(typeof Symbol === 'function' &&
Symbol.for &&
Symbol.for('react.fragment')) ||
0xeacb;

// Based on reading the React.Children implementation. TODO: type this somewhere?
type ReactNode = string | number | ReactElement;
type FlatReactChildren = Array<null | ReactNode>;
Expand Down
43 changes: 8 additions & 35 deletions packages/react-reconciler/src/ReactChildFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ import type {ExpirationTime} from 'react-reconciler/src/ReactFiberExpirationTime

import {enableReactFragment} from 'shared/ReactFeatureFlags';
import {Placement, Deletion} from 'shared/ReactTypeOfSideEffect';
import {
getIteratorFn,
REACT_ELEMENT_TYPE,
REACT_FRAGMENT_TYPE,
REACT_CALL_TYPE,
REACT_RETURN_TYPE,
REACT_PORTAL_TYPE,
} from 'shared/ReactSymbols';
import {
FunctionalComponent,
ClassComponent,
Expand All @@ -27,7 +35,6 @@ import emptyObject from 'fbjs/lib/emptyObject';
import invariant from 'fbjs/lib/invariant';
import warning from 'fbjs/lib/warning';

import {REACT_PORTAL_TYPE} from './ReactPortal';
import {
createWorkInProgress,
createFiberFromElement,
Expand Down Expand Up @@ -87,40 +94,6 @@ if (__DEV__) {

const isArray = Array.isArray;

const ITERATOR_SYMBOL = typeof Symbol === 'function' && Symbol.iterator;
const FAUX_ITERATOR_SYMBOL = '@@iterator'; // Before Symbol spec.

// The Symbol used to tag the ReactElement-like types. If there is no native Symbol
// nor polyfill, then a plain number is used for performance.
var REACT_ELEMENT_TYPE;
var REACT_CALL_TYPE;
var REACT_RETURN_TYPE;
var REACT_FRAGMENT_TYPE;
if (typeof Symbol === 'function' && Symbol.for) {
REACT_ELEMENT_TYPE = Symbol.for('react.element');
REACT_CALL_TYPE = Symbol.for('react.call');
REACT_RETURN_TYPE = Symbol.for('react.return');
REACT_FRAGMENT_TYPE = Symbol.for('react.fragment');
} else {
REACT_ELEMENT_TYPE = 0xeac7;
REACT_CALL_TYPE = 0xeac8;
REACT_RETURN_TYPE = 0xeac9;
REACT_FRAGMENT_TYPE = 0xeacb;
}

function getIteratorFn(maybeIterable: ?any): ?() => ?Iterator<*> {
if (maybeIterable === null || typeof maybeIterable === 'undefined') {
return null;
}
const iteratorFn =
(ITERATOR_SYMBOL && maybeIterable[ITERATOR_SYMBOL]) ||
maybeIterable[FAUX_ITERATOR_SYMBOL];
if (typeof iteratorFn === 'function') {
return iteratorFn;
}
return null;
}

function coerceRef(current: Fiber | null, element: ReactElement) {
let mixedRef = element.ref;
if (mixedRef !== null && typeof mixedRef !== 'function') {
Expand Down
8 changes: 2 additions & 6 deletions packages/react-reconciler/src/ReactPortal.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@
* @flow
*/

import type {ReactNodeList, ReactPortal} from 'shared/ReactTypes';
import {REACT_PORTAL_TYPE} from 'shared/ReactSymbols';

// The Symbol used to tag the special React types. If there is no native Symbol
// nor polyfill, then a plain number is used for performance.
export const REACT_PORTAL_TYPE =
(typeof Symbol === 'function' && Symbol.for && Symbol.for('react.portal')) ||
0xeaca;
import type {ReactNodeList, ReactPortal} from 'shared/ReactTypes';

export function createPortal(
children: ReactNodeList,
Expand Down
7 changes: 1 addition & 6 deletions packages/react/src/React.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import assign from 'object-assign';
import ReactVersion from 'shared/ReactVersion';
import {enableReactFragment} from 'shared/ReactFeatureFlags';
import {REACT_FRAGMENT_TYPE} from 'shared/ReactSymbols';

import {Component, PureComponent, AsyncComponent} from './ReactBaseClasses';
import {forEach, map, count, toArray, only} from './ReactChildren';
Expand All @@ -25,12 +26,6 @@ import {
} from './ReactElementValidator';
import ReactDebugCurrentFrame from './ReactDebugCurrentFrame';

const REACT_FRAGMENT_TYPE =
(typeof Symbol === 'function' &&
Symbol.for &&
Symbol.for('react.fragment')) ||
0xeacb;

var React = {
Children: {
map,
Expand Down
27 changes: 8 additions & 19 deletions packages/react/src/ReactChildren.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,17 @@
import emptyFunction from 'fbjs/lib/emptyFunction';
import invariant from 'fbjs/lib/invariant';
import warning from 'fbjs/lib/warning';
import {
getIteratorFn,
REACT_ELEMENT_TYPE,
REACT_CALL_TYPE,
REACT_RETURN_TYPE,
REACT_PORTAL_TYPE,
} from 'shared/ReactSymbols';

import {isValidElement, cloneAndReplaceKey} from './ReactElement';
import ReactDebugCurrentFrame from './ReactDebugCurrentFrame';

var ITERATOR_SYMBOL = typeof Symbol === 'function' && Symbol.iterator;
var FAUX_ITERATOR_SYMBOL = '@@iterator'; // Before Symbol spec.
// The Symbol used to tag the ReactElement type. If there is no native Symbol
// nor polyfill, then a plain number is used for performance.
var REACT_ELEMENT_TYPE =
(typeof Symbol === 'function' && Symbol.for && Symbol.for('react.element')) ||
0xeac7;
const REACT_CALL_TYPE =
(typeof Symbol === 'function' && Symbol.for && Symbol.for('react.call')) ||
0xeac8;
const REACT_RETURN_TYPE =
(typeof Symbol === 'function' && Symbol.for && Symbol.for('react.return')) ||
0xeac9;
const REACT_PORTAL_TYPE =
(typeof Symbol === 'function' && Symbol.for && Symbol.for('react.portal')) ||
0xeaca;
var SEPARATOR = '.';
var SUBSEPARATOR = ':';

Expand Down Expand Up @@ -170,9 +161,7 @@ function traverseAllChildrenImpl(
);
}
} else {
var iteratorFn =
(ITERATOR_SYMBOL && children[ITERATOR_SYMBOL]) ||
children[FAUX_ITERATOR_SYMBOL];
var iteratorFn = getIteratorFn(children);
if (typeof iteratorFn === 'function') {
if (__DEV__) {
// Warn about using Maps as children
Expand Down
7 changes: 1 addition & 6 deletions packages/react/src/ReactElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@
*/

import warning from 'fbjs/lib/warning';
import {REACT_ELEMENT_TYPE} from 'shared/ReactSymbols';

import ReactCurrentOwner from './ReactCurrentOwner';

var hasOwnProperty = Object.prototype.hasOwnProperty;

// The Symbol used to tag the ReactElement type. If there is no native Symbol
// nor polyfill, then a plain number is used for performance.
var REACT_ELEMENT_TYPE =
(typeof Symbol === 'function' && Symbol.for && Symbol.for('react.element')) ||
0xeac7;

var RESERVED_PROPS = {
key: true,
ref: true,
Expand Down
13 changes: 2 additions & 11 deletions packages/react/src/ReactElementValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import lowPriorityWarning from 'shared/lowPriorityWarning';
import describeComponentFrame from 'shared/describeComponentFrame';
import getComponentName from 'shared/getComponentName';
import {getIteratorFn, REACT_FRAGMENT_TYPE} from 'shared/ReactSymbols';
import checkPropTypes from 'prop-types/checkPropTypes';
import warning from 'fbjs/lib/warning';

Expand Down Expand Up @@ -54,18 +55,9 @@ if (__DEV__) {
return stack;
};

var REACT_FRAGMENT_TYPE =
(typeof Symbol === 'function' &&
Symbol.for &&
Symbol.for('react.fragment')) ||
0xeacb;

var VALID_FRAGMENT_PROPS = new Map([['children', true], ['key', true]]);
}

var ITERATOR_SYMBOL = typeof Symbol === 'function' && Symbol.iterator;
var FAUX_ITERATOR_SYMBOL = '@@iterator'; // Before Symbol spec.

function getDeclarationErrorAddendum() {
if (ReactCurrentOwner.current) {
var name = getComponentName(ReactCurrentOwner.current);
Expand Down Expand Up @@ -190,8 +182,7 @@ function validateChildKeys(node, parentType) {
node._store.validated = true;
}
} else if (node) {
var iteratorFn =
(ITERATOR_SYMBOL && node[ITERATOR_SYMBOL]) || node[FAUX_ITERATOR_SYMBOL];
var iteratorFn = getIteratorFn(node);
if (typeof iteratorFn === 'function') {
// Entry iterators used to provide implicit keys,
// but now we print a separate warning for them later.
Expand Down
42 changes: 42 additions & 0 deletions packages/shared/ReactSymbols.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

// The Symbol used to tag the ReactElement-like types. If there is no native Symbol
// nor polyfill, then a plain number is used for performance.
const hasSymbol = typeof Symbol === 'function' && Symbol.for;

export const REACT_ELEMENT_TYPE = hasSymbol
? Symbol.for('react.element')
: 0xeac7;
export const REACT_CALL_TYPE = hasSymbol ? Symbol.for('react.call') : 0xeac8;
export const REACT_RETURN_TYPE = hasSymbol
? Symbol.for('react.return')
: 0xeac9;
export const REACT_PORTAL_TYPE = hasSymbol
? Symbol.for('react.portal')
: 0xeaca;
export const REACT_FRAGMENT_TYPE = hasSymbol
? Symbol.for('react.fragment')
: 0xeacb;

const MAYBE_ITERATOR_SYMBOL = typeof Symbol === 'function' && Symbol.iterator;
const FAUX_ITERATOR_SYMBOL = '@@iterator';

export function getIteratorFn(maybeIterable: ?any): ?() => ?Iterator<*> {
if (maybeIterable === null || typeof maybeIterable === 'undefined') {
return null;
}
const maybeIterator =
(MAYBE_ITERATOR_SYMBOL && maybeIterable[MAYBE_ITERATOR_SYMBOL]) ||
maybeIterable[FAUX_ITERATOR_SYMBOL];
if (typeof maybeIterator === 'function') {
return maybeIterator;
}
return null;
}
Loading

0 comments on commit 77e22a2

Please sign in to comment.