-
Notifications
You must be signed in to change notification settings - Fork 581
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
Add cancellation token package #2
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/node_modules/ | ||
*.js | ||
*.js.map | ||
*.d.ts |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {Token} from "../lib/Token"; | ||
import {TokenSource} from "../lib/TokenSource"; | ||
|
||
describe('Token', () => { | ||
it('should not be cancellable if no source provided at construction', () => { | ||
const token = new Token(); | ||
|
||
expect(token.canBeCancelled).toBe(false); | ||
expect(token.isCancellationRequested).toBe(false); | ||
}); | ||
|
||
it('should defer cancellation queries to parent token source', () => { | ||
const source = {isCancellationRequested: true}; | ||
const token = new Token(<TokenSource>source); | ||
|
||
expect(token.isCancellationRequested).toBe(true); | ||
source.isCancellationRequested = false; | ||
expect(token.isCancellationRequested).toBe(false); | ||
}); | ||
|
||
it( | ||
'should register cancellation handlers when onCancellationRequested called', | ||
() => { | ||
const source = new TokenSource(); | ||
source.registerCancellationHandler = jest.fn(); | ||
const token = new Token(source); | ||
|
||
const cb = () => {}; | ||
token.onCancellationRequested(cb); | ||
expect(source.registerCancellationHandler).toHaveBeenCalledWith(cb); | ||
} | ||
); | ||
|
||
it( | ||
'should throw if cancellation requested and throwIfCancellationRequested called', | ||
() => { | ||
const token = new Token(<TokenSource>{isCancellationRequested: true}); | ||
|
||
expect(() => { | ||
token.throwIfCancellationRequested('PANIC PANIC PANIC'); | ||
}).toThrow('PANIC PANIC PANIC'); | ||
} | ||
); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import {Token} from "../lib/Token"; | ||
import {TokenSource} from "../lib/TokenSource"; | ||
|
||
jest.useFakeTimers(); | ||
|
||
describe('TokenSource', () => { | ||
it('should return a new token on each property access', () => { | ||
const source = new TokenSource(); | ||
const token = source.getToken(); | ||
expect(token).toBeInstanceOf(Token); | ||
expect(source.getToken()).not.toBe(token); | ||
}); | ||
|
||
it( | ||
'should report that cancellation was requested after cancel called', | ||
() => { | ||
const source = new TokenSource(); | ||
expect(source.isCancellationRequested).toBe(false); | ||
source.cancel(); | ||
expect(source.isCancellationRequested).toBe(true); | ||
} | ||
); | ||
|
||
it('should invoke registered cancellation handlers on cancellation', () => { | ||
const source = new TokenSource(); | ||
const cb = jest.fn(); | ||
source.registerCancellationHandler(cb); | ||
expect(cb).not.toHaveBeenCalled(); | ||
source.cancel(); | ||
jest.runAllTimers(); | ||
expect(cb).toHaveBeenCalled(); | ||
}); | ||
|
||
it( | ||
'should not invoke registered handlers if silent cancellation requested', | ||
() => { | ||
const source = new TokenSource(); | ||
const cb = jest.fn(); | ||
source.registerCancellationHandler(cb); | ||
expect(cb).not.toHaveBeenCalled(); | ||
source.cancel(false); | ||
jest.runAllTimers(); | ||
expect(cb).not.toHaveBeenCalled(); | ||
} | ||
); | ||
|
||
it( | ||
'should invoke cancellation handlers immediately if cancellation already requested', | ||
() => { | ||
const timeoutMock = <any>setTimeout; | ||
|
||
const source = new TokenSource(); | ||
const cb = jest.fn(); | ||
source.cancel(false); | ||
source.registerCancellationHandler(cb); | ||
expect(timeoutMock).toHaveBeenCalledWith(cb, 0); | ||
jest.runAllTimers(); | ||
expect(cb).toHaveBeenCalled(); | ||
} | ||
); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./lib/Token"; | ||
export * from "./lib/TokenSource"; |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
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 canBeCancelled: 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.canBeCancelled = Boolean(source); | ||
} | ||
|
||
/** | ||
* Whether the associated operation has already been cancelled. | ||
*/ | ||
get isCancellationRequested(): boolean { | ||
if (this.source) { | ||
return this.source.isCancellationRequested; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* 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.isCancellationRequested) { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import {Token} from "./Token"; | ||
|
||
/** | ||
* The AWS SDK uses a TokenSource/Token model to allow for cooperative | ||
* cancellation of asynchronous operations. When initiating such an operation, | ||
* the caller can create a TokenSource and then provide linked tokens to | ||
* subtasks. This allows a single source to signal to multiple consumers that a | ||
* cancellation has been requested without dictating how that cancellation | ||
* should be handled. | ||
* | ||
* Holders of a TokenSource object may create new tokens, register cancellation | ||
* handlers, or declare an operation cancelled (thereby invoking any registered | ||
* handlers). | ||
*/ | ||
export class TokenSource { | ||
private _cancellationRequested: boolean = false; | ||
private _invokeAfterCancellation: Array<() => void> = []; | ||
|
||
/** | ||
* Whether the operation associated with this TokenSource has been | ||
* cancelled. | ||
*/ | ||
get isCancellationRequested(): boolean { | ||
return this._cancellationRequested; | ||
} | ||
|
||
/** | ||
* Declares the operation associated with this TokenSource to be cancelled | ||
* and invokes any registered cancellation handlers. The latter may be | ||
* skipped if so desired. | ||
*/ | ||
cancel(invokeRegisteredActions: boolean = true): void { | ||
this._cancellationRequested = true; | ||
|
||
if (invokeRegisteredActions) { | ||
while (this._invokeAfterCancellation.length > 0) { | ||
let action = this._invokeAfterCancellation.shift(); | ||
if (action) { | ||
setTimeout(action, 0); | ||
} | ||
} | ||
} | ||
} | ||
|
||
getToken(): Token { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: The docs for |
||
return new Token(this); | ||
} | ||
|
||
/** | ||
* Adds a handler to be invoked when cancellation of the associated | ||
* operation has been requested. If cancellation has already been requested, | ||
* the handler will be invoked on the next tick of the event loop. | ||
*/ | ||
registerCancellationHandler(handler: () => void): void { | ||
if (this._cancellationRequested) { | ||
setTimeout(handler, 0); | ||
} else { | ||
this._invokeAfterCancellation.push(handler); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "@aws/cancellation-token", | ||
"version": "0.0.1", | ||
"private": true, | ||
"description": "A simple cancellation token library", | ||
"main": "index.js", | ||
"scripts": { | ||
"prepublishOnly": "tsc", | ||
"pretest": "tsc", | ||
"test": "jest" | ||
}, | ||
"author": "[email protected]", | ||
"license": "UNLICENSED", | ||
"devDependencies": { | ||
"@types/jest": "^19.2.2", | ||
"jest": "^19.0.2", | ||
"typescript": "^2.3" | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"target": "es5", | ||
"strict": true, | ||
"sourceMap": true, | ||
"declaration": true, | ||
"stripInternal": true | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: I actually liked
cancellable
, but it doesn't really matter.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed.