|
|
'use strict'; |
|
|
|
|
|
var Cancel = require('./Cancel'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) { |
|
|
|
|
|
return; |
|
|
} |
|
|
|
|
|
token.reason = new Cancel(message); |
|
|
resolvePromise(token.reason); |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CancelToken.prototype.throwIfRequested = function throwIfRequested() { |
|
|
if (this.reason) { |
|
|
throw this.reason; |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CancelToken.source = function source() { |
|
|
var cancel; |
|
|
var token = new CancelToken(function executor(c) { |
|
|
cancel = c; |
|
|
}); |
|
|
return { |
|
|
token: token, |
|
|
cancel: cancel |
|
|
}; |
|
|
}; |
|
|
|
|
|
module.exports = CancelToken; |
|
|
|