Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add client side abort for request.js #370

Merged
merged 3 commits into from
Aug 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ function Request(client, queries, callback){
};
this.configure(client, queries, cb);
cb = callback = null;
this._activeQueries = [];
this._isAborted = false;
};
Emitter(Request.prototype);

Expand All @@ -35,14 +37,25 @@ Request.prototype.timeout = function(ms){
return this;
};

// Abort all remaining requests
Request.prototype.abort = function(){
this._isAborted = true;
this._activeQueries.forEach(function(query) {
query.abort();
});
this._activeQueries = [];
return this;
}

Request.prototype.refresh = function(){
var self = this,
completions = 0,
response = [],
errored = false;
this._isAborted = false;

var handleResponse = function(err, res, index){
if (errored) {
if (errored || self._isAborted) {
return;
}
if (err) {
Expand All @@ -62,6 +75,7 @@ Request.prototype.refresh = function(){
self.callback(null, self.data);
}
}
self._activeQueries.splice(index, 1);
};

each(self.queries, function(query, index){
Expand All @@ -72,16 +86,16 @@ Request.prototype.refresh = function(){

if (typeof query === 'string') {
path += '/saved/' + query + '/result';
sendSavedQuery.call(self, path, {}, cbSequencer);
self._activeQueries[index] = sendSavedQuery.call(self, path, {}, cbSequencer);
}
else if (query instanceof Query) {
path += '/' + query.analysis;
if (query.analysis === 'saved') {
path += '/' + query.params.query_name + '/result';
sendSavedQuery.call(self, path, {}, cbSequencer);
self._activeQueries[index] = sendSavedQuery.call(self, path, {}, cbSequencer);
}
else {
sendQuery.call(self, path, query.params, cbSequencer);
self._activeQueries[index] = sendQuery.call(self, path, query.params, cbSequencer);
}
}
else {
Expand Down
14 changes: 5 additions & 9 deletions src/core/utils/sendQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,15 @@ module.exports = function(path, params, callback){
}

if (getXHR() || getContext() === 'server' ) {
request
return request
.post(url)
.set('Content-Type', 'application/json')
.set('Authorization', this.client.readKey())
.timeout(this.timeout())
.send(params || {})
.end(handleResponse);
.end(function handleResponse(err, res){
responseHandler(err, res, callback);
callback = null;
});
}

function handleResponse(err, res){
responseHandler(err, res, callback);
callback = null;
}

return;
}
4 changes: 1 addition & 3 deletions src/core/utils/sendSavedQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var request = require('superagent');
var responseHandler = require('../helpers/superagent-handle-response');

module.exports = function(path, params, callback){
request
return request
.get(this.client.url(path))
.set('Content-Type', 'application/json')
.set('Authorization', this.client.readKey())
Expand All @@ -12,6 +12,4 @@ module.exports = function(path, params, callback){
responseHandler(err, res, callback);
callback = null;
});

return;
}
15 changes: 15 additions & 0 deletions test/unit/modules/core/request/browser-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ describe("Keen.Request", function() {
});
});

describe("abort queries", function() {
it("with multiple queries", function(done) {
var response = [{ result: 1 }, { result: 1 }, { result: 1 }];
this.server.respondWith( "POST", this.postUrl, [ 200, { "Content-Type": "application/json"}, JSON2.stringify(response[0]) ] );
this.server.respondWith( "POST", this.postUrl, [ 200, { "Content-Type": "application/json"}, JSON2.stringify(response[1]) ] );
this.server.respondWith( "POST", this.postUrl, [ 200, { "Content-Type": "application/json"}, JSON2.stringify(response[2]) ] );
this.server.respond();
var run = this.client.run([this.query, this.query, this.query], function(err, res){
expect().fail("callback shouldn't have been called.");
});
run.abort();
setTimeout(done, 100);
});
});

});

});
25 changes: 25 additions & 0 deletions test/unit/modules/core/request/server-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,31 @@ describe("Keen.Request", function() {
});
});

describe("abort queries", function() {
it("with multiple queries", function(done) {
var response = [{ result: 0 }, { result: 1 }, { result: 2 }];
mock.post("/queries/count", 200, JSON2.stringify(response[0]));
mock.post("/queries/count", 200, JSON2.stringify(response[1]));
mock.post("/queries/count", 200, JSON2.stringify(response[2]));

var req = new Keen.Request(this.client, [this.query, this.query, this.query], function(err, res){
expect().fail("callback shouldn't have been called.");
});
req
.refresh()
.abort();
setTimeout(done, 100);
});

it("with no queries", function(done) {
var req = new Keen.Request(this.client, [], function(err, res){
expect().fail("callback shouldn't have been called.");
});
req.refresh().abort();
done();
});
});

});

});