[d565449] | 1 | /**
|
---|
| 2 | * @license React
|
---|
| 3 | * react-refresh-runtime.development.js
|
---|
| 4 | *
|
---|
| 5 | * Copyright (c) Facebook, Inc. and its affiliates.
|
---|
| 6 | *
|
---|
| 7 | * This source code is licensed under the MIT license found in the
|
---|
| 8 | * LICENSE file in the root directory of this source tree.
|
---|
| 9 | */
|
---|
| 10 |
|
---|
| 11 | 'use strict';
|
---|
| 12 |
|
---|
| 13 | if (process.env.NODE_ENV !== "production") {
|
---|
| 14 | (function() {
|
---|
| 15 | 'use strict';
|
---|
| 16 |
|
---|
| 17 | // ATTENTION
|
---|
| 18 | var REACT_FORWARD_REF_TYPE = Symbol.for('react.forward_ref');
|
---|
| 19 | var REACT_MEMO_TYPE = Symbol.for('react.memo');
|
---|
| 20 |
|
---|
| 21 | var PossiblyWeakMap = typeof WeakMap === 'function' ? WeakMap : Map; // We never remove these associations.
|
---|
| 22 | // It's OK to reference families, but use WeakMap/Set for types.
|
---|
| 23 |
|
---|
| 24 | var allFamiliesByID = new Map();
|
---|
| 25 | var allFamiliesByType = new PossiblyWeakMap();
|
---|
| 26 | var allSignaturesByType = new PossiblyWeakMap(); // This WeakMap is read by React, so we only put families
|
---|
| 27 | // that have actually been edited here. This keeps checks fast.
|
---|
| 28 | // $FlowIssue
|
---|
| 29 |
|
---|
| 30 | var updatedFamiliesByType = new PossiblyWeakMap(); // This is cleared on every performReactRefresh() call.
|
---|
| 31 | // It is an array of [Family, NextType] tuples.
|
---|
| 32 |
|
---|
| 33 | var pendingUpdates = []; // This is injected by the renderer via DevTools global hook.
|
---|
| 34 |
|
---|
| 35 | var helpersByRendererID = new Map();
|
---|
| 36 | var helpersByRoot = new Map(); // We keep track of mounted roots so we can schedule updates.
|
---|
| 37 |
|
---|
| 38 | var mountedRoots = new Set(); // If a root captures an error, we remember it so we can retry on edit.
|
---|
| 39 |
|
---|
| 40 | var failedRoots = new Set(); // In environments that support WeakMap, we also remember the last element for every root.
|
---|
| 41 | // It needs to be weak because we do this even for roots that failed to mount.
|
---|
| 42 | // If there is no WeakMap, we won't attempt to do retrying.
|
---|
| 43 | // $FlowIssue
|
---|
| 44 |
|
---|
| 45 | var rootElements = // $FlowIssue
|
---|
| 46 | typeof WeakMap === 'function' ? new WeakMap() : null;
|
---|
| 47 | var isPerformingRefresh = false;
|
---|
| 48 |
|
---|
| 49 | function computeFullKey(signature) {
|
---|
| 50 | if (signature.fullKey !== null) {
|
---|
| 51 | return signature.fullKey;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | var fullKey = signature.ownKey;
|
---|
| 55 | var hooks;
|
---|
| 56 |
|
---|
| 57 | try {
|
---|
| 58 | hooks = signature.getCustomHooks();
|
---|
| 59 | } catch (err) {
|
---|
| 60 | // This can happen in an edge case, e.g. if expression like Foo.useSomething
|
---|
| 61 | // depends on Foo which is lazily initialized during rendering.
|
---|
| 62 | // In that case just assume we'll have to remount.
|
---|
| 63 | signature.forceReset = true;
|
---|
| 64 | signature.fullKey = fullKey;
|
---|
| 65 | return fullKey;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | for (var i = 0; i < hooks.length; i++) {
|
---|
| 69 | var hook = hooks[i];
|
---|
| 70 |
|
---|
| 71 | if (typeof hook !== 'function') {
|
---|
| 72 | // Something's wrong. Assume we need to remount.
|
---|
| 73 | signature.forceReset = true;
|
---|
| 74 | signature.fullKey = fullKey;
|
---|
| 75 | return fullKey;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | var nestedHookSignature = allSignaturesByType.get(hook);
|
---|
| 79 |
|
---|
| 80 | if (nestedHookSignature === undefined) {
|
---|
| 81 | // No signature means Hook wasn't in the source code, e.g. in a library.
|
---|
| 82 | // We'll skip it because we can assume it won't change during this session.
|
---|
| 83 | continue;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | var nestedHookKey = computeFullKey(nestedHookSignature);
|
---|
| 87 |
|
---|
| 88 | if (nestedHookSignature.forceReset) {
|
---|
| 89 | signature.forceReset = true;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | fullKey += '\n---\n' + nestedHookKey;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | signature.fullKey = fullKey;
|
---|
| 96 | return fullKey;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | function haveEqualSignatures(prevType, nextType) {
|
---|
| 100 | var prevSignature = allSignaturesByType.get(prevType);
|
---|
| 101 | var nextSignature = allSignaturesByType.get(nextType);
|
---|
| 102 |
|
---|
| 103 | if (prevSignature === undefined && nextSignature === undefined) {
|
---|
| 104 | return true;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | if (prevSignature === undefined || nextSignature === undefined) {
|
---|
| 108 | return false;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | if (computeFullKey(prevSignature) !== computeFullKey(nextSignature)) {
|
---|
| 112 | return false;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | if (nextSignature.forceReset) {
|
---|
| 116 | return false;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | return true;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | function isReactClass(type) {
|
---|
| 123 | return type.prototype && type.prototype.isReactComponent;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | function canPreserveStateBetween(prevType, nextType) {
|
---|
| 127 | if (isReactClass(prevType) || isReactClass(nextType)) {
|
---|
| 128 | return false;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | if (haveEqualSignatures(prevType, nextType)) {
|
---|
| 132 | return true;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | return false;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | function resolveFamily(type) {
|
---|
| 139 | // Only check updated types to keep lookups fast.
|
---|
| 140 | return updatedFamiliesByType.get(type);
|
---|
| 141 | } // If we didn't care about IE11, we could use new Map/Set(iterable).
|
---|
| 142 |
|
---|
| 143 |
|
---|
| 144 | function cloneMap(map) {
|
---|
| 145 | var clone = new Map();
|
---|
| 146 | map.forEach(function (value, key) {
|
---|
| 147 | clone.set(key, value);
|
---|
| 148 | });
|
---|
| 149 | return clone;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | function cloneSet(set) {
|
---|
| 153 | var clone = new Set();
|
---|
| 154 | set.forEach(function (value) {
|
---|
| 155 | clone.add(value);
|
---|
| 156 | });
|
---|
| 157 | return clone;
|
---|
| 158 | } // This is a safety mechanism to protect against rogue getters and Proxies.
|
---|
| 159 |
|
---|
| 160 |
|
---|
| 161 | function getProperty(object, property) {
|
---|
| 162 | try {
|
---|
| 163 | return object[property];
|
---|
| 164 | } catch (err) {
|
---|
| 165 | // Intentionally ignore.
|
---|
| 166 | return undefined;
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | function performReactRefresh() {
|
---|
| 171 |
|
---|
| 172 | if (pendingUpdates.length === 0) {
|
---|
| 173 | return null;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | if (isPerformingRefresh) {
|
---|
| 177 | return null;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | isPerformingRefresh = true;
|
---|
| 181 |
|
---|
| 182 | try {
|
---|
| 183 | var staleFamilies = new Set();
|
---|
| 184 | var updatedFamilies = new Set();
|
---|
| 185 | var updates = pendingUpdates;
|
---|
| 186 | pendingUpdates = [];
|
---|
| 187 | updates.forEach(function (_ref) {
|
---|
| 188 | var family = _ref[0],
|
---|
| 189 | nextType = _ref[1];
|
---|
| 190 | // Now that we got a real edit, we can create associations
|
---|
| 191 | // that will be read by the React reconciler.
|
---|
| 192 | var prevType = family.current;
|
---|
| 193 | updatedFamiliesByType.set(prevType, family);
|
---|
| 194 | updatedFamiliesByType.set(nextType, family);
|
---|
| 195 | family.current = nextType; // Determine whether this should be a re-render or a re-mount.
|
---|
| 196 |
|
---|
| 197 | if (canPreserveStateBetween(prevType, nextType)) {
|
---|
| 198 | updatedFamilies.add(family);
|
---|
| 199 | } else {
|
---|
| 200 | staleFamilies.add(family);
|
---|
| 201 | }
|
---|
| 202 | }); // TODO: rename these fields to something more meaningful.
|
---|
| 203 |
|
---|
| 204 | var update = {
|
---|
| 205 | updatedFamilies: updatedFamilies,
|
---|
| 206 | // Families that will re-render preserving state
|
---|
| 207 | staleFamilies: staleFamilies // Families that will be remounted
|
---|
| 208 |
|
---|
| 209 | };
|
---|
| 210 | helpersByRendererID.forEach(function (helpers) {
|
---|
| 211 | // Even if there are no roots, set the handler on first update.
|
---|
| 212 | // This ensures that if *new* roots are mounted, they'll use the resolve handler.
|
---|
| 213 | helpers.setRefreshHandler(resolveFamily);
|
---|
| 214 | });
|
---|
| 215 | var didError = false;
|
---|
| 216 | var firstError = null; // We snapshot maps and sets that are mutated during commits.
|
---|
| 217 | // If we don't do this, there is a risk they will be mutated while
|
---|
| 218 | // we iterate over them. For example, trying to recover a failed root
|
---|
| 219 | // may cause another root to be added to the failed list -- an infinite loop.
|
---|
| 220 |
|
---|
| 221 | var failedRootsSnapshot = cloneSet(failedRoots);
|
---|
| 222 | var mountedRootsSnapshot = cloneSet(mountedRoots);
|
---|
| 223 | var helpersByRootSnapshot = cloneMap(helpersByRoot);
|
---|
| 224 | failedRootsSnapshot.forEach(function (root) {
|
---|
| 225 | var helpers = helpersByRootSnapshot.get(root);
|
---|
| 226 |
|
---|
| 227 | if (helpers === undefined) {
|
---|
| 228 | throw new Error('Could not find helpers for a root. This is a bug in React Refresh.');
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | if (!failedRoots.has(root)) {// No longer failed.
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | if (rootElements === null) {
|
---|
| 235 | return;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | if (!rootElements.has(root)) {
|
---|
| 239 | return;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | var element = rootElements.get(root);
|
---|
| 243 |
|
---|
| 244 | try {
|
---|
| 245 | helpers.scheduleRoot(root, element);
|
---|
| 246 | } catch (err) {
|
---|
| 247 | if (!didError) {
|
---|
| 248 | didError = true;
|
---|
| 249 | firstError = err;
|
---|
| 250 | } // Keep trying other roots.
|
---|
| 251 |
|
---|
| 252 | }
|
---|
| 253 | });
|
---|
| 254 | mountedRootsSnapshot.forEach(function (root) {
|
---|
| 255 | var helpers = helpersByRootSnapshot.get(root);
|
---|
| 256 |
|
---|
| 257 | if (helpers === undefined) {
|
---|
| 258 | throw new Error('Could not find helpers for a root. This is a bug in React Refresh.');
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | if (!mountedRoots.has(root)) {// No longer mounted.
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | try {
|
---|
| 265 | helpers.scheduleRefresh(root, update);
|
---|
| 266 | } catch (err) {
|
---|
| 267 | if (!didError) {
|
---|
| 268 | didError = true;
|
---|
| 269 | firstError = err;
|
---|
| 270 | } // Keep trying other roots.
|
---|
| 271 |
|
---|
| 272 | }
|
---|
| 273 | });
|
---|
| 274 |
|
---|
| 275 | if (didError) {
|
---|
| 276 | throw firstError;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | return update;
|
---|
| 280 | } finally {
|
---|
| 281 | isPerformingRefresh = false;
|
---|
| 282 | }
|
---|
| 283 | }
|
---|
| 284 | function register(type, id) {
|
---|
| 285 | {
|
---|
| 286 | if (type === null) {
|
---|
| 287 | return;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | if (typeof type !== 'function' && typeof type !== 'object') {
|
---|
| 291 | return;
|
---|
| 292 | } // This can happen in an edge case, e.g. if we register
|
---|
| 293 | // return value of a HOC but it returns a cached component.
|
---|
| 294 | // Ignore anything but the first registration for each type.
|
---|
| 295 |
|
---|
| 296 |
|
---|
| 297 | if (allFamiliesByType.has(type)) {
|
---|
| 298 | return;
|
---|
| 299 | } // Create family or remember to update it.
|
---|
| 300 | // None of this bookkeeping affects reconciliation
|
---|
| 301 | // until the first performReactRefresh() call above.
|
---|
| 302 |
|
---|
| 303 |
|
---|
| 304 | var family = allFamiliesByID.get(id);
|
---|
| 305 |
|
---|
| 306 | if (family === undefined) {
|
---|
| 307 | family = {
|
---|
| 308 | current: type
|
---|
| 309 | };
|
---|
| 310 | allFamiliesByID.set(id, family);
|
---|
| 311 | } else {
|
---|
| 312 | pendingUpdates.push([family, type]);
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | allFamiliesByType.set(type, family); // Visit inner types because we might not have registered them.
|
---|
| 316 |
|
---|
| 317 | if (typeof type === 'object' && type !== null) {
|
---|
| 318 | switch (getProperty(type, '$$typeof')) {
|
---|
| 319 | case REACT_FORWARD_REF_TYPE:
|
---|
| 320 | register(type.render, id + '$render');
|
---|
| 321 | break;
|
---|
| 322 |
|
---|
| 323 | case REACT_MEMO_TYPE:
|
---|
| 324 | register(type.type, id + '$type');
|
---|
| 325 | break;
|
---|
| 326 | }
|
---|
| 327 | }
|
---|
| 328 | }
|
---|
| 329 | }
|
---|
| 330 | function setSignature(type, key) {
|
---|
| 331 | var forceReset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
---|
| 332 | var getCustomHooks = arguments.length > 3 ? arguments[3] : undefined;
|
---|
| 333 |
|
---|
| 334 | {
|
---|
| 335 | if (!allSignaturesByType.has(type)) {
|
---|
| 336 | allSignaturesByType.set(type, {
|
---|
| 337 | forceReset: forceReset,
|
---|
| 338 | ownKey: key,
|
---|
| 339 | fullKey: null,
|
---|
| 340 | getCustomHooks: getCustomHooks || function () {
|
---|
| 341 | return [];
|
---|
| 342 | }
|
---|
| 343 | });
|
---|
| 344 | } // Visit inner types because we might not have signed them.
|
---|
| 345 |
|
---|
| 346 |
|
---|
| 347 | if (typeof type === 'object' && type !== null) {
|
---|
| 348 | switch (getProperty(type, '$$typeof')) {
|
---|
| 349 | case REACT_FORWARD_REF_TYPE:
|
---|
| 350 | setSignature(type.render, key, forceReset, getCustomHooks);
|
---|
| 351 | break;
|
---|
| 352 |
|
---|
| 353 | case REACT_MEMO_TYPE:
|
---|
| 354 | setSignature(type.type, key, forceReset, getCustomHooks);
|
---|
| 355 | break;
|
---|
| 356 | }
|
---|
| 357 | }
|
---|
| 358 | }
|
---|
| 359 | } // This is lazily called during first render for a type.
|
---|
| 360 | // It captures Hook list at that time so inline requires don't break comparisons.
|
---|
| 361 |
|
---|
| 362 | function collectCustomHooksForSignature(type) {
|
---|
| 363 | {
|
---|
| 364 | var signature = allSignaturesByType.get(type);
|
---|
| 365 |
|
---|
| 366 | if (signature !== undefined) {
|
---|
| 367 | computeFullKey(signature);
|
---|
| 368 | }
|
---|
| 369 | }
|
---|
| 370 | }
|
---|
| 371 | function getFamilyByID(id) {
|
---|
| 372 | {
|
---|
| 373 | return allFamiliesByID.get(id);
|
---|
| 374 | }
|
---|
| 375 | }
|
---|
| 376 | function getFamilyByType(type) {
|
---|
| 377 | {
|
---|
| 378 | return allFamiliesByType.get(type);
|
---|
| 379 | }
|
---|
| 380 | }
|
---|
| 381 | function findAffectedHostInstances(families) {
|
---|
| 382 | {
|
---|
| 383 | var affectedInstances = new Set();
|
---|
| 384 | mountedRoots.forEach(function (root) {
|
---|
| 385 | var helpers = helpersByRoot.get(root);
|
---|
| 386 |
|
---|
| 387 | if (helpers === undefined) {
|
---|
| 388 | throw new Error('Could not find helpers for a root. This is a bug in React Refresh.');
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | var instancesForRoot = helpers.findHostInstancesForRefresh(root, families);
|
---|
| 392 | instancesForRoot.forEach(function (inst) {
|
---|
| 393 | affectedInstances.add(inst);
|
---|
| 394 | });
|
---|
| 395 | });
|
---|
| 396 | return affectedInstances;
|
---|
| 397 | }
|
---|
| 398 | }
|
---|
| 399 | function injectIntoGlobalHook(globalObject) {
|
---|
| 400 | {
|
---|
| 401 | // For React Native, the global hook will be set up by require('react-devtools-core').
|
---|
| 402 | // That code will run before us. So we need to monkeypatch functions on existing hook.
|
---|
| 403 | // For React Web, the global hook will be set up by the extension.
|
---|
| 404 | // This will also run before us.
|
---|
| 405 | var hook = globalObject.__REACT_DEVTOOLS_GLOBAL_HOOK__;
|
---|
| 406 |
|
---|
| 407 | if (hook === undefined) {
|
---|
| 408 | // However, if there is no DevTools extension, we'll need to set up the global hook ourselves.
|
---|
| 409 | // Note that in this case it's important that renderer code runs *after* this method call.
|
---|
| 410 | // Otherwise, the renderer will think that there is no global hook, and won't do the injection.
|
---|
| 411 | var nextID = 0;
|
---|
| 412 | globalObject.__REACT_DEVTOOLS_GLOBAL_HOOK__ = hook = {
|
---|
| 413 | renderers: new Map(),
|
---|
| 414 | supportsFiber: true,
|
---|
| 415 | inject: function (injected) {
|
---|
| 416 | return nextID++;
|
---|
| 417 | },
|
---|
| 418 | onScheduleFiberRoot: function (id, root, children) {},
|
---|
| 419 | onCommitFiberRoot: function (id, root, maybePriorityLevel, didError) {},
|
---|
| 420 | onCommitFiberUnmount: function () {}
|
---|
| 421 | };
|
---|
| 422 | }
|
---|
| 423 |
|
---|
| 424 | if (hook.isDisabled) {
|
---|
| 425 | // This isn't a real property on the hook, but it can be set to opt out
|
---|
| 426 | // of DevTools integration and associated warnings and logs.
|
---|
| 427 | // Using console['warn'] to evade Babel and ESLint
|
---|
| 428 | console['warn']('Something has shimmed the React DevTools global hook (__REACT_DEVTOOLS_GLOBAL_HOOK__). ' + 'Fast Refresh is not compatible with this shim and will be disabled.');
|
---|
| 429 | return;
|
---|
| 430 | } // Here, we just want to get a reference to scheduleRefresh.
|
---|
| 431 |
|
---|
| 432 |
|
---|
| 433 | var oldInject = hook.inject;
|
---|
| 434 |
|
---|
| 435 | hook.inject = function (injected) {
|
---|
| 436 | var id = oldInject.apply(this, arguments);
|
---|
| 437 |
|
---|
| 438 | if (typeof injected.scheduleRefresh === 'function' && typeof injected.setRefreshHandler === 'function') {
|
---|
| 439 | // This version supports React Refresh.
|
---|
| 440 | helpersByRendererID.set(id, injected);
|
---|
| 441 | }
|
---|
| 442 |
|
---|
| 443 | return id;
|
---|
| 444 | }; // Do the same for any already injected roots.
|
---|
| 445 | // This is useful if ReactDOM has already been initialized.
|
---|
| 446 | // https://github.com/facebook/react/issues/17626
|
---|
| 447 |
|
---|
| 448 |
|
---|
| 449 | hook.renderers.forEach(function (injected, id) {
|
---|
| 450 | if (typeof injected.scheduleRefresh === 'function' && typeof injected.setRefreshHandler === 'function') {
|
---|
| 451 | // This version supports React Refresh.
|
---|
| 452 | helpersByRendererID.set(id, injected);
|
---|
| 453 | }
|
---|
| 454 | }); // We also want to track currently mounted roots.
|
---|
| 455 |
|
---|
| 456 | var oldOnCommitFiberRoot = hook.onCommitFiberRoot;
|
---|
| 457 |
|
---|
| 458 | var oldOnScheduleFiberRoot = hook.onScheduleFiberRoot || function () {};
|
---|
| 459 |
|
---|
| 460 | hook.onScheduleFiberRoot = function (id, root, children) {
|
---|
| 461 | if (!isPerformingRefresh) {
|
---|
| 462 | // If it was intentionally scheduled, don't attempt to restore.
|
---|
| 463 | // This includes intentionally scheduled unmounts.
|
---|
| 464 | failedRoots.delete(root);
|
---|
| 465 |
|
---|
| 466 | if (rootElements !== null) {
|
---|
| 467 | rootElements.set(root, children);
|
---|
| 468 | }
|
---|
| 469 | }
|
---|
| 470 |
|
---|
| 471 | return oldOnScheduleFiberRoot.apply(this, arguments);
|
---|
| 472 | };
|
---|
| 473 |
|
---|
| 474 | hook.onCommitFiberRoot = function (id, root, maybePriorityLevel, didError) {
|
---|
| 475 | var helpers = helpersByRendererID.get(id);
|
---|
| 476 |
|
---|
| 477 | if (helpers !== undefined) {
|
---|
| 478 | helpersByRoot.set(root, helpers);
|
---|
| 479 | var current = root.current;
|
---|
| 480 | var alternate = current.alternate; // We need to determine whether this root has just (un)mounted.
|
---|
| 481 | // This logic is copy-pasted from similar logic in the DevTools backend.
|
---|
| 482 | // If this breaks with some refactoring, you'll want to update DevTools too.
|
---|
| 483 |
|
---|
| 484 | if (alternate !== null) {
|
---|
| 485 | var wasMounted = alternate.memoizedState != null && alternate.memoizedState.element != null && mountedRoots.has(root);
|
---|
| 486 | var isMounted = current.memoizedState != null && current.memoizedState.element != null;
|
---|
| 487 |
|
---|
| 488 | if (!wasMounted && isMounted) {
|
---|
| 489 | // Mount a new root.
|
---|
| 490 | mountedRoots.add(root);
|
---|
| 491 | failedRoots.delete(root);
|
---|
| 492 | } else if (wasMounted && isMounted) ; else if (wasMounted && !isMounted) {
|
---|
| 493 | // Unmount an existing root.
|
---|
| 494 | mountedRoots.delete(root);
|
---|
| 495 |
|
---|
| 496 | if (didError) {
|
---|
| 497 | // We'll remount it on future edits.
|
---|
| 498 | failedRoots.add(root);
|
---|
| 499 | } else {
|
---|
| 500 | helpersByRoot.delete(root);
|
---|
| 501 | }
|
---|
| 502 | } else if (!wasMounted && !isMounted) {
|
---|
| 503 | if (didError) {
|
---|
| 504 | // We'll remount it on future edits.
|
---|
| 505 | failedRoots.add(root);
|
---|
| 506 | }
|
---|
| 507 | }
|
---|
| 508 | } else {
|
---|
| 509 | // Mount a new root.
|
---|
| 510 | mountedRoots.add(root);
|
---|
| 511 | }
|
---|
| 512 | } // Always call the decorated DevTools hook.
|
---|
| 513 |
|
---|
| 514 |
|
---|
| 515 | return oldOnCommitFiberRoot.apply(this, arguments);
|
---|
| 516 | };
|
---|
| 517 | }
|
---|
| 518 | }
|
---|
| 519 | function hasUnrecoverableErrors() {
|
---|
| 520 | // TODO: delete this after removing dependency in RN.
|
---|
| 521 | return false;
|
---|
| 522 | } // Exposed for testing.
|
---|
| 523 |
|
---|
| 524 | function _getMountedRootCount() {
|
---|
| 525 | {
|
---|
| 526 | return mountedRoots.size;
|
---|
| 527 | }
|
---|
| 528 | } // This is a wrapper over more primitive functions for setting signature.
|
---|
| 529 | // Signatures let us decide whether the Hook order has changed on refresh.
|
---|
| 530 | //
|
---|
| 531 | // This function is intended to be used as a transform target, e.g.:
|
---|
| 532 | // var _s = createSignatureFunctionForTransform()
|
---|
| 533 | //
|
---|
| 534 | // function Hello() {
|
---|
| 535 | // const [foo, setFoo] = useState(0);
|
---|
| 536 | // const value = useCustomHook();
|
---|
| 537 | // _s(); /* Call without arguments triggers collecting the custom Hook list.
|
---|
| 538 | // * This doesn't happen during the module evaluation because we
|
---|
| 539 | // * don't want to change the module order with inline requires.
|
---|
| 540 | // * Next calls are noops. */
|
---|
| 541 | // return <h1>Hi</h1>;
|
---|
| 542 | // }
|
---|
| 543 | //
|
---|
| 544 | // /* Call with arguments attaches the signature to the type: */
|
---|
| 545 | // _s(
|
---|
| 546 | // Hello,
|
---|
| 547 | // 'useState{[foo, setFoo]}(0)',
|
---|
| 548 | // () => [useCustomHook], /* Lazy to avoid triggering inline requires */
|
---|
| 549 | // );
|
---|
| 550 |
|
---|
| 551 | function createSignatureFunctionForTransform() {
|
---|
| 552 | {
|
---|
| 553 | var savedType;
|
---|
| 554 | var hasCustomHooks;
|
---|
| 555 | var didCollectHooks = false;
|
---|
| 556 | return function (type, key, forceReset, getCustomHooks) {
|
---|
| 557 | if (typeof key === 'string') {
|
---|
| 558 | // We're in the initial phase that associates signatures
|
---|
| 559 | // with the functions. Note this may be called multiple times
|
---|
| 560 | // in HOC chains like _s(hoc1(_s(hoc2(_s(actualFunction))))).
|
---|
| 561 | if (!savedType) {
|
---|
| 562 | // We're in the innermost call, so this is the actual type.
|
---|
| 563 | savedType = type;
|
---|
| 564 | hasCustomHooks = typeof getCustomHooks === 'function';
|
---|
| 565 | } // Set the signature for all types (even wrappers!) in case
|
---|
| 566 | // they have no signatures of their own. This is to prevent
|
---|
| 567 | // problems like https://github.com/facebook/react/issues/20417.
|
---|
| 568 |
|
---|
| 569 |
|
---|
| 570 | if (type != null && (typeof type === 'function' || typeof type === 'object')) {
|
---|
| 571 | setSignature(type, key, forceReset, getCustomHooks);
|
---|
| 572 | }
|
---|
| 573 |
|
---|
| 574 | return type;
|
---|
| 575 | } else {
|
---|
| 576 | // We're in the _s() call without arguments, which means
|
---|
| 577 | // this is the time to collect custom Hook signatures.
|
---|
| 578 | // Only do this once. This path is hot and runs *inside* every render!
|
---|
| 579 | if (!didCollectHooks && hasCustomHooks) {
|
---|
| 580 | didCollectHooks = true;
|
---|
| 581 | collectCustomHooksForSignature(savedType);
|
---|
| 582 | }
|
---|
| 583 | }
|
---|
| 584 | };
|
---|
| 585 | }
|
---|
| 586 | }
|
---|
| 587 | function isLikelyComponentType(type) {
|
---|
| 588 | {
|
---|
| 589 | switch (typeof type) {
|
---|
| 590 | case 'function':
|
---|
| 591 | {
|
---|
| 592 | // First, deal with classes.
|
---|
| 593 | if (type.prototype != null) {
|
---|
| 594 | if (type.prototype.isReactComponent) {
|
---|
| 595 | // React class.
|
---|
| 596 | return true;
|
---|
| 597 | }
|
---|
| 598 |
|
---|
| 599 | var ownNames = Object.getOwnPropertyNames(type.prototype);
|
---|
| 600 |
|
---|
| 601 | if (ownNames.length > 1 || ownNames[0] !== 'constructor') {
|
---|
| 602 | // This looks like a class.
|
---|
| 603 | return false;
|
---|
| 604 | } // eslint-disable-next-line no-proto
|
---|
| 605 |
|
---|
| 606 |
|
---|
| 607 | if (type.prototype.__proto__ !== Object.prototype) {
|
---|
| 608 | // It has a superclass.
|
---|
| 609 | return false;
|
---|
| 610 | } // Pass through.
|
---|
| 611 | // This looks like a regular function with empty prototype.
|
---|
| 612 |
|
---|
| 613 | } // For plain functions and arrows, use name as a heuristic.
|
---|
| 614 |
|
---|
| 615 |
|
---|
| 616 | var name = type.name || type.displayName;
|
---|
| 617 | return typeof name === 'string' && /^[A-Z]/.test(name);
|
---|
| 618 | }
|
---|
| 619 |
|
---|
| 620 | case 'object':
|
---|
| 621 | {
|
---|
| 622 | if (type != null) {
|
---|
| 623 | switch (getProperty(type, '$$typeof')) {
|
---|
| 624 | case REACT_FORWARD_REF_TYPE:
|
---|
| 625 | case REACT_MEMO_TYPE:
|
---|
| 626 | // Definitely React components.
|
---|
| 627 | return true;
|
---|
| 628 |
|
---|
| 629 | default:
|
---|
| 630 | return false;
|
---|
| 631 | }
|
---|
| 632 | }
|
---|
| 633 |
|
---|
| 634 | return false;
|
---|
| 635 | }
|
---|
| 636 |
|
---|
| 637 | default:
|
---|
| 638 | {
|
---|
| 639 | return false;
|
---|
| 640 | }
|
---|
| 641 | }
|
---|
| 642 | }
|
---|
| 643 | }
|
---|
| 644 |
|
---|
| 645 | exports._getMountedRootCount = _getMountedRootCount;
|
---|
| 646 | exports.collectCustomHooksForSignature = collectCustomHooksForSignature;
|
---|
| 647 | exports.createSignatureFunctionForTransform = createSignatureFunctionForTransform;
|
---|
| 648 | exports.findAffectedHostInstances = findAffectedHostInstances;
|
---|
| 649 | exports.getFamilyByID = getFamilyByID;
|
---|
| 650 | exports.getFamilyByType = getFamilyByType;
|
---|
| 651 | exports.hasUnrecoverableErrors = hasUnrecoverableErrors;
|
---|
| 652 | exports.injectIntoGlobalHook = injectIntoGlobalHook;
|
---|
| 653 | exports.isLikelyComponentType = isLikelyComponentType;
|
---|
| 654 | exports.performReactRefresh = performReactRefresh;
|
---|
| 655 | exports.register = register;
|
---|
| 656 | exports.setSignature = setSignature;
|
---|
| 657 | })();
|
---|
| 658 | }
|
---|