<commit_before>var paused = true;
var queue = [];

function beforeSend(xhr) {
  window.addEventListener('beforeunload', function() {
    xhr.abort();
    queue = [];
  });
}

function doAjax(options, retries, dfr) {
  var opt;
  if (typeof options === 'string') {
    opt = {url: options};
  } else {
    opt = options;
  }
  opt.beforeSend = beforeSend;
  return $.ajax(opt).pipe(dfr.resolve,
    function(jqXhr, textStatus, errorThrown) {
      if (retries > 0 && jqXhr.status === 503) {
        setTimeout(doAjax, 100, opt, retries - 1, dfr);
      } else {
        dfr.reject(jqXhr, textStatus, errorThrown);
      }
    }
  );
}

function taskRunner() {
  if (queue.length === 0) {
    paused = true;
  } else {
    paused = false;
    if ($.active < 4) {
      var opts = queue.shift();
      doAjax.apply(null, opts);
      taskRunner();
    }
  }
}

function add(options, retries, dfr) {
  queue.push([options, retries, dfr]);
  if (paused) {taskRunner();}
}

export default function retryAjax(options) {
  var dfr = $.Deferred();
  add(options, 10, dfr);
  return dfr.promise();
}
<commit_msg>protect retryAjax<commit_after>var paused = true;
var queue = [];

function beforeSend(xhr) {
  window.addEventListener('beforeunload', function() {
    xhr.abort();
    queue = [];
  });
}

function doAjax(options, retries, dfr) {
  var opt;
  if (typeof options === 'string') {
    opt = {url: options};
  } else {
    opt = options;
  }
  opt.beforeSend = beforeSend;
  return $.ajax(opt).pipe(dfr.resolve,
    function(jqXhr, textStatus, errorThrown) {
      if (retries > 0 && jqXhr.status === 503) {
        setTimeout(doAjax, 100, opt, retries - 1, dfr);
      } else {
        dfr.reject(jqXhr, textStatus, errorThrown);
      }
    }
  );
}

function taskRunner() {
  if (queue.length === 0) {
    paused = true;
  } else {
    paused = false;
    if ($.active < 4) {
      var opts = queue.shift();
      doAjax.apply(null, opts);
      taskRunner();
    }
  }
}

function add(options, retries, dfr) {
  queue.push([options, retries, dfr]);
  if (paused) {taskRunner();}
}

export default function retryAjax(options) {
  var dfr = $.Deferred();
  if (options) {add(options, 10, dfr);}
  return dfr.promise();
}

{'fraction_non_alphanumeric': 0.16464237516869096, 'fraction_numerical': 0.010796221322537112, 'mean_word_length': 2.8745644599303137, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1438738', 'n_tokens_mistral': 827, 'n_tokens_neox': 803, 'n_words': 183}