From 8a5854f364c98b4ad91980577d2f5d5098d4c66a Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Thu, 11 Apr 2019 14:43:39 -0400 Subject: [PATCH] Add support for AbortController --- Libraries/Core/setUpXHR.js | 8 ++++ RNTester/js/XHRExample.js | 7 +++ RNTester/js/XHRExampleAbortController.js | 58 ++++++++++++++++++++++++ package.json | 3 +- yarn.lock | 15 ++++-- 5 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 RNTester/js/XHRExampleAbortController.js 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/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 ( + +