Skip to content

Instantly share code, notes, and snippets.

@addam
Last active March 2, 2021 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save addam/b6e4b6a8c45b0af171cc561c13c07f42 to your computer and use it in GitHub Desktop.
Save addam/b6e4b6a8c45b0af171cc561c13c07f42 to your computer and use it in GitHub Desktop.
make sure that outdated web responses are ignored
class Barrier {
constructor() {
this.time = 0
this.state = 0
}
start() {
return ++this.time
}
// Check that the suggested update is helpful
end(time) {
if (this.state < time) {
this.state = time
return true
}
return false
}
}
// Example usage:
const barrier = new Barrier()
async function search(ev) {
const query = ev.target.value
const transaction = barrier.start()
const response = await fetch(`/api/search/${query}?limit=100`, {method: 'GET'})
if (barrier.end(transaction)) {
resultList.innerHTML = response.text()
}
}
const input = document.createElement("input")
input.oninput = search
input.default = "Type to search..."
document.body.appendChild(input)
const resultList = document.createElement("ul")
document.body.appendChild(resultList)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment