okvoice / jsonmanager.js
zshashz's picture
okvo
f563b68
var gdjs;(function(c){const l=new c.Logger("JSON Manager"),i=["json","tilemap","tileset"];class u{constructor(s){this._loadedJsons=new c.ResourceCache;this._callbacks=new c.ResourceCache;this._getJsonResource=s=>{const r=this._resourceLoader.getResource(s);return r&&this.getResourceKinds().includes(r.kind)?r:null};this._resourceLoader=s}getResourceKinds(){return i}async loadResource(s){const r=this._resourceLoader.getResource(s);if(!r){l.warn('Unable to find json for resource "'+s+'".');return}if(!r.disablePreload)try{await this.loadJsonAsync(r.name)}catch(e){l.error(`Error while preloading json resource ${r.name}:`,e)}}loadJsonAsync(s){const r=this;return new Promise((e,t)=>{r.loadJson(s,(o,n)=>{o&&t(o.message),e(n)})})}async processResource(s){}loadJson(s,r){const e=this._getJsonResource(s);if(!e){r(new Error(`Can't find resource with name: "`+s+'" (or is not a json resource).'),null);return}if(this._loadedJsons.get(e)){r(null,this._loadedJsons.get(e));return}{const n=this._callbacks.get(e);if(n){n.push(r);return}else this._callbacks.set(e,[r])}const t=this,o=new XMLHttpRequest;o.responseType="json",o.withCredentials=this._resourceLoader.checkIfCredentialsRequired(e.file),o.open("GET",this._resourceLoader.getFullUrl(e.file)),o.onload=function(){const n=t._callbacks.get(e);if(!!n){if(o.status!==200){for(const a of n)a(new Error("HTTP error: "+o.status+"("+o.statusText+")"),null);t._callbacks.delete(e);return}t._loadedJsons.set(e,o.response);for(const a of n)a(null,o.response);t._callbacks.delete(e)}},o.onerror=function(){const n=t._callbacks.get(e);if(!!n){for(const a of n)a(new Error("Network error"),null);t._callbacks.delete(e)}},o.onabort=function(){const n=t._callbacks.get(e);if(!!n){for(const a of n)a(new Error("Request aborted"),null);t._callbacks.delete(e)}},o.send()}isJsonLoaded(s){return!!this._loadedJsons.getFromName(s)}getLoadedJson(s){return this._loadedJsons.getFromName(s)||null}dispose(){this._loadedJsons.clear(),this._callbacks.clear()}}c.JsonManager=u})(gdjs||(gdjs={}));
//# sourceMappingURL=jsonmanager.js.map