-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat($resource): add support for timeout in cancellable actions #13824
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,7 @@ function shallowClearAndCopy(src, dst) { | |
* @requires $http | ||
* @requires ng.$log | ||
* @requires $q | ||
* @requires $timeout | ||
* | ||
* @description | ||
* A factory which creates a resource object that lets you interact with | ||
|
@@ -160,7 +161,6 @@ function shallowClearAndCopy(src, dst) { | |
* will be cancelled (if not already completed) by calling `$cancelRequest()` on the call's | ||
* return value. Calling `$cancelRequest()` for a non-cancellable or an already | ||
* completed/cancelled request will have no effect.<br /> | ||
* **Note:** If a timeout is specified in millisecondes, `cancellable` is ignored. | ||
* - **`withCredentials`** - `{boolean}` - whether to set the `withCredentials` flag on the | ||
* XHR object. See | ||
* [requests with credentials](https://developer.mozilla.org/en/http_access_control#section_5) | ||
|
@@ -416,7 +416,7 @@ angular.module('ngResource', ['ng']). | |
} | ||
}; | ||
|
||
this.$get = ['$http', '$log', '$q', function($http, $log, $q) { | ||
this.$get = ['$http', '$log', '$q', '$timeout', function($http, $log, $q, $timeout) { | ||
|
||
var noop = angular.noop, | ||
forEach = angular.forEach, | ||
|
@@ -575,7 +575,11 @@ angular.module('ngResource', ['ng']). | |
|
||
forEach(actions, function(action, name) { | ||
var hasBody = /^(POST|PUT|PATCH)$/i.test(action.method); | ||
var cancellable = false; | ||
var cancellable = angular.isDefined(action.cancellable) ? action.cancellable : | ||
(options && angular.isDefined(options.cancellable)) ? options.cancellable : | ||
provider.defaults.cancellable; | ||
|
||
var simulatedTimeout; | ||
|
||
if (!angular.isNumber(action.timeout)) { | ||
if (action.timeout) { | ||
|
@@ -586,9 +590,11 @@ angular.module('ngResource', ['ng']). | |
'requests, you should use the `cancellable` option.'); | ||
delete action.timeout; | ||
} | ||
cancellable = angular.isDefined(action.cancellable) ? action.cancellable : | ||
(options && angular.isDefined(options.cancellable)) ? options.cancellable : | ||
provider.defaults.cancellable; | ||
} else { | ||
if(cancellable) { | ||
simulatedTimeout = action.timeout; | ||
delete action.timeout; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why delete it here ? Couldn't we just let it be ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes we don't need to delete it. It gets overwritten anyway later:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it is overwritten. |
||
} | ||
} | ||
|
||
Resource[name] = function(a1, a2, a3, a4) { | ||
|
@@ -639,6 +645,7 @@ angular.module('ngResource', ['ng']). | |
var responseErrorInterceptor = action.interceptor && action.interceptor.responseError || | ||
undefined; | ||
var timeoutDeferred; | ||
var simulatedTimeoutPromise; | ||
|
||
forEach(action, function(value, key) { | ||
switch (key) { | ||
|
@@ -656,6 +663,14 @@ angular.module('ngResource', ['ng']). | |
if (!isInstanceCall && cancellable) { | ||
timeoutDeferred = $q.defer(); | ||
httpConfig.timeout = timeoutDeferred.promise; | ||
|
||
if(simulatedTimeout) { | ||
simulatedTimeoutPromise = $timeout(function() { | ||
if(timeoutDeferred) { | ||
timeoutDeferred.resolve(); | ||
} | ||
}, simulatedTimeout); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but I wanted to be sure, timeoutDeferred is not null already, so extra if() here. But may be it's obvious. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right, timeoutDeferred.resolve can be evaluated instantly (and not inside the timeout handler). |
||
} | ||
} | ||
|
||
if (hasBody) httpConfig.data = data; | ||
|
@@ -707,6 +722,7 @@ angular.module('ngResource', ['ng']). | |
if (!isInstanceCall && cancellable) { | ||
value.$cancelRequest = angular.noop; | ||
timeoutDeferred = httpConfig.timeout = null; | ||
$timeout.cancel(simulatedTimeoutPromise); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. |
||
} | ||
}); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
simulatedTimeout
is a little confusing imo (what is "simulated" about it).(On the other hand, I'm notiriously bad with names 😃)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was trying to find a different name, not to confuse with timeout which is passed to $http. There are quite a few variables that deal with $http timeout.
So for me it means "non-$http" timeout.