Skip to content

Commit

Permalink
Convert Request instances returned by io.makeRequest into Tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
aarranz committed Nov 16, 2016
1 parent 894cf8c commit 550c6b2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
5 changes: 4 additions & 1 deletion src/js_tests/wirecloud/ioSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@

var request = Wirecloud.io.makeRequest(original);
expect(request.url).toBe(expected);
expect(request.progress).toBe(0);
});

it("should ignore null headers", function () {
Expand Down Expand Up @@ -279,6 +280,8 @@
var request = Wirecloud.io.makeRequest(url, {
onComplete: function (response) {
expect(response.request).toBe(request);
expect(request.progress).toBe(0);
expect(request.status).toBe("aborted");
done();
}
});
Expand Down Expand Up @@ -539,7 +542,7 @@
request.transport[key] = extra[key];
}
}
findListener(request.transport.addEventListener, 'readystatechange')();
findListener(request.transport.addEventListener, status === 0 ? 'load' : 'error')();
};

var findListener = function findListener(spy, name) {
Expand Down
61 changes: 41 additions & 20 deletions src/wirecloud/platform/static/js/wirecloud/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
event.preventDefault();

var response = new Response(this);
Wirecloud.Task.prototype.abort.call(this, response);

utils.callCallback(this.options.onAbort);
if (this.options.onComplete) {
Expand All @@ -66,31 +67,31 @@
}
};

var onReadyStateChange = function onReadyStateChange() {
if (this.transport.readyState === 4 && this.transport.aborted !== true) {
var onReadyStateChange = function onReadyStateChange(handler) {
var response = new Response(this);
handler(response);

var response = new Response(this);
try {
if (('on' + response.status) in this.options) {
this.options['on' + response.status](response);
} else if (this.options.onSuccess && (response.status >= 200 && response.status < 300)) {
this.options.onSuccess(response);
} else if (this.options.onFailure && (response.status < 200 || response.status >= 300)) {
this.options.onFailure(response);
}
} catch (e) {
utils.callCallback(this.options.onException, response, e);
}

if (this.options.onComplete) {
try {
if (('on' + response.status) in this.options) {
this.options['on' + response.status](response);
} else if (this.options.onSuccess && (response.status >= 200 && response.status < 300)) {
this.options.onSuccess(response);
} else if (this.options.onFailure && (response.status < 200 || response.status >= 300)) {
this.options.onFailure(response);
}
this.options.onComplete(response);
} catch (e) {
utils.callCallback(this.options.onException, response, e);
}

if (this.options.onComplete) {
try {
this.options.onComplete(response);
} catch (e) {
utils.callCallback(this.options.onException, response, e);
}
}
}

return response;
};

var Response = function Response(request) {
Expand Down Expand Up @@ -195,18 +196,38 @@
if (this.options.withCredentials === true && this.options.supportsAccessControl) {
this.transport.withCredentials = true;
}
this.transport.open(this.options.method, this.url, this.options.asynchronous);
if (this.options.responseType) {
this.transport.responseType = this.options.responseType;
}
this.transport.addEventListener('abort', onAbort.bind(this), true);
this.transport.addEventListener('readystatechange', onReadyStateChange.bind(this), true);
if (typeof this.options.onUploadProgress === 'function') {
this.transport.upload.addEventListener('progress', options.onUploadProgress, false);
}

Wirecloud.Task.call(this, "making request", function (resolve, reject, update) {
this.transport.upload.addEventListener('progress', function (event) {
var progress = Math.round(event.loaded * 100 / event.total);
update(progress / 2);
});
this.transport.addEventListener("progress", function (event) {
if (event.lengthComputable) {
var progress = Math.round(event.loaded * 100 / event.total);
update(50 + (progress / 2));
}
}.bind(this));
this.transport.addEventListener("load", function () {
onReadyStateChange.call(this, resolve);
}.bind(this));
this.transport.addEventListener("error", function () {
onReadyStateChange.call(this, reject);
}.bind(this));
}.bind(this));

this.transport.open(this.method, this.url, this.options.asynchronous);
setRequestHeaders.call(this);
this.transport.send(this.options.postBody);
};
utils.inherit(Request, Wirecloud.Task);

var io = {};

Expand Down

0 comments on commit 550c6b2

Please sign in to comment.