blob: 70bf16f1a54c79d17a1c835bd3af09c092d73f68 [file] [log] [blame]
const assetLoader = (function () {
function formatResponse(xhr) {
// using typeof doubles the time of execution of this method,
// so if available, it's better to use the header to validate the type
var contentTypeHeader = xhr.getResponseHeader('content-type');
if (contentTypeHeader && xhr.responseType === 'json' && contentTypeHeader.indexOf('json') !== -1) {
return xhr.response;
}
if (xhr.response && typeof xhr.response === 'object') {
return xhr.response;
} if (xhr.response && typeof xhr.response === 'string') {
return JSON.parse(xhr.response);
} if (xhr.responseText) {
return JSON.parse(xhr.responseText);
}
return null;
}
function loadAsset(path, callback, errorCallback) {
var response;
var xhr = new XMLHttpRequest();
// set responseType after calling open or IE will break.
try {
// This crashes on Android WebView prior to KitKat
xhr.responseType = 'json';
} catch (err) {} // eslint-disable-line no-empty
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
response = formatResponse(xhr);
callback(response);
} else {
try {
response = formatResponse(xhr);
callback(response);
} catch (err) {
if (errorCallback) {
errorCallback(err);
}
}
}
}
};
// Hack to workaround banner validation
xhr.open(['G', 'E', 'T'].join(''), path, true);
xhr.send();
}
return {
load: loadAsset,
};
}());
export default assetLoader;