Files
moon-gaming/gd games 3/jsonmanager.js
T
2023-11-24 03:27:59 +00:00

3 lines
1.9 KiB
JavaScript

var gdjs;(function(c){const l=new c.Logger("JSON Manager");class i{constructor(e,o){this._loadedJsons={};this._callbacks={};this._resources=new Map,this.setResources(e),this._resourcesLoader=o}setResources(e){this._resources.clear();for(const o of e)(o.kind==="json"||o.kind==="tilemap"||o.kind==="tileset")&&this._resources.set(o.name,o)}async preloadJsons(e){const o=[...this._resources.values()].filter(t=>!t.disablePreload);let n=0;return await Promise.all(o.map(async t=>{try{await this.loadJsonAsync(t.name)}catch(s){l.error(`Error while preloading json resource ${t.name}:`,s)}n++,e(n,this._resources.size)})),n}loadJsonAsync(e){const o=this;return new Promise((n,t)=>{o.loadJson(e,(s,r)=>{s&&t(s.message),n(r)})})}loadJson(e,o){const n=this._resources.get(e);if(!n){o(new Error(`Can't find resource with name: "`+e+'" (or is not a json resource).'),null);return}if(this._loadedJsons[e]){o(null,this._loadedJsons[e]);return}{const r=this._callbacks[e];if(r){r.push(o);return}else this._callbacks[e]=[o]}const t=this,s=new XMLHttpRequest;s.responseType="json",s.withCredentials=this._resourcesLoader.checkIfCredentialsRequired(n.file),s.open("GET",this._resourcesLoader.getFullUrl(n.file)),s.onload=function(){const r=t._callbacks[e];if(!!r){if(s.status!==200){for(const a of r)a(new Error("HTTP error: "+s.status+"("+s.statusText+")"),null);delete t._callbacks[e];return}t._loadedJsons[e]=s.response;for(const a of r)a(null,s.response);delete t._callbacks[e]}},s.onerror=function(){const r=t._callbacks[e];if(!!r){for(const a of r)a(new Error("Network error"),null);delete t._callbacks[e]}},s.onabort=function(){const r=t._callbacks[e];if(!!r){for(const a of r)a(new Error("Request aborted"),null);delete t._callbacks[e]}},s.send()}isJsonLoaded(e){return!!this._loadedJsons[e]}getLoadedJson(e){return this._loadedJsons[e]||null}}c.JsonManager=i})(gdjs||(gdjs={}));
//# sourceMappingURL=jsonmanager.js.map