Skip to content

Commit 8af25b4

Browse files
Merge pull request #7336 from Slowlife01/unloadtab
fix: unable to unload tab with auto tab unloader disabled
2 parents d0877eb + d6c30ca commit 8af25b4

File tree

1 file changed

+50
-42
lines changed

1 file changed

+50
-42
lines changed

src/browser/base/zen-components/ZenTabUnloader.mjs

+50-42
Original file line numberDiff line numberDiff line change
@@ -63,60 +63,23 @@
6363
static INTERVAL = 1000 * 60; // 1 minute
6464

6565
interval = null;
66+
/** @type {ZenTabUnloader} */
6667
unloader = null;
6768

68-
#excludedUrls = [];
69-
#compiledExcludedUrls = [];
70-
7169
constructor(unloader) {
7270
this.unloader = unloader;
7371
this.interval = setInterval(this.intervalListener.bind(this), ZenTabsIntervalUnloader.INTERVAL);
74-
this.#excludedUrls = this.lazyExcludeUrls;
75-
}
76-
77-
get lazyExcludeUrls() {
78-
return [
79-
...ZEN_TAB_UNLOADER_DEFAULT_EXCLUDED_URLS,
80-
...lazy.zenTabUnloaderExcludedUrls.split(',').map((url) => url.trim()),
81-
];
82-
}
83-
84-
arraysEqual(a, b) {
85-
if (a === b) return true;
86-
if (a == null || b == null) return false;
87-
if (a.length !== b.length) return false;
88-
89-
// If you don't care about the order of the elements inside
90-
// the array, you should sort both arrays here.
91-
// Please note that calling sort on an array will modify that array.
92-
// you might want to clone your array first.
93-
94-
for (var i = 0; i < a.length; ++i) {
95-
if (a[i] !== b[i]) return false;
96-
}
97-
return true;
98-
}
99-
100-
get excludedUrls() {
101-
// Check if excludedrls is the same as the pref value
102-
const excludedUrls = this.lazyExcludeUrls;
103-
if (!this.arraysEqual(this.#excludedUrls, excludedUrls) || !this.#compiledExcludedUrls.length) {
104-
this.#excludedUrls = excludedUrls;
105-
this.#compiledExcludedUrls = excludedUrls.map((url) => new RegExp(url));
106-
}
107-
return this.#compiledExcludedUrls;
10872
}
10973

11074
intervalListener() {
11175
if (!lazy.zenTabUnloaderEnabled) {
11276
return;
11377
}
11478
const currentTimestamp = Date.now();
115-
const excludedUrls = this.excludedUrls;
11679
const tabs = ZenWorkspaces.allStoredTabs;
11780
for (let i = 0; i < tabs.length; i++) {
11881
const tab = tabs[i];
119-
if (this.unloader.canUnloadTab(tab, currentTimestamp, excludedUrls)) {
82+
if (this.unloader.canUnloadTab(tab, currentTimestamp)) {
12083
this.unloader.unload(tab);
12184
}
12285
}
@@ -126,8 +89,14 @@
12689
class ZenTabUnloader extends ZenDOMOperatedFeature {
12790
static ACTIVITY_MODIFIERS = ['muted', 'soundplaying', 'label', 'attention'];
12891

92+
#excludedUrls = [];
93+
#compiledExcludedUrls = [];
94+
#lastCheckedUrlTimestamp = 0;
95+
12996
constructor() {
13097
super();
98+
99+
this.#excludedUrls = this.lazyExcludeUrls;
131100
if (!lazy.zenTabUnloaderEnabled) {
132101
return;
133102
}
@@ -217,6 +186,45 @@
217186
document.getElementById('context_closeDuplicateTabs').parentNode.appendChild(element);
218187
}
219188

189+
get lazyExcludeUrls() {
190+
return [
191+
...ZEN_TAB_UNLOADER_DEFAULT_EXCLUDED_URLS,
192+
...lazy.zenTabUnloaderExcludedUrls.split(',').map((url) => url.trim()),
193+
];
194+
}
195+
196+
arraysEqual(a, b) {
197+
if (a === b) return true;
198+
if (a == null || b == null) return false;
199+
if (a.length !== b.length) return false;
200+
201+
const currentTimestamp = Date.now();
202+
if (currentTimestamp - this.#lastCheckedUrlTimestamp < 5 * 1000) {
203+
return true;
204+
}
205+
206+
this.#lastCheckedUrlTimestamp = currentTimestamp;
207+
// If you don't care about the order of the elements inside
208+
// the array, you should sort both arrays here.
209+
// Please note that calling sort on an array will modify that array.
210+
// you might want to clone your array first.
211+
212+
for (const i = 0; i < a.length; ++i) {
213+
if (a[i] !== b[i]) return false;
214+
}
215+
return true;
216+
}
217+
218+
get excludedUrls() {
219+
// Check if excludedrls is the same as the pref value
220+
const excludedUrls = this.lazyExcludeUrls;
221+
if (!this.arraysEqual(this.#excludedUrls, excludedUrls) || !this.#compiledExcludedUrls.length) {
222+
this.#excludedUrls = excludedUrls;
223+
this.#compiledExcludedUrls = excludedUrls.map((url) => new RegExp(url));
224+
}
225+
return this.#compiledExcludedUrls;
226+
}
227+
220228
unload(tab, skipPermitUnload = false) {
221229
gBrowser.explicitUnloadTabs([tab], skipPermitUnload);
222230
tab.removeAttribute('linkedpanel');
@@ -229,7 +237,7 @@
229237

230238
explicitUnloadTabs(tabs, extraArgs = {}) {
231239
for (let i = 0; i < tabs.length; i++) {
232-
if (this.canUnloadTab(tabs[i], Date.now(), this.intervalUnloader.excludedUrls, true, extraArgs)) {
240+
if (this.canUnloadTab(tabs[i], Date.now(), true, extraArgs)) {
233241
this.unload(tabs[i], true);
234242
}
235243
}
@@ -251,7 +259,7 @@
251259
}
252260
}
253261

254-
canUnloadTab(tab, currentTimestamp, excludedUrls, ignoreTimestamp = false, extraArgs = {}) {
262+
canUnloadTab(tab, currentTimestamp, ignoreTimestamp = false, extraArgs = {}) {
255263
if (
256264
(tab.pinned && !ignoreTimestamp) ||
257265
tab.selected ||
@@ -266,7 +274,7 @@
266274
(tab.pictureinpicture && !ignoreTimestamp) ||
267275
(tab.soundPlaying && !ignoreTimestamp) ||
268276
(tab.zenIgnoreUnload && !ignoreTimestamp) ||
269-
(excludedUrls.some((url) => url.test(tab.linkedBrowser?.currentURI.spec)) &&
277+
(this.excludedUrls.some((url) => url.test(tab.linkedBrowser?.currentURI.spec)) &&
270278
tab.linkedBrowser?.currentURI.spec !== 'about:blank')
271279
) {
272280
return false;

0 commit comments

Comments
 (0)