code cosmetics: add invokeOrPostpone()

This commit is contained in:
tophf 2017-06-27 15:15:49 +03:00
parent 1c1a9f2804
commit 358e8e034f
4 changed files with 12 additions and 13 deletions

View File

@ -38,6 +38,7 @@ globals:
sessionStorageHash: false sessionStorageHash: false
download: false download: false
doTimeout: false doTimeout: false
invokeOrPostpone: false
# localization.js # localization.js
template: false template: false
t: false t: false

View File

@ -287,10 +287,9 @@ function importFromString(jsonString) {
const message = {method: 'styleReplaceAll', styles}; const message = {method: 'styleReplaceAll', styles};
if (tab.id == ownTab.id) { if (tab.id == ownTab.id) {
applyOnMessage(message); applyOnMessage(message);
} else if (tab.id == activeTab.id) {
chrome.tabs.sendMessage(tab.id, message, ignoreChromeError);
} else { } else {
setTimeout(chrome.tabs.sendMessage, 0, tab.id, message, ignoreChromeError); invokeOrPostpone(tab.id == activeTab.id,
chrome.tabs.sendMessage, tab.id, message, ignoreChromeError);
} }
setTimeout(BG.updateIcon, 0, tab, styles); setTimeout(BG.updateIcon, 0, tab, styles);
if (tab == lastTab) { if (tab == lastTab) {

View File

@ -81,11 +81,7 @@ function notifyAllTabs(msg) {
]).then(([tabs, activeTab]) => { ]).then(([tabs, activeTab]) => {
const activeTabId = activeTab && activeTab.id; const activeTabId = activeTab && activeTab.id;
for (const tab of tabs) { for (const tab of tabs) {
if (tab.id === activeTabId) { invokeOrPostpone(tab.id === activeTabId, notifyTab, tab);
notifyTab(tab);
} else {
setTimeout(notifyTab, 0, tab);
}
} }
}); });
} }
@ -366,3 +362,10 @@ function doTimeout(ms = 0, ...args) {
? () => new Promise(resolve => setTimeout(resolve, ms, ...args)) ? () => new Promise(resolve => setTimeout(resolve, ms, ...args))
: new Promise(resolve => setTimeout(resolve, 0, ...args)); : new Promise(resolve => setTimeout(resolve, 0, ...args));
} }
function invokeOrPostpone(isInvoke, fn, ...args) {
return isInvoke
? fn(...args)
: setTimeout(invokeOrPostpone, 0, true, fn, ...args);
}

View File

@ -246,11 +246,7 @@ function createStyleElement({
$('.disable', entry).onclick = handleEvent.toggle; $('.disable', entry).onclick = handleEvent.toggle;
$('.delete', entry).onclick = handleEvent.delete; $('.delete', entry).onclick = handleEvent.delete;
if (postponeDetect) { invokeOrPostpone(!postponeDetect, detectSloppyRegexps, {entry, style});
setTimeout(detectSloppyRegexps, 0, {entry, style});
} else {
detectSloppyRegexps({entry, style});
}
const oldElement = $('#style-' + style.id); const oldElement = $('#style-' + style.id);
if (oldElement) { if (oldElement) {