Skip to content
Open
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
29 changes: 19 additions & 10 deletions src/request-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
*/

angular.module('angular-abortable-requests', ['ngResource'])
.constant('DEFAULT_REASON', 'ABORT')
.factory('RequestFactory', [ '$resource', '$http',
'Util', '$q' ,function($resource, $http,
Util, $q) {
'Util', '$q', 'DEFAULT_REASON', function($resource, $http,
Util, $q, DEFAULT_REASON) {

function abortablePromiseWrap (promise, deferred, outstanding) {

Expand Down Expand Up @@ -52,8 +53,8 @@ angular.module('angular-abortable-requests', ['ngResource'])
return {
promise: deferred.promise,

abort: function (){
deferred.reject('ABORT');
abort: function (reason) {
deferred.reject(getRejectReason(reason));
}

};
Expand All @@ -64,16 +65,24 @@ angular.module('angular-abortable-requests', ['ngResource'])
* Abort all the outstanding requests on
* this $resource
*/
resource.abortAll = function () {
resource.abortAll = function (reason) {
angular.forEach(outstanding, function(deferred) {
deferred.reject('ABORT');
deferred.reject(getRejectReason(reason));
});
outstanding = [];
};

return resource;
}

function getRejectReason (reason) {
if (!angular.isDefined(reason)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could just be:

function getRejectReason (reason) {
  return reason || DEFAULT_REASON
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Someone might want to make the reason 0

return DEFAULT_REASON;
}

return reason;
}

function getHttpConfig (url) {
return {
method: 'GET',
Expand All @@ -93,9 +102,9 @@ angular.module('angular-abortable-requests', ['ngResource'])
/*
* Abort all outstanding requests
*/
abortAll: function() {
abortAll: function(reason) {
angular.forEach(outstanding, function(deferred) {
deferred.reject('ABORT');
deferred.reject(getRejectReason(reason));
});
outstanding = [];
},
Expand Down Expand Up @@ -130,8 +139,8 @@ angular.module('angular-abortable-requests', ['ngResource'])

promise: deferred.promise,

abort: function() {
deferred.reject('ABORT');
abort: function(reason) {
deferred.reject(getRejectReason(reason));
}
};
}
Expand Down
24 changes: 20 additions & 4 deletions test/request-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,30 @@
describe('RequestFactory', function () {
var RequestFactory,
resourceConfig,
DEFAULT_REASON,
$httpBackend;

beforeEach(module('angular-abortable-requests'));

//Takes the api from http/resource
function abortRequestTest (api, output) {
// abort the request
api.abort();
function abortRequestTest (api, output, reason) {
var expectedReason;

if (!angular.isDefined(reason)) {
expectedReason = DEFAULT_REASON;
api.abort();
} else {
expectedReason = reason;
api.abort(reason);
}

// on reject
api.promise.catch(function(err){
output = err;
});

$httpBackend.flush();
expect(output).toEqual('ABORT');
expect(output).toEqual(expectedReason);
}

function resolveRequestTest (api, output) {
Expand All @@ -33,6 +42,7 @@ describe('RequestFactory', function () {
beforeEach(inject(function ($injector) {
RequestFactory = $injector.get('RequestFactory');
$httpBackend = $injector.get('$httpBackend');
DEFAULT_REASON = $injector.get('DEFAULT_REASON');
resourceConfig = {
url : '/proxy/test',
options: null,
Expand Down Expand Up @@ -82,6 +92,12 @@ describe('RequestFactory', function () {
abortRequestTest(res, output);
});

it('returns a promise which can be aborted with a custom reason', function() {
var res, output;
res = rInstance.get();
abortRequestTest(res, output, 'arbitrary reason');
});

it('returns a promise which resolves',
function() {
var res, output;
Expand Down