Skip to content
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

Why not just have 'CancellationToken.race'? #13

Closed
rbuckton opened this issue Jul 24, 2017 · 5 comments
Closed

Why not just have 'CancellationToken.race'? #13

rbuckton opened this issue Jul 24, 2017 · 5 comments
Labels

Comments

@rbuckton
Copy link
Collaborator

This question came up at the May, 2017 TC39 meeting, asking why we don't just have a CancellationToken.race(tokens) method instead of using the CancellationTokenSource contstructor:

// why this?
const source = new CancellationTokenSource([token1, token2]);
const token = source.token;

// and not this?
const source = new CancellationTokenSource();
const token = CancellationToken.race([token1, token2, source.token]);
@rbuckton
Copy link
Collaborator Author

The reason we have chosen the first API is that we have the ability to close a CancellationTokenSource. Closing a source gives us the ability to stop listening for notifications from linked tokens and to clean up the source (allow registered callbacks to be garbage collected, etc.).

The second approach does not give us the ability to clean up registered callbacks.

Given #12, we could consider amending the API as follows:

class CancellationTokenSource {
  ...
  constructor();
  static race(linkedTokens: Iterable<CancellationToken>): CancellationTokenSource;
  static all(linkedTokens: Iterable<CancellationToken>): CancellationTokenSource;
  ...
}

This then parallels Promise.all and Promise.race, but requires the indirection through CancellationTokenSource:

// similar to the first API above:
const source = CancellationTokenSource.race([token1, token2]);
const token = source.token;

@Volune
Copy link

Volune commented Jul 25, 2017

Some thoughts after reading this issue:

const source = new CancellationTokenSource([token1, token2]);
const token = source.token;

If we extend CancellationTokenSource we can provide extra parameters to the constructor, new CustomSource([token1, token2], customArg);

const source = CancellationTokenSource.race([token1, token2]);
const token = source.token;

If one of the tokens is already canceled, we can return a constant object like CancellationTokenSource.canceled (not currently in the spec) (or this.canceled to support extensibility)

@rbuckton
Copy link
Collaborator Author

The only problem with returning a constant object would have is if we opt to allow custom cancellation reason propagation. Returning a constant would not propagate the reason. However, given that you are describing a custom subclass this is perhaps less of an issue.

@mayorovp
Copy link

mayorovp commented Jan 26, 2018

If one of tokens is already canceled "we" can return the cancelled token itself. Like && operator.

@rbuckton
Copy link
Collaborator Author

Closing this issue as the proposal no longer specifies cancellation behavior. See #22 for the current thinking regarding cancellation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants