Sha256: b7a51de3db800329ad504c3528ee3f1285caabd3f988456e8a0beedaf5a9f33c

Contents?: true

Size: 1.8 KB

Versions: 85

Compression:

Stored size: 1.8 KB

Contents

pageflow.bandwidth = function() {
  var maxLoadTime = 5000;

  pageflow.bandwidth.promise = pageflow.bandwidth.promise || new $.Deferred(function(deferred) {
    var smallFileUrl = pageflow.assetUrls.smallBandwidthProbe + "?" + new Date().getTime(),
        largeFileUrl = pageflow.assetUrls.largeBandwidthProbe + "?" + new Date().getTime(),
        smallFileSize = 165,
        largeFileSize = 1081010;

    $.when(timeFile(smallFileUrl), timeFile(largeFileUrl))
      .done(function(timeToLoadSmallFile, timeToLoadLargeFile) {
        var timeDelta = (timeToLoadLargeFile - timeToLoadSmallFile) / 1000;
        var bitsDelta = (largeFileSize - smallFileSize) * 8;

        timeDelta = Math.max(timeDelta, 0.01);

        deferred.resolve({
          durationInSeconds: timeDelta,
          speedInBps: (bitsDelta / timeDelta).toFixed(2)
        });
      })
      .fail(function() {
        deferred.resolve({
          durationInSeconds: Infinity,
          speedInBps: 0
        });
      });
  }).promise();

  return pageflow.bandwidth.promise;

  function timeFile(url) {
    var startTime = new Date().getTime();

    return withTimeout(loadFile(url), maxLoadTime).pipe(function() {
      return new Date().getTime() - startTime;
    });
  }

  function loadFile(url, options) {
    return new $.Deferred(function(deferred) {
      var image = new Image();

      image.onload = deferred.resolve;
      image.onerror = deferred.reject;

      image.src = url;
    }).promise();
  }

  function withTimeout(promise, milliseconds) {
    return new $.Deferred(function(deferred) {
      var timeout = setTimeout(function() {
        deferred.reject();
      }, milliseconds);

      promise
        .always(function() {
          clearTimeout(timeout);
        })
        .then(deferred.resolve, deferred.reject);
    }).promise();
  }
};

Version data entries

85 entries across 85 versions & 1 rubygems

Version Path
pageflow-14.5.2 app/assets/javascripts/pageflow/bandwidth.js
pageflow-15.0.2 app/assets/javascripts/pageflow/bandwidth.js
pageflow-15.0.1 app/assets/javascripts/pageflow/bandwidth.js
pageflow-15.0.0 app/assets/javascripts/pageflow/bandwidth.js
pageflow-14.5.1 app/assets/javascripts/pageflow/bandwidth.js
pageflow-14.5.0 app/assets/javascripts/pageflow/bandwidth.js
pageflow-15.0.0.rc2 app/assets/javascripts/pageflow/bandwidth.js
pageflow-15.0.0.rc1 app/assets/javascripts/pageflow/bandwidth.js
pageflow-15.0.0.beta4 app/assets/javascripts/pageflow/bandwidth.js
pageflow-15.0.0.beta3 app/assets/javascripts/pageflow/bandwidth.js
pageflow-14.4.0 app/assets/javascripts/pageflow/bandwidth.js
pageflow-15.0.0.beta2 app/assets/javascripts/pageflow/bandwidth.js
pageflow-15.0.0.beta1 app/assets/javascripts/pageflow/bandwidth.js
pageflow-14.3.0 app/assets/javascripts/pageflow/bandwidth.js
pageflow-14.2.1 app/assets/javascripts/pageflow/bandwidth.js
pageflow-14.2.0 app/assets/javascripts/pageflow/bandwidth.js
pageflow-14.1.1 app/assets/javascripts/pageflow/bandwidth.js
pageflow-14.1.0 app/assets/javascripts/pageflow/bandwidth.js
pageflow-14.0.0 app/assets/javascripts/pageflow/bandwidth.js
pageflow-14.0.0.rc2 app/assets/javascripts/pageflow/bandwidth.js