Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

support for soft- and hard-aborts

  • Loading branch information...
commit 0ef489319b8c3cb94284aa8eec154f26ca3b0dcd 1 parent 57d80be
disfated disfated authored

Showing 3 changed files with 66 additions and 20 deletions. Show diff stats Hide diff stats

  1. +1 1  README.md
  2. +28 5 lib/restler.js
  3. +37 14 test/restler.js
2  README.md
Source Rendered
@@ -44,7 +44,7 @@ Basic method to make a request of any type. The function returns a RestRequest o
44 44
45 45 #### members
46 46
47   -* `abort()` Cancels request. `abort` event is emitted. `aborted` property is set to `true`. only `complete` and `error` event should.
  47 +* `abort([error])` Cancels request. `abort` event is emitted. `request.aborted` is set to `true`. If non-falsy `error` is passed, then `error` will be additionaly emitted (with `error` passed as a param and `error.type` is set to `"abort"`). Otherwise only `complete` event will raise.
48 48 * `aborted` Determines if request was aborted.
49 49
50 50
33 lib/restler.js
@@ -206,7 +206,9 @@ mixin(Request.prototype, {
206 206 this.request.on('response', function(response) {
207 207 self._responseHandler(response);
208 208 }).on('error', function(err) {
209   - self._fireError(err, null);
  209 + if (!self.aborted) {
  210 + self._fireError(err, null);
  211 + }
210 212 });
211 213 },
212 214 run: function() {
@@ -225,10 +227,31 @@ mixin(Request.prototype, {
225 227
226 228 return this;
227 229 },
228   - abort: function() {
229   - this.aborted = true;
230   - this.request.abort();
231   - this.emit('abort');
  230 + abort: function(err) {
  231 + var self = this;
  232 +
  233 + if (err) {
  234 + if (typeof err == 'string') {
  235 + err = new Error(err);
  236 + } else if (!(err instanceof Error)) {
  237 + err = new Error('AbortError');
  238 + }
  239 + err.type = 'abort';
  240 + } else {
  241 + err = null;
  242 + }
  243 +
  244 + self.request.on('close', function() {
  245 + if (err) {
  246 + self._fireError(err, null);
  247 + } else {
  248 + self.emit('complete', null, null);
  249 + }
  250 + });
  251 +
  252 + self.aborted = true;
  253 + self.request.abort();
  254 + self.emit('abort', err);
232 255 return this;
233 256 }
234 257 });
51 test/restler.js
@@ -298,6 +298,12 @@ function dataResponse(request, response) {
298 298 response.writeHead(200, { 'content-type': 'application/yaml' });
299 299 response.end('{Чебурашка');
300 300 break;
  301 + case '/abort':
  302 + setTimeout(function() {
  303 + response.writeHead(200);
  304 + response.end('not aborted');
  305 + }, 100);
  306 + break;
301 307 default:
302 308 response.writeHead(404);
303 309 response.end();
@@ -431,22 +437,39 @@ module.exports['Deserialization'] = {
431 437 });
432 438 },
433 439
434   - 'Should correctly abort request': function(test) {
435   - test.expect(5);
436   - rest.get(host + '/json').on('complete', function(data) {
437   - test.ok(data instanceof Error, 'should be error, got: ' + p(data));
438   - test.equal(this.aborted, true, 'should aborted, got: ' + p(this.aborted));
  440 + 'Should correctly soft-abort request': function(test) {
  441 + test.expect(4);
  442 + rest.get(host + '/abort').on('complete', function(data) {
  443 + test.equal(data, null, 'data should be null');
  444 + test.equal(this.aborted, true, 'should be aborted');
439 445 test.done();
440   - }).on('error', function(data) {
  446 + }).on('error', function(err) {
  447 + test.ok(false, 'should not emit error event');
  448 + }).on('abort', function(err) {
  449 + test.equal(err, null, 'err should be null');
  450 + test.equal(this.aborted, true, 'should be aborted');
  451 + }).on('success', function() {
  452 + test.ok(false, 'should not emit success event');
  453 + }).on('fail', function() {
  454 + test.ok(false, 'should not emit fail event');
  455 + }).abort();
  456 + },
  457 +
  458 + 'Should correctly hard-abort request': function(test) {
  459 + test.expect(4);
  460 + rest.get(host + '/abort').on('complete', function(data) {
441 461 test.ok(data instanceof Error, 'should be error, got: ' + p(data));
442   - test.equal(this.aborted, true, 'should aborted, got: ' + p(this.aborted));
443   - }).on('abort', function() {
444   - test.equal(this.aborted, true, 'should aborted, got: ' + p(this.aborted));
445   - }).on('success', function() {
446   - test.ok(false, 'should not have got here');
447   - }).on('fail', function() {
448   - test.ok(false, 'should not have got here');
449   - }).abort();
  462 + test.equal(this.aborted, true, 'should be aborted');
  463 + test.done();
  464 + }).on('error', function(err) {
  465 + test.ok(err instanceof Error, 'should be error, got: ' + p(err));
  466 + }).on('abort', function(err) {
  467 + test.equal(this.aborted, true, 'should be aborted');
  468 + }).on('success', function() {
  469 + test.ok(false, 'should not emit success event');
  470 + }).on('fail', function() {
  471 + test.ok(false, 'should not emit fail event');
  472 + }).abort(true);
450 473 },
451 474
452 475 'Should correctly handle malformed JSON': function(test) {

0 comments on commit 0ef4893

Please sign in to comment.
Something went wrong with that request. Please try again.