Public
Snippet $17 authored by Rafael Monnerat

redirect.js

redirect.js
/*jslint indent: 2, maxlen: 80, nomen: true */
/*global console, window, document, RSVP  */
(function (window, document) {
  "use strict";
   var result,
     promise_list = [],
     test_dict = {}, 
     test_url_list = [
      "https://frontend0.grandenet.cn/notfound.html",
      "https://frontend1.grandenet.cn/notfound.html",
      "https://frontend3.grandenet.cn/notfound.html",
      "https://frontend4.grandenet.cn/notfound.html",
      "https://frontend5.grandenet.cn/notfound.html",
      "https://frontend6.grandenet.cn/notfound.html",
      "https://frontend7.grandenet.cn/notfound.html"]

  function ajax(param) {
    var xhr = new XMLHttpRequest();
    return new RSVP.Promise(function (resolve, reject, notify) {
      var k;
      xhr.open(param.type || "GET", param.url, true);
      xhr.responseType = param.dataType || "";
      if (typeof param.headers === 'object' && param.headers !== null) {
        for (k in param.headers) {
          if (param.headers.hasOwnProperty(k)) {
            xhr.setRequestHeader(k, param.headers[k]);
          }
        }
      }
      xhr.addEventListener("load", function (e) {
        if (e.target.status >= 400) {
          return reject(e);
        }
        resolve(e);
      });
      xhr.addEventListener("error", reject);
      xhr.addEventListener("progress", notify);
      if (typeof param.xhrFields === 'object' && param.xhrFields !== null) {
        for (k in param.xhrFields) {
          if (param.xhrFields.hasOwnProperty(k)) {
            xhr[k] = param.xhrFields[k];
          }
        }
      }
      if (typeof param.beforeSend === 'function') {
        param.beforeSend(xhr);
      }
      xhr.send(param.data);
    }, function () {
      xhr.abort();
    });
  }
  function testURL(url) {
      return RSVP.Queue()
        .push(function () { 
          return ajax({
            url: url
          })
          .fail(function() {
            return "FAIL";
          })
          .then(function (evt) {
            return evt.target.responseText;
          });
        });
    }

  function launchTest(url) {
    var start = new Date().getTime();
    return testURL(url)
      .push(function (result) {
         console.log(test_dict);
         test_dict[url] =  new Date().getTime() - start;
         return test_dict;
      })
   }
  
  function runOnce(test_queue) {
     return new RSVP.Queue()
      .push(function () {
         var u;
         for (u in test_url_list) {
           if (test_url_list.hasOwnProperty(u)) {
             test_queue.push(launchTest(test_url_list[u]));
           }
         }
         return RSVP.all(test_queue)
         .catch(function(reason) {
            return "OK"  });
      });
  };

  function runAll() {
    return new RSVP.Queue()
      .push(function () {
         return runOnce(promise_list)
          .push(function () {
              return runOnce(promise_list)
             })

           })
     }
  runAll();
  
}(window, document));