MCPcopy Create free account
hub / github.com/microsoft/SandDance / invokeGuardedCallbackDev

Function invokeGuardedCallbackDev

docs/external/js/react-dom.development.js:137–250  ·  view source on GitHub ↗
(name, func, context, a, b, c, d, e, f)

Source from the content-addressed store, hash-verified

135 var fakeNode = document.createElement('react');
136
137 var invokeGuardedCallbackDev = function (name, func, context, a, b, c, d, e, f) {
138 // If document doesn't exist we know for sure we will crash in this method
139 // when we call document.createEvent(). However this can cause confusing
140 // errors: https://github.com/facebookincubator/create-react-app/issues/3482
141 // So we preemptively throw with a better message instead.
142 if (!(typeof document !== 'undefined')) {
143 {
144 throw Error( "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." );
145 }
146 }
147
148 var evt = document.createEvent('Event'); // Keeps track of whether the user-provided callback threw an error. We
149 // set this to true at the beginning, then set it to false right after
150 // calling the function. If the function errors, `didError` will never be
151 // set to false. This strategy works even if the browser is flaky and
152 // fails to call our global error handler, because it doesn't rely on
153 // the error event at all.
154
155 var didError = true; // Keeps track of the value of window.event so that we can reset it
156 // during the callback to let user code access window.event in the
157 // browsers that support it.
158
159 var windowEvent = window.event; // Keeps track of the descriptor of window.event to restore it after event
160 // dispatching: https://github.com/facebook/react/issues/13688
161
162 var windowEventDescriptor = Object.getOwnPropertyDescriptor(window, 'event'); // Create an event handler for our fake event. We will synchronously
163 // dispatch our fake event using `dispatchEvent`. Inside the handler, we
164 // call the user-provided callback.
165
166 var funcArgs = Array.prototype.slice.call(arguments, 3);
167
168 function callCallback() {
169 // We immediately remove the callback from event listeners so that
170 // nested `invokeGuardedCallback` calls do not clash. Otherwise, a
171 // nested call would trigger the fake event handlers of any call higher
172 // in the stack.
173 fakeNode.removeEventListener(evtType, callCallback, false); // We check for window.hasOwnProperty('event') to prevent the
174 // window.event assignment in both IE <= 10 as they throw an error
175 // "Member not found" in strict mode, and in Firefox which does not
176 // support window.event.
177
178 if (typeof window.event !== 'undefined' && window.hasOwnProperty('event')) {
179 window.event = windowEvent;
180 }
181
182 func.apply(context, funcArgs);
183 didError = false;
184 } // Create a global error event handler. We use this to capture the value
185 // that was thrown. It's possible that this error handler will fire more
186 // than once; for example, if non-React code also calls `dispatchEvent`
187 // and a handler for that event throws. We should be resilient to most of
188 // those cases. Even if our error event handler fires more than once, the
189 // last error event is always used. If the callback actually does error,
190 // we know that the last error event is the correct one, because it's not
191 // possible for anything else to have happened in between our callback
192 // erroring and the code that follows the `dispatchEvent` call below. If
193 // the callback doesn't error, but the error event was fired, we know to
194 // ignore it because `didError` will be false, as described above.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected