MCPcopy Index your code
hub / github.com/krasimir/react-in-patterns / invokeGuardedCallbackDev

Function invokeGuardedCallbackDev

code/new-context-api/public/app.js:1102–1184  ·  view source on GitHub ↗
(name, func, context, a, b, c, d, e, f)

Source from the content-addressed store, hash-verified

1100 var fakeNode = document.createElement('react');
1101
1102 var invokeGuardedCallbackDev = function (name, func, context, a, b, c, d, e, f) {
1103 // If document doesn't exist we know for sure we will crash in this method
1104 // when we call document.createEvent(). However this can cause confusing
1105 // errors: https://github.com/facebookincubator/create-react-app/issues/3482
1106 // So we preemptively throw with a better message instead.
1107 !(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;
1108 var evt = document.createEvent('Event');
1109
1110 // Keeps track of whether the user-provided callback threw an error. We
1111 // set this to true at the beginning, then set it to false right after
1112 // calling the function. If the function errors, `didError` will never be
1113 // set to false. This strategy works even if the browser is flaky and
1114 // fails to call our global error handler, because it doesn't rely on
1115 // the error event at all.
1116 var didError = true;
1117
1118 // Create an event handler for our fake event. We will synchronously
1119 // dispatch our fake event using `dispatchEvent`. Inside the handler, we
1120 // call the user-provided callback.
1121 var funcArgs = Array.prototype.slice.call(arguments, 3);
1122 function callCallback() {
1123 // We immediately remove the callback from event listeners so that
1124 // nested `invokeGuardedCallback` calls do not clash. Otherwise, a
1125 // nested call would trigger the fake event handlers of any call higher
1126 // in the stack.
1127 fakeNode.removeEventListener(evtType, callCallback, false);
1128 func.apply(context, funcArgs);
1129 didError = false;
1130 }
1131
1132 // Create a global error event handler. We use this to capture the value
1133 // that was thrown. It's possible that this error handler will fire more
1134 // than once; for example, if non-React code also calls `dispatchEvent`
1135 // and a handler for that event throws. We should be resilient to most of
1136 // those cases. Even if our error event handler fires more than once, the
1137 // last error event is always used. If the callback actually does error,
1138 // we know that the last error event is the correct one, because it's not
1139 // possible for anything else to have happened in between our callback
1140 // erroring and the code that follows the `dispatchEvent` call below. If
1141 // the callback doesn't error, but the error event was fired, we know to
1142 // ignore it because `didError` will be false, as described above.
1143 var error = void 0;
1144 // Use this to track whether the error event is ever called.
1145 var didSetError = false;
1146 var isCrossOriginError = false;
1147
1148 function onError(event) {
1149 error = event.error;
1150 didSetError = true;
1151 if (error === null && event.colno === 0 && event.lineno === 0) {
1152 isCrossOriginError = true;
1153 }
1154 }
1155
1156 // Create a fake event type.
1157 var evtType = 'react-' + (name ? name : 'invokeguardedcallback');
1158
1159 // Attach our event handlers

Callers

nothing calls this directly

Calls 1

invariantFunction · 0.70

Tested by

no test coverage detected