Last active
March 2, 2021 07:56
-
-
Save addam/b6e4b6a8c45b0af171cc561c13c07f42 to your computer and use it in GitHub Desktop.
make sure that outdated web responses are ignored
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
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