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

Function invokeGuardedCallbackDev

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

Source from the content-addressed store, hash-verified

1739 var fakeNode = document.createElement('react');
1740
1741 var invokeGuardedCallbackDev = function (name, func, context, a, b, c, d, e, f) {
1742 // If document doesn't exist we know for sure we will crash in this method
1743 // when we call document.createEvent(). However this can cause confusing
1744 // errors: https://github.com/facebookincubator/create-react-app/issues/3482
1745 // So we preemptively throw with a better message instead.
1746 !(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;
1747 var evt = document.createEvent('Event');
1748
1749 // Keeps track of whether the user-provided callback threw an error. We
1750 // set this to true at the beginning, then set it to false right after
1751 // calling the function. If the function errors, `didError` will never be
1752 // set to false. This strategy works even if the browser is flaky and
1753 // fails to call our global error handler, because it doesn't rely on
1754 // the error event at all.
1755 var didError = true;
1756
1757 // Create an event handler for our fake event. We will synchronously
1758 // dispatch our fake event using `dispatchEvent`. Inside the handler, we
1759 // call the user-provided callback.
1760 var funcArgs = Array.prototype.slice.call(arguments, 3);
1761 function callCallback() {
1762 // We immediately remove the callback from event listeners so that
1763 // nested `invokeGuardedCallback` calls do not clash. Otherwise, a
1764 // nested call would trigger the fake event handlers of any call higher
1765 // in the stack.
1766 fakeNode.removeEventListener(evtType, callCallback, false);
1767 func.apply(context, funcArgs);
1768 didError = false;
1769 }
1770
1771 // Create a global error event handler. We use this to capture the value
1772 // that was thrown. It's possible that this error handler will fire more
1773 // than once; for example, if non-React code also calls `dispatchEvent`
1774 // and a handler for that event throws. We should be resilient to most of
1775 // those cases. Even if our error event handler fires more than once, the
1776 // last error event is always used. If the callback actually does error,
1777 // we know that the last error event is the correct one, because it's not
1778 // possible for anything else to have happened in between our callback
1779 // erroring and the code that follows the `dispatchEvent` call below. If
1780 // the callback doesn't error, but the error event was fired, we know to
1781 // ignore it because `didError` will be false, as described above.
1782 var error = void 0;
1783 // Use this to track whether the error event is ever called.
1784 var didSetError = false;
1785 var isCrossOriginError = false;
1786
1787 function onError(event) {
1788 error = event.error;
1789 didSetError = true;
1790 if (error === null && event.colno === 0 && event.lineno === 0) {
1791 isCrossOriginError = true;
1792 }
1793 }
1794
1795 // Create a fake event type.
1796 var evtType = 'react-' + (name ? name : 'invokeguardedcallback');
1797
1798 // Attach our event handlers

Callers

nothing calls this directly

Calls 1

invariantFunction · 0.70

Tested by

no test coverage detected