startup storage sync is now a promise

This commit is contained in:
Ilya Ig. Petrov 2016-01-26 20:50:25 +05:00
parent 9420177df2
commit 278aaad865

View File

@ -60,7 +60,7 @@ window.antiCensorRu = {
if (Object.getOwnPropertyDescriptor(this, key).writable && typeof(this[key]) !== 'function') if (Object.getOwnPropertyDescriptor(this, key).writable && typeof(this[key]) !== 'function')
onlySettable[key] = this[key] onlySettable[key] = this[key]
return chrome.storage.local.set(onlySettable, cb); return chrome.storage.local.set(onlySettable, () => cb(chrome.runtime.lastError, onlySettable) );
}, },
pullFromStorage(cb) { pullFromStorage(cb) {
@ -68,8 +68,9 @@ window.antiCensorRu = {
for(var key of Object.keys(storage)) for(var key of Object.keys(storage))
this[key] = storage[key]; this[key] = storage[key];
console.log('Pulled from storage, any callback?', !!cb);
if (cb) if (cb)
cb(storage); cb(chrome.runtime.lastError, storage);
}); });
}, },
@ -127,35 +128,16 @@ window.antiCensorRu = {
}; };
window.ifPulled = false; // ON EACH LAUNCH OF EXTENSION
window.onPulledFuns = [];
function pullFinished() {
window.onPulledFuns.map( fun => fun() );
window.ifPulled = true;
}
function execAfterPulled(fun) {
if (window.ifPulled)
fun();
else
window.onPulledFuns.push( fun );
}
chrome.runtime.onInstalled.addListener( details => { window.storageSyncedPromise = new Promise(
console.log('Installing, reason:', details.reason); (resolve, reject) => window.antiCensorRu.pullFromStorage(
switch(details.reason) { (err, res) => err ? reject(err) : resolve(res)
case 'update': )
return execAfterPulled( () => window.antiCensorRu.installPac() ); );
case 'install':
return execAfterPulled( () => {
window.antiCensorRu.ifNotInstalled = true;
chrome.runtime.openOptionsPage();
} );
}
});
window.antiCensorRu.pullFromStorage( () => {
console.log('Pulled from storage.');
window.storageSyncedPromise.then(
storage => {
chrome.alarms.onAlarm.addListener( chrome.alarms.onAlarm.addListener(
alarm => { alarm => {
if (alarm.name === window.antiCensorRu._periodicUpdateAlarmReason) { if (alarm.name === window.antiCensorRu._periodicUpdateAlarmReason) {
@ -165,8 +147,23 @@ window.antiCensorRu.pullFromStorage( () => {
} }
); );
console.log('Installed alarm listener.'); console.log('Installed alarm listener.');
}
);
return pullFinished(); chrome.runtime.onInstalled.addListener( details => {
console.log('Installing, reason:', details.reason);
window.storageSyncedPromise.then(
storage => {
switch(details.reason) {
case 'update':
window.antiCensorRu.installPac();
break;
case 'install':
window.antiCensorRu.ifNotInstalled = true;
chrome.runtime.openOptionsPage();
}
}
)
}); });
// PRIVATE // PRIVATE