From 431e4fc9d1657a50ebc34b8ac24f9bfaea06417f Mon Sep 17 00:00:00 2001 From: Soopyboo32 <49228220+Soopyboo32@users.noreply.github.com> Date: Sat, 17 Sep 2022 19:39:05 +0800 Subject: Initial move to babel + change fetch to use async/await --- src/features/settings/helpDataLoader.js | 50 +++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/features/settings/helpDataLoader.js (limited to 'src/features/settings/helpDataLoader.js') diff --git a/src/features/settings/helpDataLoader.js b/src/features/settings/helpDataLoader.js new file mode 100644 index 0000000..21ca241 --- /dev/null +++ b/src/features/settings/helpDataLoader.js @@ -0,0 +1,50 @@ +import { fetch } from "../../utils/networkUtils"; + +class HelpDataLoader { + constructor() { + this.availableHelpData = {} + this.dataCach = {} + + fetch("http://soopy.dev/api/soopyv2/settingshelpoptions.json").json().then(data => { + Object.keys(data).forEach(category => { + this.availableHelpData[category] = new Set(data[category]) + }); + }) + } + + hasData(category, id) { + return this.availableHelpData[category] && this.availableHelpData[category].has(id) + } + + getData(category, id, callback) { + if (!this.hasData(category, id)) { + callback("") + return + } + + if (this.dataCach[category] && this.dataCach[category][id]) { + callback(this.dataCach[category][id]) + return + } + + fetch("http://soopy.dev/api/soopyv2/settingshelp/" + category + "/" + id).text().then(data => { + if (!this.dataCach[category]) { + this.dataCach[category] = {} + } + + this.dataCach[category][id] = data + + callback(data) + }) + } +} + +if (!global.helpDataLoader) { + global.helpDataLoader = new HelpDataLoader(); + + register("gameUnload", () => { + global.helpDataLoader = undefined + }) +} + +export default global.helpDataLoader; \ No newline at end of file -- cgit