MCPcopy Create free account
hub / github.com/krasimir/react-in-patterns / invokeGuardedCallbackDev

Function invokeGuardedCallbackDev

code/third-party/public/app.js:974–1056  ·  view source on GitHub ↗
(name, func, context, a, b, c, d, e, f)

Source from the content-addressed store, hash-verified

972 var fakeNode = document.createElement('react');
973
974 var invokeGuardedCallbackDev = function (name, func, context, a, b, c, d, e, f) {
975 // If document doesn't exist we know for sure we will crash in this method
976 // when we call document.createEvent(). However this can cause confusing
977 // errors: https://github.com/facebookincubator/create-react-app/issues/3482
978 // So we preemptively throw with a better message instead.
979 !(typeof document !== 'undefined') ? invariant(false, 'The `document` global was defined when React was initialized, but is not defined anymore. This can happen in a test environment if a component schedules an update from an asynchronous callback, but the test has already finished running. To solve this, you can either unmount the component at the end of your test (and ensure that any asynchronous operations get canceled in `componentWillUnmount`), or you can change the test itself to be asynchronous.') : void 0;
980 var evt = document.createEvent('Event');
981
982 // Keeps track of whether the user-provided callback threw an error. We
983 // set this to true at the beginning, then set it to false right after
984 // calling the function. If the function errors, `didError` will never be
985 // set to false. This strategy works even if the browser is flaky and
986 // fails to call our global error handler, because it doesn't rely on
987 // the error event at all.
988 var didError = true;
989
990 // Create an event handler for our fake event. We will synchronously
991 // dispatch our fake event using `dispatchEvent`. Inside the handler, we
992 // call the user-provided callback.
993 var funcArgs = Array.prototype.slice.call(arguments, 3);
994 function callCallback() {
995 // We immediately remove the callback from event listeners so that
996 // nested `invokeGuardedCallback` calls do not clash. Otherwise, a
997 // nested call would trigger the fake event handlers of any call higher
998 // in the stack.
999 fakeNode.removeEventListener(evtType, callCallback, false);
1000 func.apply(context, funcArgs);
1001 didError = false;
1002 }
1003
1004 // Create a global error event handler. We use this to capture the value
1005 // that was thrown. It's possible that this error handler will fire more
1006 // than once; for example, if non-React code also calls `dispatchEvent`
1007 // and a handler for that event throws. We should be resilient to most of
1008 // those cases. Even if our error event handler fires more than once, the
1009 // last error event is always used. If the callback actually does error,
1010 // we know that the last error event is the correct one, because it's not
1011 // possible for anything else to have happened in between our callback
1012 // erroring and the code that follows the `dispatchEvent` call below. If
1013 // the callback doesn't error, but the error event was fired, we know to
1014 // ignore it because `didError` will be false, as described above.
1015 var error = void 0;
1016 // Use this to track whether the error event is ever called.
1017 var didSetError = false;
1018 var isCrossOriginError = false;
1019
1020 function onError(event) {
1021 error = event.error;
1022 didSetError = true;
1023 if (error === null && event.colno === 0 && event.lineno === 0) {
1024 isCrossOriginError = true;
1025 }
1026 }
1027
1028 // Create a fake event type.
1029 var evtType = 'react-' + (name ? name : 'invokeguardedcallback');
1030
1031 // Attach our event handlers

Callers

nothing calls this directly

Calls 1

invariantFunction · 0.70

Tested by

no test coverage detected