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
download: false
doTimeout: false
invokeOrPostpone: false
# localization.js
template: false
t: false

View File

@ -287,10 +287,9 @@ function importFromString(jsonString) {
const message = {method: 'styleReplaceAll', styles};
if (tab.id == ownTab.id) {
applyOnMessage(message);
} else if (tab.id == activeTab.id) {
chrome.tabs.sendMessage(tab.id, message, ignoreChromeError);
} 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);
if (tab == lastTab) {

View File

@ -81,11 +81,7 @@ function notifyAllTabs(msg) {
]).then(([tabs, activeTab]) => {
const activeTabId = activeTab && activeTab.id;
for (const tab of tabs) {
if (tab.id === activeTabId) {
notifyTab(tab);
} else {
setTimeout(notifyTab, 0, tab);
}
invokeOrPostpone(tab.id === activeTabId, notifyTab, tab);
}
});
}
@ -366,3 +362,10 @@ function doTimeout(ms = 0, ...args) {
? () => new Promise(resolve => setTimeout(resolve, ms, ...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;
$('.delete', entry).onclick = handleEvent.delete;
if (postponeDetect) {
setTimeout(detectSloppyRegexps, 0, {entry, style});
} else {
detectSloppyRegexps({entry, style});
}
invokeOrPostpone(!postponeDetect, detectSloppyRegexps, {entry, style});
const oldElement = $('#style-' + style.id);
if (oldElement) {