Skip to content
Permalink
Browse files

Abort previous unfinished fetch requests

Fixes #2.
  • Loading branch information...
mrhead committed Apr 11, 2019
1 parent 48643f1 commit bc99b3b8fcd194690142b72f2372b66acf78c3b5
Showing with 15 additions and 4 deletions.
  1. +15 −4 app/javascript/controllers/search_controller.js
@@ -21,22 +21,33 @@ export default class extends Controller {
const url = new URL(this.data.get("url"))
url.searchParams.append("query", this.query)

fetch(url)
this.abortPreviousFetchRequest()

this.abortController = new AbortController()
fetch(url, { signal: this.abortController.signal })
.then(response => response.text())
.then(html => {
this.resultsTarget.innerHTML = html
})
}

navigateResults(event) {
if(this.searchResultsController) {
this.searchResultsController.navigateResults(event)
}
}

// private

reset() {
this.resultsTarget.innerHTML = ""
this.queryTarget.value = ""
this.previousQuery = null
}

navigateResults(event) {
if(this.searchResultsController) {
this.searchResultsController.navigateResults(event)
abortPreviousFetchRequest() {
if(this.abortController) {
this.abortController.abort()
}
}

0 comments on commit bc99b3b

Please sign in to comment.
You can’t perform that action at this time.