Publish Vulpes Preview 15 sources for desktop, Android and Tundra
Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
This commit is contained in:
parent
93e3a48004
commit
b38dea0597
25901 changed files with 9982925 additions and 369 deletions
102
gaia/shared/js/contacts/import/utilities/config.js
Executable file
102
gaia/shared/js/contacts/import/utilities/config.js
Executable file
|
|
@ -0,0 +1,102 @@
|
|||
'use strict';
|
||||
/* global Promise */
|
||||
|
||||
var utils = window.utils || {};
|
||||
|
||||
if (typeof utils.config === 'undefined') {
|
||||
(function() {
|
||||
var configs = {};
|
||||
var initializing = {};
|
||||
var initialized = {};
|
||||
|
||||
var EVENT_INITIALIZED = 'config_initialized';
|
||||
|
||||
utils.config = utils.config || {};
|
||||
|
||||
utils.config.reset = function() {
|
||||
configs = {};
|
||||
initializing = {};
|
||||
initialized = {};
|
||||
};
|
||||
|
||||
function onLoad(xhr, file, resolveCb) {
|
||||
configs[file] = xhr.response;
|
||||
|
||||
delete initializing[file];
|
||||
initialized[file] = true;
|
||||
|
||||
document.dispatchEvent(new CustomEvent(EVENT_INITIALIZED, {
|
||||
detail: {
|
||||
file: file,
|
||||
success: true
|
||||
}
|
||||
}));
|
||||
resolveCb(configs[file]);
|
||||
}
|
||||
|
||||
utils.config.load = function(file) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
var isInitialized = initialized[file];
|
||||
if (isInitialized === true) {
|
||||
resolve(configs[file]);
|
||||
return;
|
||||
}
|
||||
|
||||
var isInitializing = initializing[file];
|
||||
if (isInitializing) {
|
||||
var handler = function(expectedFile, e) {
|
||||
document.removeEventListener(EVENT_INITIALIZED, handler);
|
||||
var file = e.detail.file;
|
||||
if (expectedFile !== file) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.detail.success) {
|
||||
resolve(configs[file]);
|
||||
}
|
||||
else {
|
||||
reject();
|
||||
}
|
||||
}.bind(null, file);
|
||||
|
||||
document.addEventListener(EVENT_INITIALIZED, handler);
|
||||
return;
|
||||
}
|
||||
|
||||
initializing[file] = true;
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', file, true);
|
||||
xhr.responseType = 'json';
|
||||
|
||||
xhr.onload = function() {
|
||||
var resolvedCb = resolve;
|
||||
if (xhr.status !== 200) {
|
||||
resolvedCb = reject;
|
||||
}
|
||||
onLoad(xhr, file, resolvedCb);
|
||||
};
|
||||
|
||||
xhr.onerror = function() {
|
||||
document.dispatchEvent(new CustomEvent(EVENT_INITIALIZED, {
|
||||
detail: {
|
||||
file: file,
|
||||
success: false
|
||||
}
|
||||
}));
|
||||
delete initializing[file];
|
||||
reject(xhr.error);
|
||||
};
|
||||
|
||||
// If there is any exception we consider the file does not exist
|
||||
try {
|
||||
xhr.send(null);
|
||||
}
|
||||
catch(e) {
|
||||
console.error('Error while loading config file: ', e.message);
|
||||
onLoad({ status: 404 }, file, reject);
|
||||
}
|
||||
});
|
||||
};
|
||||
})();
|
||||
}
|
||||
Loading…
Reference in a new issue