-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgear_cache.js
102 lines (93 loc) · 2.7 KB
/
gear_cache.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const CONTROLLER = new AbortController();
function fetch_json_options(method, data) {
return {
body: JSON.stringify(data),
method: method,
headers: {
"Content-Type": "application/json",
},
signal: CONTROLLER.signal,
}
}
function fetch_json_missing(type, id) {
const item = {
id: id,
type: type,
}
const missing_options = fetch_json_options("PUT", item);
return fetch("/missing", missing_options);
}
function empty_json_promise() {
return new Promise(resolve => resolve(undefined));
// return new Promise(resolve => resolve({}));
}
class FetchCache {
constructor() {
this.id = Date.now();
this.CACHES = {
class_set: {},
icon: {},
item: {},
enchant: {},
};
}
async fetch_item_data(id) {
return this._fetch_wrap("item", id);
}
async fetch_enchant_data(id) {
return this._fetch_wrap("enchant", id);
}
async fetch_class_set(class_name) {
class_name = class_name.toLowerCase().replace(" ", "");
if (!this.CACHES.class_set[class_name]) {
await this._fetch_class_set(class_name);
}
return this.CACHES.class_set[class_name];
}
async icon_exists(id) {
const cache = this.CACHES.icon;
if (!cache[id]) {
cache[id] = this._fetch_icon_exists(id);
}
return cache[id];
}
async _fetch_json(type, id) {
const url = `/static/${type}/${id}.json`;
const cache_options = {signal: CONTROLLER.signal};
const cache_response = await fetch(url, cache_options);
if (cache_response.status == 200) {
return cache_response.json();
}
}
async _fetch_missing_check_exists(type, id) {
const missing_response = await fetch_json_missing(type, id);
return [200, 201, 409].includes(missing_response.status);
}
async _fetch_json_wrap(type, id) {
const fetch_try_1 = await this._fetch_json(type, id);
if (fetch_try_1) return fetch_try_1;
const missing_created = await this._fetch_missing_check_exists(type, id);
if (missing_created) {
const fetch_try_2 = await this._fetch_json(type, id);
if (fetch_try_2) return fetch_try_2;
}
return empty_json_promise();
}
async _fetch_wrap(type, id) {
const cache = this.CACHES[type];
if (!cache[id]) {
cache[id] = this._fetch_json_wrap(type, id);
}
return cache[id];
}
async _fetch_class_set(class_name) {
const response = await fetch(`/static/sets/${class_name}.json`);
this.CACHES.class_set[class_name] = response.json();
}
async _fetch_icon_exists(id) {
const fetch_try_1 = await fetch(`/static/icons/${id}.jpg`);
if (fetch_try_1.status != 404) return true;
return await this._fetch_missing_check_exists("icon", id);
}
}
export const CACHE = new FetchCache();