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

Function invokeGuardedCallbackDev

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

Source from the content-addressed store, hash-verified

1774 var fakeNode = document.createElement('react');
1775
1776 var invokeGuardedCallbackDev = function (name, func, context, a, b, c, d, e, f) {
1777 // If document doesn't exist we know for sure we will crash in this method
1778 // when we call document.createEvent(). However this can cause confusing
1779 // errors: https://github.com/facebookincubator/create-react-app/issues/3482
1780 // So we preemptively throw with a better message instead.
1781 !(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;
1782 var evt = document.createEvent('Event');
1783
1784 // Keeps track of whether the user-provided callback threw an error. We
1785 // set this to true at the beginning, then set it to false right after
1786 // calling the function. If the function errors, `didError` will never be
1787 // set to false. This strategy works even if the browser is flaky and
1788 // fails to call our global error handler, because it doesn't rely on
1789 // the error event at all.
1790 var didError = true;
1791
1792 // Create an event handler for our fake event. We will synchronously
1793 // dispatch our fake event using `dispatchEvent`. Inside the handler, we
1794 // call the user-provided callback.
1795 var funcArgs = Array.prototype.slice.call(arguments, 3);
1796 function callCallback() {
1797 // We immediately remove the callback from event listeners so that
1798 // nested `invokeGuardedCallback` calls do not clash. Otherwise, a
1799 // nested call would trigger the fake event handlers of any call higher
1800 // in the stack.
1801 fakeNode.removeEventListener(evtType, callCallback, false);
1802 func.apply(context, funcArgs);
1803 didError = false;
1804 }
1805
1806 // Create a global error event handler. We use this to capture the value
1807 // that was thrown. It's possible that this error handler will fire more
1808 // than once; for example, if non-React code also calls `dispatchEvent`
1809 // and a handler for that event throws. We should be resilient to most of
1810 // those cases. Even if our error event handler fires more than once, the
1811 // last error event is always used. If the callback actually does error,
1812 // we know that the last error event is the correct one, because it's not
1813 // possible for anything else to have happened in between our callback
1814 // erroring and the code that follows the `dispatchEvent` call below. If
1815 // the callback doesn't error, but the error event was fired, we know to
1816 // ignore it because `didError` will be false, as described above.
1817 var error = void 0;
1818 // Use this to track whether the error event is ever called.
1819 var didSetError = false;
1820 var isCrossOriginError = false;
1821
1822 function onError(event) {
1823 error = event.error;
1824 didSetError = true;
1825 if (error === null && event.colno === 0 && event.lineno === 0) {
1826 isCrossOriginError = true;
1827 }
1828 }
1829
1830 // Create a fake event type.
1831 var evtType = 'react-' + (name ? name : 'invokeguardedcallback');
1832
1833 // Attach our event handlers

Callers

nothing calls this directly

Calls 1

invariantFunction · 0.70

Tested by

no test coverage detected