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

Function invokeGuardedCallbackDev

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

Source from the content-addressed store, hash-verified

1877 var fakeNode = document.createElement('react');
1878
1879 var invokeGuardedCallbackDev = function (name, func, context, a, b, c, d, e, f) {
1880 // If document doesn't exist we know for sure we will crash in this method
1881 // when we call document.createEvent(). However this can cause confusing
1882 // errors: https://github.com/facebookincubator/create-react-app/issues/3482
1883 // So we preemptively throw with a better message instead.
1884 !(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;
1885 var evt = document.createEvent('Event');
1886
1887 // Keeps track of whether the user-provided callback threw an error. We
1888 // set this to true at the beginning, then set it to false right after
1889 // calling the function. If the function errors, `didError` will never be
1890 // set to false. This strategy works even if the browser is flaky and
1891 // fails to call our global error handler, because it doesn't rely on
1892 // the error event at all.
1893 var didError = true;
1894
1895 // Create an event handler for our fake event. We will synchronously
1896 // dispatch our fake event using `dispatchEvent`. Inside the handler, we
1897 // call the user-provided callback.
1898 var funcArgs = Array.prototype.slice.call(arguments, 3);
1899 function callCallback() {
1900 // We immediately remove the callback from event listeners so that
1901 // nested `invokeGuardedCallback` calls do not clash. Otherwise, a
1902 // nested call would trigger the fake event handlers of any call higher
1903 // in the stack.
1904 fakeNode.removeEventListener(evtType, callCallback, false);
1905 func.apply(context, funcArgs);
1906 didError = false;
1907 }
1908
1909 // Create a global error event handler. We use this to capture the value
1910 // that was thrown. It's possible that this error handler will fire more
1911 // than once; for example, if non-React code also calls `dispatchEvent`
1912 // and a handler for that event throws. We should be resilient to most of
1913 // those cases. Even if our error event handler fires more than once, the
1914 // last error event is always used. If the callback actually does error,
1915 // we know that the last error event is the correct one, because it's not
1916 // possible for anything else to have happened in between our callback
1917 // erroring and the code that follows the `dispatchEvent` call below. If
1918 // the callback doesn't error, but the error event was fired, we know to
1919 // ignore it because `didError` will be false, as described above.
1920 var error = void 0;
1921 // Use this to track whether the error event is ever called.
1922 var didSetError = false;
1923 var isCrossOriginError = false;
1924
1925 function onError(event) {
1926 error = event.error;
1927 didSetError = true;
1928 if (error === null && event.colno === 0 && event.lineno === 0) {
1929 isCrossOriginError = true;
1930 }
1931 }
1932
1933 // Create a fake event type.
1934 var evtType = 'react-' + (name ? name : 'invokeguardedcallback');
1935
1936 // Attach our event handlers

Callers

nothing calls this directly

Calls 1

invariantFunction · 0.70

Tested by

no test coverage detected