| '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; |
|
|
| |
| this.promise.then(function(cancel) { |
| if (!token._listeners) return; |
|
|
| var i; |
| var l = token._listeners.length; |
|
|
| for (i = 0; i < l; i++) { |
| token._listeners[i](cancel); |
| } |
| token._listeners = null; |
| }); |
|
|
| |
| this.promise.then = function(onfulfilled) { |
| var _resolve; |
| |
| var promise = new Promise(function(resolve) { |
| token.subscribe(resolve); |
| _resolve = resolve; |
| }).then(onfulfilled); |
|
|
| promise.cancel = function reject() { |
| token.unsubscribe(_resolve); |
| }; |
|
|
| return promise; |
| }; |
|
|
| 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.prototype.subscribe = function subscribe(listener) { |
| if (this.reason) { |
| listener(this.reason); |
| return; |
| } |
|
|
| if (this._listeners) { |
| this._listeners.push(listener); |
| } else { |
| this._listeners = [listener]; |
| } |
| }; |
|
|
| |
| |
| |
|
|
| CancelToken.prototype.unsubscribe = function unsubscribe(listener) { |
| if (!this._listeners) { |
| return; |
| } |
| var index = this._listeners.indexOf(listener); |
| if (index !== -1) { |
| this._listeners.splice(index, 1); |
| } |
| }; |
|
|
| |
| |
| |
| |
| CancelToken.source = function source() { |
| var cancel; |
| var token = new CancelToken(function executor(c) { |
| cancel = c; |
| }); |
| return { |
| token: token, |
| cancel: cancel |
| }; |
| }; |
|
|
| module.exports = CancelToken; |
|
|