From 632b0276c400057867d930b4928a41575fd6baf6 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Thu, 7 Nov 2019 22:08:28 +0000 Subject: [PATCH] Abort previous search requests --- webclient/package.json | 1 + webclient/src/Results.js | 15 +++++++++++++-- webclient/yarn.lock | 12 ++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/webclient/package.json b/webclient/package.json index 99e5370..318d4de 100644 --- a/webclient/package.json +++ b/webclient/package.json @@ -3,6 +3,7 @@ "version": "0.1.0", "private": true, "dependencies": { + "abort-controller": "^3.0.0", "localforage": "^1.7.3", "moment": "^2.24.0", "query-string": "^6.8.3", diff --git a/webclient/src/Results.js b/webclient/src/Results.js index 3e9d648..8c2842a 100644 --- a/webclient/src/Results.js +++ b/webclient/src/Results.js @@ -2,6 +2,7 @@ import React from 'react'; import { Link } from 'react-router-dom'; import { Helmet } from 'react-helmet'; import { sourceLink, infoLine } from './utils.js'; +import AbortController from 'abort-controller'; class Results extends React.Component { constructor(props) { @@ -12,18 +13,28 @@ class Results extends React.Component { error: false, }; + this.controller = null; } performSearch = () => { + if (this.controller) { + this.controller.abort(); + } + + this.controller = new AbortController(); + const signal = this.controller.signal; + const search = this.props.location.search; - fetch('/api/search' + search) + fetch('/api/search' + search, { method: 'get', signal: signal }) .then(res => res.json()) .then( (result) => { this.setState({ stories: result.results }); }, (error) => { - this.setState({ error: true }); + if (error.message !== 'The operation was aborted. ') { + this.setState({ error: true }); + } } ); } diff --git a/webclient/yarn.lock b/webclient/yarn.lock index 32e764e..deaff3a 100644 --- a/webclient/yarn.lock +++ b/webclient/yarn.lock @@ -1395,6 +1395,13 @@ abbrev@1: resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8" integrity sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q== +abort-controller@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/abort-controller/-/abort-controller-3.0.0.tgz#eaf54d53b62bae4138e809ca225c8439a6efb392" + integrity sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg== + dependencies: + event-target-shim "^5.0.0" + accepts@~1.3.4, accepts@~1.3.5, accepts@~1.3.7: version "1.3.7" resolved "https://registry.yarnpkg.com/accepts/-/accepts-1.3.7.tgz#531bc726517a3b2b41f850021c6cc15eaab507cd" @@ -3652,6 +3659,11 @@ etag@~1.8.1: resolved "https://registry.yarnpkg.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887" integrity sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc= +event-target-shim@^5.0.0: + version "5.0.1" + resolved "https://registry.yarnpkg.com/event-target-shim/-/event-target-shim-5.0.1.tgz#5d4d3ebdf9583d63a5333ce2deb7480ab2b05789" + integrity sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ== + eventemitter3@^3.0.0: version "3.1.2" resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-3.1.2.tgz#2d3d48f9c346698fce83a85d7d664e98535df6e7"