Skip to content

Commit

Permalink
URL loader: allow overriding default Xhr based loader by a custom one
Browse files Browse the repository at this point in the history
useful for P2P/testing
add a callback to deal with timeout
related to #2
related to #3
  • Loading branch information
mangui committed Jun 2, 2015
1 parent 890b4e8 commit 39256b5
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions src/utils/xhr-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Xhr based Loader
*
*/

import {logger} from '../utils/logger';

class XhrLoader {

constructor() {
}

destroy() {
this.abort();
this.loader = null;
}

abort() {
if(this.loader &&this.loader.readyState !== 4) {
this.loader.abort();
}
if(this.timeoutHandle) {
window.clearTimeout(this.timeoutHandle);
}
}

load(url,responseType,onSuccess,onError,onTimeout,timeout,maxRetry,retryDelay) {
this.url = url;
this.responseType = responseType;
this.onSuccess = onSuccess;
this.onTimeout = onTimeout;
this.onError = onError;
this.trequest = new Date();
this.timeout = timeout;
this.maxRetry = maxRetry;
this.retryDelay = retryDelay;
this.retry = 0;
this.timeoutHandle = window.setTimeout(this.loadtimeout.bind(this),timeout);
this.loadInternal();
}

loadInternal() {
var xhr = this.loader = new XMLHttpRequest();
xhr.onload = this.loadsuccess.bind(this);
xhr.onerror = this.loaderror.bind(this);
xhr.onprogress = this.loadprogress.bind(this);
xhr.open('GET', this.url , true);
xhr.responseType = this.responseType;
this.tfirst = null;
this.loaded = 0;
xhr.send();
}

loadsuccess(event) {
window.clearTimeout(this.timeoutHandle);
this.onSuccess(event,{trequest : this.trequest, tfirst : this.tfirst, tload : new Date(), loaded : this.loaded});
}

loaderror(event) {
if(this.retry < this.maxRetry) {
logger.log(`${event.type} while loading ${this.url}, retrying in ${this.retryDelay}...`);
this.destroy();
window.setTimeout(this.loadInternal.bind(this),this.retryDelay);
// exponential backoff
this.retryDelay=Math.min(2*this.retryDelay,64000);
this.retry++;
} else {
window.clearTimeout(this.timeoutHandle);
logger.log(`${event.type} while loading ${this.url}` );
this.onError(event);
}
}

loadtimeout(event) {
logger.log(`timeout while loading ${this.url}` );
this.onTimeout(event,{trequest : this.trequest, tfirst : this.tfirst, loaded : this.loaded});
}

loadprogress(event) {
if(this.tfirst === null) {
this.tfirst = new Date();
}
if(event.lengthComputable) {
this.loaded = event.loaded;
}
}
}

export default XhrLoader;

0 comments on commit 39256b5

Please sign in to comment.