Skip to content

Commit

Permalink
♻️ Refactor tab specific events to inherit from TabCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
kierandrewett committed Nov 18, 2023
1 parent cec1459 commit eb8dd36
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 33 deletions.
55 changes: 22 additions & 33 deletions components/commands/content/reload-tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,20 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const { Command } = ChromeUtils.importESModule(
"resource://gre/modules/Command.sys.mjs"
const { TabCommand } = ChromeUtils.importESModule(
"resource://gre/modules/TabCommand.sys.mjs"
);

var { BrowserTabsUtils } = ChromeUtils.importESModule(
"resource://gre/modules/BrowserTabsUtils.sys.mjs"
);

export class ReloadTabCommand extends Command {
export class ReloadTabCommand extends TabCommand {
constructor(subscription, area) {
super(subscription, area);

this.label = "Reload";
this.icon = "reload";

this.context.window.addEventListener(
"BrowserTabs::BrowserStateChange",
this,
{ signal: this.abortController.signal }
);
this.context.window.addEventListener("BrowserTabs::TabSelect", this, {
signal: this.abortController.signal
});
}

_reloadTimer = null;
Expand All @@ -35,12 +26,16 @@ export class ReloadTabCommand extends Command {
* @param {{ bypassCache?: boolean }} args
*/
run(args) {
this.actions.run(
this.isLoading
? "browser.tabs.stop_page"
: "browser.tabs.reload_page",
{ tab: this.context.tab, bypassCache: args?.bypassCache }
);
const actionId = this.isLoading
? "browser.tabs.stop_page"
: "browser.tabs.reload_page";

this.reloadClicked = actionId == "browser.tabs.reload_page";

this.actions.run(actionId, {
tab: this.context.tab,
bypassCache: args?.bypassCache
});
}

/**
Expand All @@ -52,7 +47,13 @@ export class ReloadTabCommand extends Command {
* @param {number} data.stateFlags
* @param {string} data.status
*/
onStateChanged({ browser, webProgress, request, stateFlags, status }) {
onContextualBrowserStateChanged({
browser,
webProgress,
request,
stateFlags,
status
}) {
if (browser != this.context.browser) return;

const { STATE_START, STATE_IS_NETWORK, STATE_STOP } =
Expand Down Expand Up @@ -84,6 +85,7 @@ export class ReloadTabCommand extends Command {

// Only runs when the browser has successfully finished loaded the document
if (
this.reloadClicked &&
isTopLevel &&
stateFlags & STATE_STOP &&
stateFlags & STATE_IS_NETWORK &&
Expand All @@ -93,25 +95,12 @@ export class ReloadTabCommand extends Command {
) {
// Prevent accidental clicks to reload the page
// after we have swapped from the stop state

this.reloadClicked = false;
this.disabled = true;

this._reloadTimer = this.window.setTimeout(() => {
this.disabled = false;
}, 650);
}
}

/**
* Handles incoming events to the command
* @param {Event} event
*/
handleEvent(event) {
switch (event.type) {
case "BrowserTabs::BrowserStateChange":
case "BrowserTabs::TabSelect":
this.onStateChanged(/** @type {CustomEvent} */ (event).detail);
break;
}
}
}
1 change: 1 addition & 0 deletions modules_registry.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const registry = {
BrowserCommands: "components/commands/BrowserCommands.sys.mjs",
Command: "components/commands/Command.sys.mjs",
CommandSubscription: "components/commands/CommandSubscription.sys.mjs",
TabCommand: "components/commands/TabCommand.sys.mjs",

BrowserCompatibility: "components/compat/BrowserCompatibility.sys.mjs",

Expand Down

0 comments on commit eb8dd36

Please sign in to comment.