Skip to content

Instantly share code, notes, and snippets.

@pzuraq
Created May 19, 2020 15:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pzuraq/d9e583296c77160275847ecf162e786e to your computer and use it in GitHub Desktop.
Save pzuraq/d9e583296c77160275847ecf162e786e to your computer and use it in GitHub Desktop.
class FetchTask {
@tracked isLoading = true;
@tracked result;
constructor(url) {
this.run(url);
}
async run(url) {
let response = await fetch(url);
this.result = await response.json();
this.isLoading = false;
}
}
class RemoteData {
currentUrl = null;
currentFetch = null;
get(url) {
if (url !== this.currentUrl) {
this.currentFetch = new FetchTask(url);
this.currentUrl = url;
}
return this.currentFetch;
}
}
class MyComponent extends Component {
remoteData = new RemoteData();
get data() {
return this.remoteData.get(this.args.url);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment