UNPKG

16.1 kBJavaScriptView Raw
1/** @license React v0.20.4
2 * react-reconciler-reflection.development.js
3 *
4 * Copyright (c) Facebook, Inc. and its affiliates.
5 *
6 * This source code is licensed under the MIT license found in the
7 * LICENSE file in the root directory of this source tree.
8 */
9
10'use strict';
11
12
13
14if (process.env.NODE_ENV !== "production") {
15 (function() {
16'use strict';
17
18Object.defineProperty(exports, '__esModule', { value: true });
19
20var React = require('react');
21
22/**
23 * Use invariant() to assert state which your program assumes to be true.
24 *
25 * Provide sprintf-style format (only %s is supported) and arguments
26 * to provide information about what broke and what you were
27 * expecting.
28 *
29 * The invariant message will be stripped in production, but the invariant
30 * will remain to ensure logic does not differ in production.
31 */
32
33var validateFormat = function () {};
34
35{
36 validateFormat = function (format) {
37 if (format === undefined) {
38 throw new Error('invariant requires an error message argument');
39 }
40 };
41}
42
43function invariant(condition, format, a, b, c, d, e, f) {
44 validateFormat(format);
45
46 if (!condition) {
47 var error = void 0;
48 if (format === undefined) {
49 error = new Error('Minified exception occurred; use the non-minified dev environment ' + 'for the full error message and additional helpful warnings.');
50 } else {
51 var args = [a, b, c, d, e, f];
52 var argIndex = 0;
53 error = new Error(format.replace(/%s/g, function () {
54 return args[argIndex++];
55 }));
56 error.name = 'Invariant Violation';
57 }
58
59 error.framesToPop = 1; // we don't care about invariant's own frame
60 throw error;
61 }
62}
63
64// Relying on the `invariant()` implementation lets us
65// preserve the format and params in the www builds.
66
67/**
68 * Similar to invariant but only logs a warning if the condition is not met.
69 * This can be used to log issues in development environments in critical
70 * paths. Removing the logging code for production environments will keep the
71 * same logic and follow the same code paths.
72 */
73
74var warningWithoutStack = function () {};
75
76{
77 warningWithoutStack = function (condition, format) {
78 for (var _len = arguments.length, args = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
79 args[_key - 2] = arguments[_key];
80 }
81
82 if (format === undefined) {
83 throw new Error('`warningWithoutStack(condition, format, ...args)` requires a warning ' + 'message argument');
84 }
85 if (args.length > 8) {
86 // Check before the condition to catch violations early.
87 throw new Error('warningWithoutStack() currently supports at most 8 arguments.');
88 }
89 if (condition) {
90 return;
91 }
92 if (typeof console !== 'undefined') {
93 var argsWithFormat = args.map(function (item) {
94 return '' + item;
95 });
96 argsWithFormat.unshift('Warning: ' + format);
97
98 // We intentionally don't use spread (or .apply) directly because it
99 // breaks IE9: https://github.com/facebook/react/issues/13610
100 Function.prototype.apply.call(console.error, console, argsWithFormat);
101 }
102 try {
103 // --- Welcome to debugging React ---
104 // This error was thrown as a convenience so that you can use this stack
105 // to find the callsite that caused this warning to fire.
106 var argIndex = 0;
107 var message = 'Warning: ' + format.replace(/%s/g, function () {
108 return args[argIndex++];
109 });
110 throw new Error(message);
111 } catch (x) {}
112 };
113}
114
115var warningWithoutStack$1 = warningWithoutStack;
116
117/**
118 * `ReactInstanceMap` maintains a mapping from a public facing stateful
119 * instance (key) and the internal representation (value). This allows public
120 * methods to accept the user facing instance as an argument and map them back
121 * to internal methods.
122 *
123 * Note that this module is currently shared and assumed to be stateless.
124 * If this becomes an actual Map, that will break.
125 */
126
127/**
128 * This API should be called `delete` but we'd have to make sure to always
129 * transform these to strings for IE support. When this transform is fully
130 * supported we can rename it.
131 */
132
133
134function get(key) {
135 return key._reactInternalFiber;
136}
137
138var ReactSharedInternals = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
139
140// Prevent newer renderers from RTE when used with older react package versions.
141// Current owner and dispatcher used to share the same ref,
142// but PR #14548 split them out to better support the react-debug-tools package.
143if (!ReactSharedInternals.hasOwnProperty('ReactCurrentDispatcher')) {
144 ReactSharedInternals.ReactCurrentDispatcher = {
145 current: null
146 };
147}
148
149// The Symbol used to tag the ReactElement-like types. If there is no native Symbol
150// nor polyfill, then a plain number is used for performance.
151var hasSymbol = typeof Symbol === 'function' && Symbol.for;
152
153
154var REACT_PORTAL_TYPE = hasSymbol ? Symbol.for('react.portal') : 0xeaca;
155var REACT_FRAGMENT_TYPE = hasSymbol ? Symbol.for('react.fragment') : 0xeacb;
156var REACT_STRICT_MODE_TYPE = hasSymbol ? Symbol.for('react.strict_mode') : 0xeacc;
157var REACT_PROFILER_TYPE = hasSymbol ? Symbol.for('react.profiler') : 0xead2;
158var REACT_PROVIDER_TYPE = hasSymbol ? Symbol.for('react.provider') : 0xeacd;
159var REACT_CONTEXT_TYPE = hasSymbol ? Symbol.for('react.context') : 0xeace;
160
161var REACT_CONCURRENT_MODE_TYPE = hasSymbol ? Symbol.for('react.concurrent_mode') : 0xeacf;
162var REACT_FORWARD_REF_TYPE = hasSymbol ? Symbol.for('react.forward_ref') : 0xead0;
163var REACT_SUSPENSE_TYPE = hasSymbol ? Symbol.for('react.suspense') : 0xead1;
164var REACT_MEMO_TYPE = hasSymbol ? Symbol.for('react.memo') : 0xead3;
165var REACT_LAZY_TYPE = hasSymbol ? Symbol.for('react.lazy') : 0xead4;
166
167var Resolved = 1;
168
169
170function refineResolvedLazyComponent(lazyComponent) {
171 return lazyComponent._status === Resolved ? lazyComponent._result : null;
172}
173
174function getWrappedName(outerType, innerType, wrapperName) {
175 var functionName = innerType.displayName || innerType.name || '';
176 return outerType.displayName || (functionName !== '' ? wrapperName + '(' + functionName + ')' : wrapperName);
177}
178
179function getComponentName(type) {
180 if (type == null) {
181 // Host root, text node or just invalid type.
182 return null;
183 }
184 {
185 if (typeof type.tag === 'number') {
186 warningWithoutStack$1(false, 'Received an unexpected object in getComponentName(). ' + 'This is likely a bug in React. Please file an issue.');
187 }
188 }
189 if (typeof type === 'function') {
190 return type.displayName || type.name || null;
191 }
192 if (typeof type === 'string') {
193 return type;
194 }
195 switch (type) {
196 case REACT_CONCURRENT_MODE_TYPE:
197 return 'ConcurrentMode';
198 case REACT_FRAGMENT_TYPE:
199 return 'Fragment';
200 case REACT_PORTAL_TYPE:
201 return 'Portal';
202 case REACT_PROFILER_TYPE:
203 return 'Profiler';
204 case REACT_STRICT_MODE_TYPE:
205 return 'StrictMode';
206 case REACT_SUSPENSE_TYPE:
207 return 'Suspense';
208 }
209 if (typeof type === 'object') {
210 switch (type.$$typeof) {
211 case REACT_CONTEXT_TYPE:
212 return 'Context.Consumer';
213 case REACT_PROVIDER_TYPE:
214 return 'Context.Provider';
215 case REACT_FORWARD_REF_TYPE:
216 return getWrappedName(type, type.render, 'ForwardRef');
217 case REACT_MEMO_TYPE:
218 return getComponentName(type.type);
219 case REACT_LAZY_TYPE:
220 {
221 var thenable = type;
222 var resolvedThenable = refineResolvedLazyComponent(thenable);
223 if (resolvedThenable) {
224 return getComponentName(resolvedThenable);
225 }
226 }
227 }
228 }
229 return null;
230}
231
232var ClassComponent = 1;
233 // Before we know whether it is function or class
234var HostRoot = 3; // Root of a host tree. Could be nested inside another node.
235var HostPortal = 4; // A subtree. Could be an entry point to a different renderer.
236var HostComponent = 5;
237var HostText = 6;
238
239// Don't change these two values. They're used by React Dev Tools.
240var NoEffect = /* */0;
241
242
243// You can change the rest (and add more).
244var Placement = /* */2;
245
246
247
248
249
250
251
252
253
254
255// Passive & Update & Callback & Ref & Snapshot
256
257
258// Union of all host effects
259
260var ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
261
262var MOUNTING = 1;
263var MOUNTED = 2;
264var UNMOUNTED = 3;
265
266function isFiberMountedImpl(fiber) {
267 var node = fiber;
268 if (!fiber.alternate) {
269 // If there is no alternate, this might be a new tree that isn't inserted
270 // yet. If it is, then it will have a pending insertion effect on it.
271 if ((node.effectTag & Placement) !== NoEffect) {
272 return MOUNTING;
273 }
274 while (node.return) {
275 node = node.return;
276 if ((node.effectTag & Placement) !== NoEffect) {
277 return MOUNTING;
278 }
279 }
280 } else {
281 while (node.return) {
282 node = node.return;
283 }
284 }
285 if (node.tag === HostRoot) {
286 // TODO: Check if this was a nested HostRoot when used with
287 // renderContainerIntoSubtree.
288 return MOUNTED;
289 }
290 // If we didn't hit the root, that means that we're in an disconnected tree
291 // that has been unmounted.
292 return UNMOUNTED;
293}
294
295function isFiberMounted(fiber) {
296 return isFiberMountedImpl(fiber) === MOUNTED;
297}
298
299function isMounted(component) {
300 {
301 var owner = ReactCurrentOwner.current;
302 if (owner !== null && owner.tag === ClassComponent) {
303 var ownerFiber = owner;
304 var instance = ownerFiber.stateNode;
305 !instance._warnedAboutRefsInRender ? warningWithoutStack$1(false, '%s is accessing isMounted inside its render() function. ' + 'render() should be a pure function of props and state. It should ' + 'never access something that requires stale data from the previous ' + 'render, such as refs. Move this logic to componentDidMount and ' + 'componentDidUpdate instead.', getComponentName(ownerFiber.type) || 'A component') : void 0;
306 instance._warnedAboutRefsInRender = true;
307 }
308 }
309
310 var fiber = get(component);
311 if (!fiber) {
312 return false;
313 }
314 return isFiberMountedImpl(fiber) === MOUNTED;
315}
316
317function assertIsMounted(fiber) {
318 !(isFiberMountedImpl(fiber) === MOUNTED) ? invariant(false, 'Unable to find node on an unmounted component.') : void 0;
319}
320
321function findCurrentFiberUsingSlowPath(fiber) {
322 var alternate = fiber.alternate;
323 if (!alternate) {
324 // If there is no alternate, then we only need to check if it is mounted.
325 var state = isFiberMountedImpl(fiber);
326 !(state !== UNMOUNTED) ? invariant(false, 'Unable to find node on an unmounted component.') : void 0;
327 if (state === MOUNTING) {
328 return null;
329 }
330 return fiber;
331 }
332 // If we have two possible branches, we'll walk backwards up to the root
333 // to see what path the root points to. On the way we may hit one of the
334 // special cases and we'll deal with them.
335 var a = fiber;
336 var b = alternate;
337 while (true) {
338 var parentA = a.return;
339 var parentB = parentA ? parentA.alternate : null;
340 if (!parentA || !parentB) {
341 // We're at the root.
342 break;
343 }
344
345 // If both copies of the parent fiber point to the same child, we can
346 // assume that the child is current. This happens when we bailout on low
347 // priority: the bailed out fiber's child reuses the current child.
348 if (parentA.child === parentB.child) {
349 var child = parentA.child;
350 while (child) {
351 if (child === a) {
352 // We've determined that A is the current branch.
353 assertIsMounted(parentA);
354 return fiber;
355 }
356 if (child === b) {
357 // We've determined that B is the current branch.
358 assertIsMounted(parentA);
359 return alternate;
360 }
361 child = child.sibling;
362 }
363 // We should never have an alternate for any mounting node. So the only
364 // way this could possibly happen is if this was unmounted, if at all.
365 invariant(false, 'Unable to find node on an unmounted component.');
366 }
367
368 if (a.return !== b.return) {
369 // The return pointer of A and the return pointer of B point to different
370 // fibers. We assume that return pointers never criss-cross, so A must
371 // belong to the child set of A.return, and B must belong to the child
372 // set of B.return.
373 a = parentA;
374 b = parentB;
375 } else {
376 // The return pointers point to the same fiber. We'll have to use the
377 // default, slow path: scan the child sets of each parent alternate to see
378 // which child belongs to which set.
379 //
380 // Search parent A's child set
381 var didFindChild = false;
382 var _child = parentA.child;
383 while (_child) {
384 if (_child === a) {
385 didFindChild = true;
386 a = parentA;
387 b = parentB;
388 break;
389 }
390 if (_child === b) {
391 didFindChild = true;
392 b = parentA;
393 a = parentB;
394 break;
395 }
396 _child = _child.sibling;
397 }
398 if (!didFindChild) {
399 // Search parent B's child set
400 _child = parentB.child;
401 while (_child) {
402 if (_child === a) {
403 didFindChild = true;
404 a = parentB;
405 b = parentA;
406 break;
407 }
408 if (_child === b) {
409 didFindChild = true;
410 b = parentB;
411 a = parentA;
412 break;
413 }
414 _child = _child.sibling;
415 }
416 !didFindChild ? invariant(false, 'Child was not found in either parent set. This indicates a bug in React related to the return pointer. Please file an issue.') : void 0;
417 }
418 }
419
420 !(a.alternate === b) ? invariant(false, 'Return fibers should always be each others\' alternates. This error is likely caused by a bug in React. Please file an issue.') : void 0;
421 }
422 // If the root is not a host container, we're in a disconnected tree. I.e.
423 // unmounted.
424 !(a.tag === HostRoot) ? invariant(false, 'Unable to find node on an unmounted component.') : void 0;
425 if (a.stateNode.current === a) {
426 // We've determined that A is the current branch.
427 return fiber;
428 }
429 // Otherwise B has to be current branch.
430 return alternate;
431}
432
433function findCurrentHostFiber(parent) {
434 var currentParent = findCurrentFiberUsingSlowPath(parent);
435 if (!currentParent) {
436 return null;
437 }
438
439 // Next we'll drill down this component to find the first HostComponent/Text.
440 var node = currentParent;
441 while (true) {
442 if (node.tag === HostComponent || node.tag === HostText) {
443 return node;
444 } else if (node.child) {
445 node.child.return = node;
446 node = node.child;
447 continue;
448 }
449 if (node === currentParent) {
450 return null;
451 }
452 while (!node.sibling) {
453 if (!node.return || node.return === currentParent) {
454 return null;
455 }
456 node = node.return;
457 }
458 node.sibling.return = node.return;
459 node = node.sibling;
460 }
461 // Flow needs the return null here, but ESLint complains about it.
462 // eslint-disable-next-line no-unreachable
463 return null;
464}
465
466function findCurrentHostFiberWithNoPortals(parent) {
467 var currentParent = findCurrentFiberUsingSlowPath(parent);
468 if (!currentParent) {
469 return null;
470 }
471
472 // Next we'll drill down this component to find the first HostComponent/Text.
473 var node = currentParent;
474 while (true) {
475 if (node.tag === HostComponent || node.tag === HostText) {
476 return node;
477 } else if (node.child && node.tag !== HostPortal) {
478 node.child.return = node;
479 node = node.child;
480 continue;
481 }
482 if (node === currentParent) {
483 return null;
484 }
485 while (!node.sibling) {
486 if (!node.return || node.return === currentParent) {
487 return null;
488 }
489 node = node.return;
490 }
491 node.sibling.return = node.return;
492 node = node.sibling;
493 }
494 // Flow needs the return null here, but ESLint complains about it.
495 // eslint-disable-next-line no-unreachable
496 return null;
497}
498
499exports.isFiberMounted = isFiberMounted;
500exports.isMounted = isMounted;
501exports.findCurrentFiberUsingSlowPath = findCurrentFiberUsingSlowPath;
502exports.findCurrentHostFiber = findCurrentHostFiber;
503exports.findCurrentHostFiberWithNoPortals = findCurrentHostFiberWithNoPortals;
504 })();
505}