From 4eae87e6063762a98b1023069ed393559ed9f043 Mon Sep 17 00:00:00 2001 From: tophf Date: Mon, 17 Apr 2017 18:08:49 +0300 Subject: [PATCH] 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 --- prefs.js | 87 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 36 deletions(-) diff --git a/prefs.js b/prefs.js index f3e817d6..dfa94ac8 100644 --- a/prefs.js +++ b/prefs.js @@ -146,54 +146,69 @@ var prefs = new function Prefs() { } else { 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} = {}) => { - if (synced) { - 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) { - 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}); - } - } - } - }); + // any access to chrome API takes time due to initialization of bindings + let lazyInit = () => { + window.removeEventListener('load', lazyInit); + lazyInit = null; - chrome.storage.onChanged.addListener((changes, area) => { - if (area == 'sync' && 'settings' in changes) { - const synced = changes.settings.newValue; + getSync().get('settings', ({settings: synced} = {}) => { if (synced) { 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) { 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 => { - if (msg.prefs) { - for (const id in msg.prefs) { - this.set(id, msg.prefs[id], {noBroadcast: true, noSync: true}); + chrome.storage.onChanged.addListener((changes, area) => { + if (area == 'sync' && 'settings' in changes) { + const synced = changes.settings.newValue; + 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() { const affects = {all: 'disableAll' in broadcastPrefs};