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

Function invokeGuardedCallbackDev

code/redux/public/app.js:1992–2074  ·  view source on GitHub ↗
(name, func, context, a, b, c, d, e, f)

Source from the content-addressed store, hash-verified

1990 var fakeNode = document.createElement('react');
1991
1992 var invokeGuardedCallbackDev = function (name, func, context, a, b, c, d, e, f) {
1993 // If document doesn't exist we know for sure we will crash in this method
1994 // when we call document.createEvent(). However this can cause confusing
1995 // errors: https://github.com/facebookincubator/create-react-app/issues/3482
1996 // So we preemptively throw with a better message instead.
1997 !(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;
1998 var evt = document.createEvent('Event');
1999
2000 // Keeps track of whether the user-provided callback threw an error. We
2001 // set this to true at the beginning, then set it to false right after
2002 // calling the function. If the function errors, `didError` will never be
2003 // set to false. This strategy works even if the browser is flaky and
2004 // fails to call our global error handler, because it doesn't rely on
2005 // the error event at all.
2006 var didError = true;
2007
2008 // Create an event handler for our fake event. We will synchronously
2009 // dispatch our fake event using `dispatchEvent`. Inside the handler, we
2010 // call the user-provided callback.
2011 var funcArgs = Array.prototype.slice.call(arguments, 3);
2012 function callCallback() {
2013 // We immediately remove the callback from event listeners so that
2014 // nested `invokeGuardedCallback` calls do not clash. Otherwise, a
2015 // nested call would trigger the fake event handlers of any call higher
2016 // in the stack.
2017 fakeNode.removeEventListener(evtType, callCallback, false);
2018 func.apply(context, funcArgs);
2019 didError = false;
2020 }
2021
2022 // Create a global error event handler. We use this to capture the value
2023 // that was thrown. It's possible that this error handler will fire more
2024 // than once; for example, if non-React code also calls `dispatchEvent`
2025 // and a handler for that event throws. We should be resilient to most of
2026 // those cases. Even if our error event handler fires more than once, the
2027 // last error event is always used. If the callback actually does error,
2028 // we know that the last error event is the correct one, because it's not
2029 // possible for anything else to have happened in between our callback
2030 // erroring and the code that follows the `dispatchEvent` call below. If
2031 // the callback doesn't error, but the error event was fired, we know to
2032 // ignore it because `didError` will be false, as described above.
2033 var error = void 0;
2034 // Use this to track whether the error event is ever called.
2035 var didSetError = false;
2036 var isCrossOriginError = false;
2037
2038 function onError(event) {
2039 error = event.error;
2040 didSetError = true;
2041 if (error === null && event.colno === 0 && event.lineno === 0) {
2042 isCrossOriginError = true;
2043 }
2044 }
2045
2046 // Create a fake event type.
2047 var evtType = 'react-' + (name ? name : 'invokeguardedcallback');
2048
2049 // Attach our event handlers

Callers

nothing calls this directly

Calls 1

invariantFunction · 0.70

Tested by

no test coverage detected