Skip to content

Commit

Permalink
Optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Nov 9, 2023
1 parent b6b7b77 commit eb23855
Showing 1 changed file with 47 additions and 20 deletions.
67 changes: 47 additions & 20 deletions common/Context.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,46 @@ export class Context {
if (this.resolved)
return;

try {
const [childTabs] = await Promise.all([
browser.tabs.query({
windowId: this.tab.windowId,
openerTabId: this.tab.id,
hidden: false,
}),
this.resolveMultiselectedTabs(),
]);
this.childTabs = childTabs.filter(tab => tab.openerTabId && tab.openerTabId != tab.id);
}
catch(error) {
console.log('failed to get child tabs: fallback to all tabs ', error);
await this.resolveAllTabs();
}

this.resolved = true;
}

async resolveMultiselectedTabs() {
if (this.multiselectedTabs)
return;
this.multiselectedTabs = !this.multiselectedTabs && (
this.tab.highlighted ?
browser.tabs.query({
windowId: this.tab.windowId,
highlighted: true,
hidden: false,
}) :
[this.tab]
);
}

async resolveDescendantTabs() {
if (this.$descendantTabs)
return;

if (!this.resolved)
await this.resolve();

try {
const collectDescendants = async tab => {
const childTabs = await browser.tabs.query({
Expand All @@ -41,29 +81,16 @@ export class Context {
childTabs.map(async childTab => [childTab, ...(await collectDescendants(childTab))])
)).flat().filter(tab => tab.openerTabId && tab.openerTabId != tab.id);
};
const [descendantTabs, multiselectedTabs] = await Promise.all([
const [descendantTabs] = await Promise.all([
collectDescendants(this.tab),
!this.multiselectedTabs && (
this.tab.highlighted ?
browser.tabs.query({
windowId: this.tab.windowId,
highlighted: true,
hidden: false,
}) :
[this.tab]
),
this.resolveMultiselectedTabs(),
]);
this.descendantTabs = descendantTabs;
this.descendantIds = new Set(descendantTabs.map(tab => tab.id));
if (!this.multiselectedTabs)
this.multiselectedTabs = multiselectedTabs;
}
catch(error) {
console.log('failed to get child tabs: fallback to all tabs ', error);
await this.resolveAllTabs();
console.log('failed to get descendant tabs: ', error);
}

this.resolved = true;
}

async resolveAllTabs() {
Expand Down Expand Up @@ -103,7 +130,7 @@ export class Context {
if ('$isTreeParent' in this)
return this.$isTreeParent;

return this.$isTreeParent = this.descendantIds.size > 0;
return this.$isTreeParent = (this.childTabs ? this.childTabs.length : this.descendantIds.size) > 0;
}

get shouldCopyOnlyDescendants() {
Expand All @@ -126,7 +153,7 @@ export class Context {

return this.$shouldCopyMultipleTabs = (
(this.isTreeParent &&
[...(this.shouldCopyOnlyDescendants ? [] : [this.tab]), ...this.descendantIds]) ||
[...(this.shouldCopyOnlyDescendants ? [] : [this.tab]), ...(this.childTabs || this.descendantIds)]) ||
this.multiselectedTabs
).length > 1;
}
Expand Down Expand Up @@ -165,8 +192,8 @@ export class Context {
if (this.$tabsToCopy)
return this.$tabsToCopy;

if (!this.resolved)
await this.resolve();
await this.resolveDescendantTabs();

if (this.shouldCopyAll)
await this.resolveAllTabs();

Expand Down

0 comments on commit eb23855

Please sign in to comment.