own page load microopt: reduce blocking on prefs

postpone noncritical init stuff to window.load because first access to chrome API takes time to initialize API bindings
This commit is contained in:
tophf 2017-04-17 18:08:49 +03:00
parent eccabb8f27
commit 4eae87e606

View File

@ -146,54 +146,69 @@ var prefs = new function Prefs() {
} else { } else {
value = defaultValue; value = defaultValue;
} }
this.set(key, value, {noBroadcast: true}); if (BG == window) {
// when in bg page, .set() will write to localStorage
this.set(key, value, {noBroadcast: true, noSync: true});
} else {
values[key] = value;
defineReadonlyProperty(this.readOnlyValues, key, value);
}
} }
getSync().get('settings', ({settings: synced} = {}) => { // any access to chrome API takes time due to initialization of bindings
if (synced) { let lazyInit = () => {
for (const key in defaults) { window.removeEventListener('load', lazyInit);
if (key == 'popupWidth' && synced[key] != values.popupWidth) { lazyInit = null;
// this is a fix for the period when popupWidth wasn't synced
// TODO: remove it in a couple of months
continue;
}
if (key in synced) {
this.set(key, synced[key], {noSync: true});
}
}
}
if (typeof contextMenus !== 'undefined') {
for (const id in contextMenus) {
if (typeof values[id] == 'boolean') {
this.broadcast(id, values[id], {noSync: true});
}
}
}
});
chrome.storage.onChanged.addListener((changes, area) => { getSync().get('settings', ({settings: synced} = {}) => {
if (area == 'sync' && 'settings' in changes) {
const synced = changes.settings.newValue;
if (synced) { if (synced) {
for (const key in defaults) { for (const key in defaults) {
if (key == 'popupWidth' && synced[key] != values.popupWidth) {
// this is a fix for the period when popupWidth wasn't synced
// TODO: remove it in a couple of months
continue;
}
if (key in synced) { if (key in synced) {
this.set(key, synced[key], {noSync: true}); this.set(key, synced[key], {noSync: true});
} }
} }
} else {
// user manually deleted our settings, we'll recreate them
getSync().set({'settings': values});
} }
} if (typeof contextMenus !== 'undefined') {
}); for (const id in contextMenus) {
if (typeof values[id] == 'boolean') {
this.broadcast(id, values[id], {noSync: true});
}
}
}
});
chrome.runtime.onMessage.addListener(msg => { chrome.storage.onChanged.addListener((changes, area) => {
if (msg.prefs) { if (area == 'sync' && 'settings' in changes) {
for (const id in msg.prefs) { const synced = changes.settings.newValue;
this.set(id, msg.prefs[id], {noBroadcast: true, noSync: true}); if (synced) {
for (const key in defaults) {
if (key in synced) {
this.set(key, synced[key], {noSync: true});
}
}
} else {
// user manually deleted our settings, we'll recreate them
getSync().set({'settings': values});
}
} }
} });
});
chrome.runtime.onMessage.addListener(msg => {
if (msg.prefs) {
for (const id in msg.prefs) {
this.set(id, msg.prefs[id], {noBroadcast: true, noSync: true});
}
}
});
};
window.addEventListener('load', lazyInit);
return;
function doBroadcast() { function doBroadcast() {
const affects = {all: 'disableAll' in broadcastPrefs}; const affects = {all: 'disableAll' in broadcastPrefs};