vimium icon indicating copy to clipboard operation
vimium copied to clipboard

feat: add 'R' for hard reload

Open tribhuwan-kumar opened this issue 1 year ago • 1 comments

Added an advance command "R" in background_scripts/commands.js & a function for hard reloading in background_scripts/main.js. I updated the README.md as well. Checked in Google Chrome in Arch Linux, Everything is working fine!!

#4444

tribhuwan-kumar avatar Mar 28 '24 03:03 tribhuwan-kumar

Refactor hardReload Function

Changes:

  • Replaced the nested function with findIndex for immediate tab location.
  • Removed array reordering; implemented modulo looping for tab cycling.
  • Added exit condition for non-existent tabId to improve error handling.

Code Comparison:

Old Implementation:

function hardReload({ count, tabId, tab: { windowId } }) {
  const bypassCache = true;
  return chrome.tabs.query({ windowId }, function (tabs) {
    const position = (function () {
      for (let index = 0; index < tabs.length; index++) {
        const tab = tabs[index];
        if (tab.id === tabId) return index;
      }
    })();
    tabs = [...tabs.slice(position), ...tabs.slice(0, position)];
    count = Math.min(count, tabs.length);
    for (const tab of tabs.slice(0, count)) {
      chrome.tabs.reload(tab.id, { bypassCache });
    }
  });
}

New Implementation:

function hardReload({ count, tabId, tab: { windowId } }) {
  const bypassCache = true;
  return chrome.tabs.query({ windowId }, function (tabs) {
    let startIndex = tabs.findIndex(tab => tab.id === tabId);
    if (startIndex === -1) return; // Exit if tabId is not found
    let processed = 0;
    while (processed < count) {
      chrome.tabs.reload(tabs[startIndex].id, { bypassCache });
      processed++;
      startIndex = (startIndex + 1) % tabs.length; // Wrap around if needed
    }
  });
}

Vincent A. Barkman

VincentBarkman avatar May 08 '24 18:05 VincentBarkman

@tribhuwan-kumar I think this is a good feature, and your implementation is very similar to that of the regular reload. However, we now have a forCountTabs method that would be perfect for both of these. If @philc is okay with it, I propose we reimplement both regular reload and hard reload to use the forCountTabs method instead of reimplementing the count logic, and get this merged. If you want to do the re-implementation @tribhuwan-kumar, go for it. Otherwise, I'd be happy to do it for you and make a PR to your fork, or make my own PR, whichever you prefer. Just let me know what you think. This change could greatly simplify the implementation of both methods and make them more similar to the rest of the code. It also means they will work with Firefox hidden tabs, or any other future needs we build into the forCountTabs method.

UncleSnail avatar Jun 13 '24 16:06 UncleSnail

@UncleSnail SGTM

philc avatar Jun 13 '24 18:06 philc

@UncleSnail, It seems forCountTabs will be a great implementation. I liked your re-implementation idea using forCountTabs. Well, you can make a PR in any fork you want. It'll be your choice!

tribhuwan-kumar avatar Jun 15 '24 13:06 tribhuwan-kumar

@tribhuwan-kumar I made a PR to your repo a few weeks ago and just wanted to make sure you saw it. If you would prefer, I can make the PR directly to the main repository if you would like.

UncleSnail avatar Jun 26 '24 14:06 UncleSnail

@philc this should be ready to merge now, unless we want to consider changing the "R" explanation messages. Perhaps change the "ie ctrl+shift+r" to something like "(skip the cache)"?

UncleSnail avatar Jun 28 '24 12:06 UncleSnail

Looks good. Thanks for the contribution!

I made a few small cleanups in 5fc9348aa3866c7bd3f1fd40664abd5eb166ff6e.

philc avatar Jul 05 '24 04:07 philc

I just realized that we already have "hard reload" implemented. See #3735. It's just not mapped to a key by default.

The problem with the existing solution (adding the "hard" option to the reload command) is that it's not particularly discoverable. Even I'd forgotten about it. Although it is documented in the wiki.

I think it's a useful enough to have its own key by default (R) as this PR implements. What do you think?

philc avatar Jul 05 '24 04:07 philc

I think it's a useful enough to have its own key by default (R) as this PR implements. What do you think?

I agree, I think having "R" mapped to hard reload by default is a good idea. However, we don't want duplicate functionality in the codebase, so we should probably only have one correct way to implement or map/unmap hard reload. I can make a PR that tries to clean up any duplicate functionality if you would like.

UncleSnail avatar Jul 08 '24 16:07 UncleSnail

@UncleSnail That would be great.

philc avatar Jul 16 '24 05:07 philc

@UncleSnail That would be great.

I fixed this issue in this PR.

UncleSnail avatar Jul 16 '24 15:07 UncleSnail