-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonmanager.js
2 lines (2 loc) · 1.81 KB
/
jsonmanager.js
1
2
var gdjs;(function(d){const i=new d.Logger("JSON Manager"),c=a=>!!(a.startsWith("https://project-resources.gdevelop.io/")||a.startsWith("https://project-resources-dev.gdevelop.io/"));class u{constructor(n){this._loadedJsons={};this._callbacks={};this._resources=n}setResources(n){this._resources=n}preloadJsons(n,l){const s=this._resources.filter(function(e){return(e.kind==="json"||e.kind==="tilemap"||e.kind==="tileset")&&!e.disablePreload});if(s.length===0)return l(s.length);let o=0;const t=function(e){e&&i.error("Error while preloading a json resource:"+e),o++,o===s.length?l(s.length):n(o,s.length)};for(let e=0;e<s.length;++e)this.loadJson(s[e].name,t)}loadJson(n,l){const r=this._resources.find(function(t){return(t.kind==="json"||t.kind==="tilemap"||t.kind==="tileset")&&t.name===n});if(!r){l(new Error(`Can't find resource with name: "`+n+'" (or is not a json resource).'),null);return}if(this._loadedJsons[n]){l(null,this._loadedJsons[n]);return}{const t=this._callbacks[n];if(t){t.push(l);return}else this._callbacks[n]=[l]}const s=this,o=new XMLHttpRequest;o.responseType="json",o.withCredentials=c(r.file),o.open("GET",r.file),o.onload=function(){const t=s._callbacks[n];if(!!t){if(o.status!==200){for(const e of t)e(new Error("HTTP error: "+o.status+"("+o.statusText+")"),null);delete s._callbacks[n];return}s._loadedJsons[n]=o.response;for(const e of t)e(null,o.response);delete s._callbacks[n]}},o.onerror=function(){const t=s._callbacks[n];if(!!t){for(const e of t)e(new Error("Network error"),null);delete s._callbacks[n]}},o.onabort=function(){const t=s._callbacks[n];if(!!t){for(const e of t)e(new Error("Request aborted"),null);delete s._callbacks[n]}},o.send()}isJsonLoaded(n){return!!this._loadedJsons[n]}getLoadedJson(n){return this._loadedJsons[n]||null}}d.JsonManager=u})(gdjs||(gdjs={}));
//# sourceMappingURL=jsonmanager.js.map