-
Notifications
You must be signed in to change notification settings - Fork 581
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add documentation for TokenSource/Token
- Loading branch information
Showing
3 changed files
with
69 additions
and
7 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
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,37 +1,69 @@ | ||
import {TokenSource} from './TokenSource'; | ||
|
||
/** | ||
* @see {TokenSource} | ||
* | ||
* Holders of a Token object may query if the associated operation has been | ||
* cancelled, register cancellation handlers, and conditionally throw an Error | ||
* if the operation has already been cancelled. | ||
*/ | ||
export class Token { | ||
/** | ||
* Whether the associated operation may be cancelled at some point in the | ||
* future. | ||
*/ | ||
public readonly cancellable: boolean; | ||
|
||
/** | ||
* Creates a new Token linked to a provided TokenSource. If no source is | ||
* provided, the Token cannot be cancelled. | ||
*/ | ||
constructor(private readonly source?: TokenSource) { | ||
this.cancellable = Boolean(source); | ||
} | ||
|
||
get canBeCanceled(): boolean { | ||
/** | ||
* Alias of this.cancellable | ||
*/ | ||
get canBeCancelled(): boolean { | ||
return this.cancellable; | ||
} | ||
|
||
get canceled(): boolean { | ||
/** | ||
* Whether the associated operation has already been cancelled. | ||
*/ | ||
get cancelled(): boolean { | ||
if (this.source) { | ||
return this.source.isCancellationRequested; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Alias of this.canceled | ||
*/ | ||
get isCancellationRequested(): boolean { | ||
return this.canceled; | ||
return this.cancelled; | ||
} | ||
|
||
/** | ||
* Registers a handler to be invoked when cancellation is requested. If | ||
* cancellation has already been requested, the handler will be invoked on | ||
* the next tick of the event loop. | ||
*/ | ||
onCancellationRequested(cb: () => void): void { | ||
if (this.source) { | ||
this.source.registerCancellationHandler(cb); | ||
} | ||
} | ||
|
||
/** | ||
* Throws an error if the associated operation has already been cancelled. | ||
*/ | ||
throwIfCancellationRequested(reason?: string): void { | ||
if (this.canceled) { | ||
throw new Error(reason); | ||
if (this.cancelled) { | ||
throw new Error(`Operation cancelled${reason ? `: ${reason}` : ''}`); | ||
} | ||
} | ||
} |
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