-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonmanager.js
76 lines (76 loc) · 2.39 KB
/
jsonmanager.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
var gdjs;
(function(gdjs2) {
class JsonManager {
constructor(resources) {
this._loadedJsons = {};
this._resources = resources;
}
setResources(resources) {
this._resources = resources;
}
preloadJsons(onProgress, onComplete) {
const resources = this._resources;
const jsonResources = resources.filter(function(resource) {
return resource.kind === "json" && !resource.disablePreload;
});
if (jsonResources.length === 0) {
return onComplete(jsonResources.length);
}
let loaded = 0;
const onLoad = function(error) {
if (error) {
console.error("Error while preloading a json resource:" + error);
}
loaded++;
if (loaded === jsonResources.length) {
onComplete(jsonResources.length);
} else {
onProgress(loaded, jsonResources.length);
}
};
for (let i = 0; i < jsonResources.length; ++i) {
this.loadJson(jsonResources[i].name, onLoad);
}
}
loadJson(resourceName, callback) {
const resource = this._resources.find(function(resource2) {
return resource2.kind === "json" && resource2.name === resourceName;
});
if (!resource) {
callback(new Error(`Can't find resource with name: "` + resourceName + '" (or is not a json resource).'), null);
return;
}
if (this._loadedJsons[resourceName]) {
callback(null, this._loadedJsons[resourceName]);
return;
}
const that = this;
const xhr = new XMLHttpRequest();
xhr.responseType = "json";
xhr.open("GET", resource.file);
xhr.onload = function() {
if (xhr.status !== 200) {
callback(new Error("HTTP error: " + xhr.status + "(" + xhr.statusText + ")"), null);
return;
}
that._loadedJsons[resourceName] = xhr.response;
callback(null, xhr.response);
};
xhr.onerror = function() {
callback(new Error("Network error"), null);
};
xhr.onabort = function() {
callback(new Error("Request aborted"), null);
};
xhr.send();
}
isJsonLoaded(resourceName) {
return !!this._loadedJsons[resourceName];
}
getLoadedJson(resourceName) {
return this._loadedJsons[resourceName] || null;
}
}
gdjs2.JsonManager = JsonManager;
})(gdjs || (gdjs = {}));
//# sourceMappingURL=jsonmanager.js.map