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

Alternative $resource cancellable API #4

Closed
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
51 changes: 27 additions & 24 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ angular.module('ngResource', ['ng']).
}
};

this.$get = ['$http', '$log', '$q', function($http, $log, $q) {
this.$get = ['$http', '$log', '$q', '$$HashMap', function($http, $log, $q, HashMap) {

var noop = angular.noop,
forEach = angular.forEach,
Expand Down Expand Up @@ -545,6 +545,7 @@ angular.module('ngResource', ['ng']).

function resourceFactory(url, paramDefaults, actions, options) {
var route = new Route(url, options);
var qTimeouts = new HashMap();

actions = extend({}, provider.defaults.actions, actions);

Expand All @@ -567,6 +568,14 @@ angular.module('ngResource', ['ng']).
shallowClearAndCopy(value || {}, this);
}

Resource.cancelRequest = function(value) {
var qTimeout = qTimeouts.get(value);
if (qTimeout) {
qTimeout.resolve();
qTimeouts.remove(value);
}
};

Resource.prototype.toJSON = function() {
var data = extend({}, this);
delete data.$promise;
Expand All @@ -587,14 +596,9 @@ angular.module('ngResource', ['ng']).
delete action.timeout;
hasTimeout = false;
}
action.cancellable = hasTimeout ?
false : action.hasOwnProperty('cancellable') ?
action.cancellable : (options && options.hasOwnProperty('cancellable')) ?
options.cancellable :
provider.defaults.cancellable;

Resource[name] = function(a1, a2, a3, a4) {
var params = {}, data, success, error;
var params = {}, data, success, error, qTimeout;

/* jshint -W086 */ /* (purposefully fall through case statements) */
switch (arguments.length) {
Expand Down Expand Up @@ -649,35 +653,28 @@ angular.module('ngResource', ['ng']).
case 'params':
case 'isArray':
case 'interceptor':
case 'cancellable':
break;
case 'timeout':
httpConfig[key] = value;
break;
}
});

if (!isInstanceCall) {
if (!action.cancellable) {
value.$cancelRequest = angular.noop;
} else {
var deferred = $q.defer();
httpConfig.timeout = deferred.promise;
value.$cancelRequest = deferred.resolve;
}
}

if (hasBody) httpConfig.data = data;
route.setUrlParams(httpConfig,
extend({}, extractParams(data, action.params || {}), params),
action.url);

var promise = $http(httpConfig).finally(function() {
if (value.$cancelRequest) value.$cancelRequest = angular.noop;
}).then(function(response) {
if (!isInstanceCall && !httpConfig.timeout) {
qTimeout = $q.defer();
httpConfig.timeout = qTimeout.promise;
qTimeouts.put(value, qTimeout);
}

var promise = $http(httpConfig).then(function(response) {

var data = response.data,
promise = value.$promise,
cancelRequest = value.$cancelRequest;
promise = value.$promise;

if (data) {
// Need to convert action.isArray to boolean in case it is undefined
Expand Down Expand Up @@ -707,7 +704,6 @@ angular.module('ngResource', ['ng']).
}
}

value.$cancelRequest = cancelRequest;
value.$resolved = true;

response.resource = value;
Expand All @@ -721,6 +717,13 @@ angular.module('ngResource', ['ng']).
return $q.reject(response);
});

// The request has completed (either successfully or in error)
// Make sure that we cancel the timeout if there was one
promise.finally(function() {
if (qTimeout) qTimeout.reject();
qTimeouts.remove(value);
});

promise = promise.then(
function(response) {
var value = responseInterceptor(response);
Expand Down
Loading