aboutsummaryrefslogtreecommitdiff
path: root/features/settings/helpDataLoader.js
blob: 7c463b229c3e7763215e72704b007bb3b7400cbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { fetch } from "../../utils/networkUtils";

class HelpDataLoader {
    constructor() {
        this.availableHelpData = {}
        this.dataCach = {}

        fetch("http://soopy.dev/api/soopyv2/settingshelpoptions.json").json(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(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;