Skip to content

Commit

Permalink
fix(CommonApi): makes canceled request promises resolve to error
Browse files Browse the repository at this point in the history
Closes #288
  • Loading branch information
iobaixas committed May 7, 2015
1 parent c934f1a commit 0b8f21c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/module/api/common-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,29 +370,31 @@ RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', function($http, $q,

return $http(_options).then(wrapPromise(this, function() {
if(action && action.canceled) {
// if request was canceled during request, ignore post request actions.
this.$status = 'canceled';
this.$dispatch('after-request-cancel', []);
return $q.reject(this);
} else {
this.$status = 'ok';
this.$response = this.$last;
this.$dispatch('after-request', [this.$last]);
if(_success) _success.call(this, this.$last);
this.$dispatch('after-request', [this.$response]);
if(_success) _success.call(this, this.$response);
}
}), wrapPromise(this, function() {
if(action && action.canceled) {
// if request was canceled during request, ignore error handling
this.$status = 'canceled';
this.$dispatch('after-request-cancel', []);
} else {
this.$status = 'error';
this.$response = this.$last;

// IDEA: Consider flushing pending request in case of an error. Also continue ignoring requests
// until the error flag is reset by user.

this.$dispatch('after-request-error', [this.$last]);
if(_error) _error.call(this, this.$last);
return $q.reject(this); // TODO: this will step over any promise generated in _error!!
this.$dispatch('after-request-error', [this.$response]);
if(_error) _error.call(this, this.$response);
}

return $q.reject(this); // TODO: this will step over any promise generated in _error!!
}));
});
},
Expand Down
10 changes: 10 additions & 0 deletions test/common-api-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ describe('Restmod model class:', function() {
expect(spy).toHaveBeenCalled();
});

it('should resolve to error if containing action is canceled', function() {
var bike = Bike.$new();
var spy = jasmine.createSpy('callback');
bike.$$action = {}
bike.$send({ method: 'GET', url: '/api/bikes/2' }).$then(null, spy);
bike.$$action.canceled = true;
$httpBackend.flush();
expect(spy).toHaveBeenCalled();
});

it('should properly propagate error states to $then', function() {
var bike = Bike.$new();
var spy = jasmine.createSpy('callback');
Expand Down

0 comments on commit 0b8f21c

Please sign in to comment.