<commit_before>require("sdk/simple-prefs").on("", preloadTabs);
require("sdk/tabs").on("activate", preloadTabsActivate);

function preloadTabsActivate(tab) {
  // wait until active tab is ready before loading neighboring tabs
  tab.on("ready", preloadTabs);
}

function preloadTabs() {
  var tU = require("tabs/utils");
  var wU = require("window/utils");

  var activeXULWindow = wU.getMostRecentBrowserWindow();
  var currentXULTab = tU.getActiveTab(activeXULWindow);
  
  var xulTabs = tU.getTabs(activeXULWindow);
  var currentTabIndex = 0;

  // find current XUL tab
  for(var i = 0; i < xulTabs.length; i++) {
    var xt = xulTabs[i];
    if (xt == currentXULTab) {
      currentTabIndex = i;
      break;
    }
  }

  var leftOffset  = require('sdk/simple-prefs').prefs['leftOffset'];
  var rightOffset = require('sdk/simple-prefs').prefs['rightOffset'];
  
  var startIndex = Math.max(0, currentTabIndex - leftOffset);
  var stopIndex  = Math.min(xulTabs.length, currentTabIndex + rightOffset + 1);
 
  for (var i = startIndex; i < stopIndex; i++) {
    var xt = xulTabs[i];
    if (i != currentTabIndex && xt && xt.hasAttribute("pending") && 
        !xt.hasAttribute("unread") 
      ) {
      // console.log("reload: " + i);
      tU.getTabBrowserForTab(xt).reloadTab(xt);
    }
  }<commit_msg>Immediately preload neighboring tabs if the current tab is up-to-date<commit_after>require("sdk/simple-prefs").on("", preloadTabs);
require("sdk/tabs").on("activate", preloadTabsActivate);
var tU = require("tabs/utils");
var wU = require("window/utils");

function preloadTabsActivate(tab) {
  var activeXULWindow = wU.getMostRecentBrowserWindow();
  var currentXULTab = tU.getActiveTab(activeXULWindow);

  // if tab has already been loaded start preloading neighbors immediately
  tab.attach({
    contentScript: 'self.postMessage(document.readyState);',
    onMessage: function (readyState) {
      if (readyState == "complete") {
        preloadTabs(activeXULWindow, currentXULTab);
      }
    }
  });

  // wait until active tab is ready before loading neighboring tabs
  tab.on("ready", function(tab) {
    preloadTabs(activeXULWindow, currentXULTab);
  });
  //  require("sdk/timers").setTimeout(preloadTabs, 100, activeXULWindow, currentXULTab);
}

function preloadTabs(activeXULWindow, currentXULTab) {

//  var activeXULWindow = wU.getMostRecentBrowserWindow();
//  var currentXULTab = tU.getActiveTab(activeXULWindow);
  
  var xulTabs = tU.getTabs(activeXULWindow);
  var currentTabIndex = 0;

  var foundActiveTab = false;
  // find current XUL tab
  for(var i = 0; i < xulTabs.length; i++) {
    var xt = xulTabs[i];
    if (xt == currentXULTab) {
      currentTabIndex = i;
      foundActiveTab = true;
      break;
    }
  }
  
  if (!foundActiveTab)
    return;

  var leftOffset  = require('sdk/simple-prefs').prefs['leftOffset'];
  var rightOffset = require('sdk/simple-prefs').prefs['rightOffset'];
  
  var startIndex = Math.max(0, currentTabIndex - leftOffset);
  var stopIndex  = Math.min(xulTabs.length, currentTabIndex + rightOffset + 1);
 
  for (var i = startIndex; i < stopIndex; i++) {
    var xt = xulTabs[i];
    if (i != currentTabIndex && xt && xt.hasAttribute("pending") && 
        !xt.hasAttribute("unread") 
      ) {
      // console.log("reload: " + i);
      tU.getTabBrowserForTab(xt).reloadTab(xt);
    }
  }
{'fraction_non_alphanumeric': 0.14153754469606675, 'fraction_numerical': 0.0032777115613825984, 'mean_word_length': 3.7348377997179125, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '166873', 'n_tokens_mistral': 1161, 'n_tokens_neox': 1135, 'n_words': 237}