Skip to content

Commit

Permalink
Replace codes using setTimeout to nextFrame()
Browse files Browse the repository at this point in the history
piroor committed Feb 2, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent efac09f commit f8c2de6
Showing 6 changed files with 27 additions and 21 deletions.
9 changes: 5 additions & 4 deletions webextensions/background/api-tabs-listener.js
Original file line number Diff line number Diff line change
@@ -30,7 +30,8 @@ import {
log as internalLogger,
dumpTab,
toLines,
configs
configs,
nextFrame,
} from '/common/common.js';
import * as ApiTabs from '/common/api-tabs.js';
import * as Constants from '/common/constants.js';
@@ -581,11 +582,11 @@ async function onNewTabTracked(tab, info) {

if (TabsStore.ensureLivingTab(tab)) { // it can be removed while waiting
window.openingTabs.add(tab.id);
setTimeout(() => {
nextFrame.then(() => {
if (!TabsStore.windows.get(tab.windowId)) // it can be removed while waiting
return;
window.openingTabs.delete(tab.id);
}, 0);
});
}

if (!TabsStore.ensureLivingTab(tab)) { // it can be removed while waiting
@@ -1024,7 +1025,7 @@ async function onAttached(tabId, attachInfo) {
attachInfo.$TST_retryCount = 0;
if (attachInfo.$TST_retryCount < 10) {
attachInfo.$TST_retryCount++;
setTimeout(onAttached, 100, tabId, attachInfo);
nextFrame.then(() => onAttached(tabId, attachInfo));
return;
}
console.log(`tabs.onAttached: the tab ${tabId} or the window ${attachInfo.newWindowId} is already closed. `);
7 changes: 4 additions & 3 deletions webextensions/background/background.js
Original file line number Diff line number Diff line change
@@ -11,7 +11,8 @@ import {
log as internalLogger,
wait,
configs,
sanitizeForHTMLText
sanitizeForHTMLText,
nextFrame,
} from '/common/common.js';
import * as ApiTabs from '/common/api-tabs.js';
import * as Constants from '/common/constants.js';
@@ -575,14 +576,14 @@ export async function confirmToCloseTabs(tabs, { windowId, configKey, messageKey
url: '/resources/blank.html', // for popup, required on Firefox ESR68
title: browser.i18n.getMessage(titleKey || 'warnOnCloseTabs_title'), // for popup
onShownInPopup(container) {
setTimeout(() => {
nextFrame().then(() => {
// this need to be done on the next tick, to use the height of
// the box for calculation of dialog size
const style = container.querySelector('ul').style;
style.height = '0px'; // this makes the box shrinkable
style.maxHeight = 'none';
style.minHeight = '0px';
}, 0);
});
}
});

15 changes: 8 additions & 7 deletions webextensions/background/context-menu.js
Original file line number Diff line number Diff line change
@@ -7,7 +7,8 @@

import {
log as internalLogger,
configs
configs,
nextFrame,
} from '/common/common.js';

import * as ApiTabs from '/common/api-tabs.js';
@@ -311,15 +312,15 @@ addBookmarkItems.done = false;
TabContextMenu.onTopLevelItemAdded.addListener(reserveToRefreshItems);

function reserveToRefreshItems() {
if (reserveToRefreshItems.reserved)
clearTimeout(reserveToRefreshItems.reserved);
reserveToRefreshItems.reserved = setTimeout(() => {
reserveToRefreshItems.reserved = null;
const startAt = `${Date.now()}-${parseInt(Math.random() * 65000)}`;
reserveToRefreshItems.lastStartedAt = startAt;
nextFrame().then(() => {
if (reserveToRefreshItems.lastStartedAt != startAt)
return;
addTabItems();
addBookmarkItems();
}, 100);
});
}
reserveToRefreshItems.reserved = null;

function updateItem(id, params) {
browser.menus.update(id, params).catch(ApiTabs.createErrorSuppressor());
5 changes: 3 additions & 2 deletions webextensions/background/handle-tab-bunches.js
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import {
configs,
sanitizeForHTMLText,
compareAsNumber,
nextFrame,
} from '/common/common.js';
import * as ApiTabs from '/common/api-tabs.js';
import * as Bookmark from '/common/bookmark.js';
@@ -229,12 +230,12 @@ async function confirmToAutoGroupNewTabsFromOthers(tabs) {
url: '/resources/blank.html', // for popup, required on Firefox ESR68
title: browser.i18n.getMessage('warnOnAutoGroupNewTabs_title'), // for popup
onShownInPopup(container) {
setTimeout(() => { // this need to be done on the next tick, to use the height of the box for calculation of dialog size
nextFrame().then(() => { // this need to be done on the next tick, to use the height of the box for calculation of dialog size
const style = container.querySelector('ul').style;
style.height = '0px'; // this makes the box shrinkable
style.maxHeight = 'none';
style.minHeight = '0px';
}, 0);
});
}
});

7 changes: 4 additions & 3 deletions webextensions/background/tree-structure.js
Original file line number Diff line number Diff line change
@@ -12,7 +12,8 @@ import {
dumpTab,
toLines,
configs,
wait
wait,
nextFrame,
} from '/common/common.js';
import * as ApiTabs from '/common/api-tabs.js';
import * as Constants from '/common/constants.js';
@@ -444,9 +445,9 @@ Tab.onRestored.addListener(tab => {
if (count == 0) {
mRestoringTabs.delete(tab.windowId);
mMaxRestoringTabs.delete(tab.windowId);
setTimeout(() => { // unblock in the next event loop, after other asynchronous operations are finished
nextFrame().then(() => { // unblock in the next event loop, after other asynchronous operations are finished
UserOperationBlocker.unblockIn(tab.windowId, { throbber: true });
}, 0);
});

const countToBeRestored = mRecentlyClosedTabs.filter(tab => !mRestoredTabIds.has(tab.uniqueId));
log('countToBeRestored: ', countToBeRestored);
5 changes: 3 additions & 2 deletions webextensions/common/Tab.js
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ import {
sanitizeForRegExpSource,
isNewTabCommandTab,
configs,
nextFrame,
} from './common.js';

import * as ApiTabs from '/common/api-tabs.js';
@@ -172,15 +173,15 @@ export default class Tab {
element.apiTab = this.tab;
this.element = element;
this.classList = element.classList;
setTimeout(() => { // wait until initialization processes are completed
nextFrame().then(() => { // wait until initialization processes are completed
this._promisedElementResolver(element);
if (!element) { // reset for the next binding
this.promisedElement = new Promise((resolve, _reject) => {
this._promisedElementResolver = resolve;
});
}
Tab.onElementBound.dispatch(this.tab);
}, 0);
});
}

unbindElement() {

0 comments on commit f8c2de6

Please sign in to comment.