-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for cancelling fetch requests with AbortController (#24419)
Summary: This adds https://github.com/mysticatea/abort-controller to polyfill [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController). This is used to cancel requests when using `fetch`. This also updates `event-target-shim` to 5.0 to make sure we only have one version of this dependency. This updates required adding a polyfill for `console.assert` which is used by the new version. I made one based on https://github.com/gskinner/console-polyfill/blob/master/console.js#L74. The polyfill is very small, especially since we already use `event-target-shim` so I think it makes sense to include in core. Depends on #24418 so that the fetch polyfill supports the `signal` parameter. Fixes #18115 [General] [Added] - Add support for cancelling fetch requests with AbortController Pull Request resolved: #24419 Differential Revision: D14912858 Pulled By: cpojer fbshipit-source-id: 8a6402910398db51e2f3e3262f07aabdf68fcf72
- Loading branch information
1 parent
5e44c08
commit 5e36b0c
Showing
6 changed files
with
93 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ( | ||
<View> | ||
<Button | ||
title="Abort before response" | ||
onPress={() => { | ||
this._submit(0); | ||
}} | ||
/> | ||
<Button | ||
title="Abort after response" | ||
onPress={() => { | ||
this._submit(5000); | ||
}} | ||
/> | ||
</View> | ||
); | ||
} | ||
} | ||
|
||
module.exports = XHRExampleAbortController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters