Created
May 19, 2020 15:14
-
-
Save pzuraq/d9e583296c77160275847ecf162e786e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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