-
Notifications
You must be signed in to change notification settings - Fork 5
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
Cancellation Token #8
Comments
I don't know if this is of use to anybody here, but here's what we're using, for the lack of a better pattern. It's pretty much based on what I would love to see first-class support for cancellation in a spec, and I enjoy the idea of passing around the token. var source = new window.CancellationTokenSource(),
token = source.getToken();
// Cancel in three seconds.
window.setTimeout(function() {
source.cancel();
}, 3000);
doFirstAsyncOperation(token) // it's safe to pass token around
.then(token.throwIfCanceled) // may get canceled here
.then(doSecondAsyncOperation)
.then(function () {
doCostlySynchronousOperation();
token.throwIfCanceled (); // or here
doSecondCostlySynchronousOperation();
})
.catch(token.catchItself) // will not propagate
.done();
(function () {
'use strict';
window.CancellationTokenSource = function () {
this._token = new window.CancellationToken(this);
this._canceled = false;
};
_.extend(window.CancellationTokenSource.prototype, {
getToken: function () {
return this._token;
},
isCanceled: function () {
return this._canceled;
},
cancel: function () {
if (this.isCanceled()) {
throw new Error('This token source is already canceled.');
}
this._canceled = true;
}
});
})();
(function () {
'use strict';
window.CancellationToken = function (source) {
this._source = source;
_.bindAll(this, 'catchItself', 'throwIfCanceled');
};
_.extend(window.CancellationToken.prototype, {
isCanceled: function () {
return this._source.isCanceled();
},
throwIfCanceled: function (previousResult) {
if (this.isCanceled()) {
throw this;
}
return previousResult;
},
catchItself: function (ex) {
if (ex !== this) {
throw ex;
}
},
toString: function () {
return 'cancellation token';
}
});
_.extend(window.CancellationToken, {
getNone: function () {
return (new window.CancellationTokenSource ()).getToken();
}
});
})(); |
What's the point of this addendum?
It's seem like it would allow for indeterminacies
Are |
This is an alternative idea based on a cancellation token. The aim with this was to produce a better idea from a "security" point of view that still had nice semantics for propagation etc.
The core idea is that you provide a
CancellationToken
which represents the ability to be notified of cancellation. You can only provide this when a promise is created, but if a promise wasn't provided with one when it was created, you can provide one later.Terminology
CancellationToken
is an object with methods for determining whether an operation should be cancelledCancellationToken
can be associated with a promise.CancellationToken
s can be registered with a promise.Requirements
The arguments of a
then
method look like.then(onFulfilled, onRejected, onProgress, CancellationToken)
. For this spec, we are interested inCancellationToken
.CancellationToken
A CancellationToken is an object with the following two methods:
.isCancelled()
must always returntrue
orfalse
onCancelled(cb)
must callcb
if Cancellation is triggered.Direct Cancellation
If a promise has a
CancellationToken
associated with it (i.e. it was created via a call to.then
that included aCancellationToken
) then it is cancelled if and only if thatCancellationToken
is cancelled. If thatCancellationToken
is cancelled, it is immediately rejected.Parent Cancellation
If a promise has no
CancellationToken
associated with it directly, it can be cancelled indirectly. All calls to.then
that create a new promise must register that new promise's associatedCancellationToken
with the current promise.This promise gets an internal
CancellationToken
associated with it that is cancelled whenever all registeredCancellationToken
s have been cancelled (providing at least one tick has gone past to allow for more tokens to be registered).Child Cancellation
Any promise returned from a call to
onFulfilled
oronRejected
should have the current promise's associatedCancellationToken
(even if it's internal via Parent Cancellation) registered with it via the call to.then
necessary for assimilation.The text was updated successfully, but these errors were encountered: