Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #452 from nickuraltsev/cancel
Adding support for request cancellation
- Loading branch information
Showing
18 changed files
with
454 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
language: node_js | ||
node_js: | ||
- node | ||
email: | ||
on_failure: change | ||
on_success: never | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
|
||
/** | ||
* A `Cancel` is an object that is thrown when an operation is canceled. | ||
* | ||
* @class | ||
* @param {string=} message The message. | ||
*/ | ||
function Cancel(message) { | ||
this.message = message; | ||
} | ||
|
||
Cancel.prototype.toString = function toString() { | ||
return 'Cancel' + (this.message ? ': ' + this.message : ''); | ||
}; | ||
|
||
Cancel.prototype.__CANCEL__ = true; | ||
|
||
module.exports = Cancel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict'; | ||
|
||
var Cancel = require('./Cancel'); | ||
|
||
/** | ||
* A `CancelToken` is an object that can be used to request cancellation of an operation. | ||
* | ||
* @class | ||
* @param {Function} executor The executor function. | ||
*/ | ||
function CancelToken(executor) { | ||
if (typeof executor !== 'function') { | ||
throw new TypeError('executor must be a function.'); | ||
} | ||
|
||
var resolvePromise; | ||
this.promise = new Promise(function promiseExecutor(resolve) { | ||
resolvePromise = resolve; | ||
}); | ||
|
||
var token = this; | ||
executor(function cancel(message) { | ||
if (token.reason) { | ||
// Cancellation has already been requested | ||
return; | ||
} | ||
|
||
token.reason = new Cancel(message); | ||
resolvePromise(token.reason); | ||
}); | ||
} | ||
|
||
/** | ||
* Throws a `Cancel` if cancellation has been requested. | ||
*/ | ||
CancelToken.prototype.throwIfRequested = function throwIfRequested() { | ||
if (this.reason) { | ||
throw this.reason; | ||
} | ||
}; | ||
|
||
/** | ||
* Returns an object that contains a new `CancelToken` and a function that, when called, | ||
* cancels the `CancelToken`. | ||
*/ | ||
CancelToken.source = function source() { | ||
var cancel; | ||
var token = new CancelToken(function executor(c) { | ||
cancel = c; | ||
}); | ||
return { | ||
token: token, | ||
cancel: cancel | ||
}; | ||
}; | ||
|
||
module.exports = CancelToken; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = function isCancel(value) { | ||
return !!(value && value.__CANCEL__); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
var Cancel = axios.Cancel; | ||
var CancelToken = axios.CancelToken; | ||
|
||
describe('cancel', function() { | ||
beforeEach(function() { | ||
jasmine.Ajax.install(); | ||
}); | ||
|
||
afterEach(function() { | ||
jasmine.Ajax.uninstall(); | ||
}); | ||
|
||
describe('when called before sending request', function() { | ||
it('rejects Promise with a Cancel object', function (done) { | ||
var source = CancelToken.source(); | ||
source.cancel('Operation has been canceled.'); | ||
axios.get('/foo', { | ||
cancelToken: source.token | ||
}).catch(function (thrown) { | ||
expect(thrown).toEqual(jasmine.any(Cancel)); | ||
expect(thrown.message).toBe('Operation has been canceled.'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when called after request has been sent', function() { | ||
it('rejects Promise with a Cancel object', function (done) { | ||
var source = CancelToken.source(); | ||
axios.get('/foo/bar', { | ||
cancelToken: source.token | ||
}).catch(function (thrown) { | ||
expect(thrown).toEqual(jasmine.any(Cancel)); | ||
expect(thrown.message).toBe('Operation has been canceled.'); | ||
done(); | ||
}); | ||
|
||
getAjaxRequest().then(function (request) { | ||
// call cancel() when the request has been sent, but a response has not been received | ||
source.cancel('Operation has been canceled.'); | ||
request.respondWith({ | ||
status: 200, | ||
responseText: 'OK' | ||
}); | ||
}); | ||
}); | ||
|
||
it('calls abort on request object', function (done) { | ||
var source = CancelToken.source(); | ||
var request; | ||
axios.get('/foo/bar', { | ||
cancelToken: source.token | ||
}).catch(function() { | ||
// jasmine-ajax sets statusText to 'abort' when request.abort() is called | ||
expect(request.statusText).toBe('abort'); | ||
done(); | ||
}); | ||
|
||
getAjaxRequest().then(function (req) { | ||
// call cancel() when the request has been sent, but a response has not been received | ||
source.cancel(); | ||
request = req; | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.