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