diff --git a/Libraries/Core/setUpXHR.js b/Libraries/Core/setUpXHR.js index ec7baad8af3dcc..b1525ea5561593 100644 --- a/Libraries/Core/setUpXHR.js +++ b/Libraries/Core/setUpXHR.js @@ -30,3 +30,11 @@ polyfillGlobal('File', () => require('File')); polyfillGlobal('FileReader', () => require('FileReader')); polyfillGlobal('URL', () => require('URL').URL); // flowlint-line untyped-import:off polyfillGlobal('URLSearchParams', () => require('URL').URLSearchParams); // flowlint-line untyped-import:off +polyfillGlobal( + 'AbortController', + () => require('abort-controller/dist/abort-controller').AbortController, // flowlint-line untyped-import:off +); +polyfillGlobal( + 'AbortSignal', + () => require('abort-controller/dist/abort-controller').AbortSignal, // flowlint-line untyped-import:off +); diff --git a/Libraries/polyfills/console.js b/Libraries/polyfills/console.js index 7954bcfc01f693..097eccc7186331 100644 --- a/Libraries/polyfills/console.js +++ b/Libraries/polyfills/console.js @@ -519,6 +519,12 @@ function consoleGroupEndPolyfill() { global.nativeLoggingHook(groupFormat(GROUP_CLOSE), LOG_LEVELS.info); } +function consoleAssertPolyfill(expression, label) { + if (!expression) { + global.nativeLoggingHook('Assertion failed: ' + label, LOG_LEVELS.error); + } +} + if (global.nativeLoggingHook) { const originalConsole = global.console; // Preserve the original `console` as `originalConsole` @@ -540,6 +546,7 @@ if (global.nativeLoggingHook) { group: consoleGroupPolyfill, groupEnd: consoleGroupEndPolyfill, groupCollapsed: consoleGroupCollapsedPolyfill, + assert: consoleAssertPolyfill, }; // If available, also call the original `console` method since that is @@ -560,7 +567,6 @@ if (global.nativeLoggingHook) { // we still should pass them to original console if they are // supported by it. [ - 'assert', 'clear', 'dir', 'dirxml', diff --git a/RNTester/js/XHRExample.js b/RNTester/js/XHRExample.js index 78f925e7da12dc..a7df025303a4e8 100644 --- a/RNTester/js/XHRExample.js +++ b/RNTester/js/XHRExample.js @@ -18,6 +18,7 @@ const XHRExampleFormData = require('./XHRExampleFormData'); const XHRExampleHeaders = require('./XHRExampleHeaders'); const XHRExampleFetch = require('./XHRExampleFetch'); const XHRExampleOnTimeOut = require('./XHRExampleOnTimeOut'); +const XHRExampleAbortController = require('./XHRExampleAbortController'); exports.framework = 'React'; exports.title = 'XMLHttpRequest'; @@ -61,4 +62,10 @@ exports.examples = [ return ; }, }, + { + title: 'Abort Test', + render() { + return ; + }, + }, ]; diff --git a/RNTester/js/XHRExampleAbortController.js b/RNTester/js/XHRExampleAbortController.js new file mode 100644 index 00000000000000..2d7b41d23061f4 --- /dev/null +++ b/RNTester/js/XHRExampleAbortController.js @@ -0,0 +1,58 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + * @flow + */ + +'use strict'; + +const React = require('react'); +const {Alert, Button, View} = require('react-native'); + +class XHRExampleAbortController extends React.Component<{}, {}> { + _timeout: any; + + _submit(abortDelay) { + clearTimeout(this._timeout); + // eslint-disable-next-line no-undef + const abortController = new AbortController(); + fetch('https://facebook.github.io/react-native/', { + signal: abortController.signal, + }) + .then(res => res.text()) + .then(res => Alert.alert(res)) + .catch(err => Alert.alert(err.message)); + this._timeout = setTimeout(() => { + abortController.abort(); + }, abortDelay); + } + + componentWillUnmount() { + clearTimeout(this._timeout); + } + + render() { + return ( + +