mirror of
https://github.com/facebook/react.git
synced 2026-02-26 07:55:55 +00:00
* New context API Introduces a declarative context API that propagates updates even when shouldComponentUpdate returns false. * Fuzz tester for context * Use ReactElement for provider and consumer children * Unify more branches in createFiberFromElement * Compare context values using Object.is Same semantics as PureComponent/shallowEqual. * Add support for Provider and Consumer to server-side renderer * Store providers on global stack Rather than using a linked list stored on the context type. The global stack can be reset in case of an interruption or error, whereas with the linked list implementation, you'd need to keep track of every context type. * Put new context API behind a feature flag We'll enable this in www only for now. * Store nearest provider on context object * Handle reentrancy in server renderer Context stack should be per server renderer instance. * Bailout of consumer updates using bitmask The context type defines an optional function that compares two context values, returning a bitfield. A consumer may specify the bits it needs for rendering. If a provider's context changes, and the consumer's bits do not intersect with the changed bits, we can skip the consumer. This is similar to how selectors are used in Redux but fast enough to do while scanning the tree. The only user code involved is the function that computes the changed bits. But that's only called once per provider update, not for every consumer. * Store current value and changed bits on context object There are fewer providers than consumers, so better to do this work at the provider. * Use maximum of 31 bits for bitmask This is the largest integer size in V8 on 32-bit systems. Warn in development if too large a number is used. * ProviderComponent -> ContextProvider, ConsumerComponent -> ContextConsumer * Inline Object.is * Warn if multiple renderers concurrently render the same context provider Let's see if we can get away with not supporting this for now. If it turns out that it's needed, we can fall back to backtracking the fiber return path. * Nits that came up during review
52 lines
1.6 KiB
JavaScript
52 lines
1.6 KiB
JavaScript
/**
|
|
* 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;
|
|
export const REACT_STRICT_MODE_TYPE = hasSymbol
|
|
? Symbol.for('react.strict_mode')
|
|
: 0xeacc;
|
|
export const REACT_PROVIDER_TYPE = hasSymbol
|
|
? Symbol.for('react.provider')
|
|
: 0xeacd;
|
|
export const REACT_CONTEXT_TYPE = hasSymbol
|
|
? Symbol.for('react.context')
|
|
: 0xeace;
|
|
|
|
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;
|
|
}
|