Skip to content

Commit

Permalink
fix: use empty cancellation reason as default (#32)
Browse files Browse the repository at this point in the history
* fix: use empty cancellation `reason` as default

Let clients cancel the token without any reason via a convenient way.

Signed-off-by: dankeboy36 <[email protected]>

* fix: remove superfluous type declaration

it can be inferred from the default empty `string`

Signed-off-by: dankeboy36 <[email protected]>

* Make reason parameter optional + test

---------

Signed-off-by: dankeboy36 <[email protected]>
Co-authored-by: Dennis Huebner <[email protected]>
  • Loading branch information
dankeboy36 and dhuebner authored Dec 17, 2024
1 parent d281883 commit 49d55e7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
14 changes: 7 additions & 7 deletions packages/vscode-messenger-common/src/cancellation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,31 @@ export class Deferred<R = any> {

/**
* Implementation of the CancellationToken interface.
* Allows to trigger cancelation.
* Allows to trigger cancellation.
*/
export class CancellationTokenImpl implements CancellationToken {
private canceled = false;
private listeners: Array<((reason: string) => void)> = [];
private listeners: Array<((reason?: string) => void)> = [];

public cancel(reason: string): void {
public cancel(reason?: string): void {
if (this.canceled) {
throw new Error('Request was already canceled.');
}
this.canceled = true;
this.listeners.forEach(callBack => callBack(reason));
this.listeners.forEach(callback => callback(reason));
this.listeners = [];
}

get isCancellationRequested(): boolean {
return this.canceled;
}

public onCancellationRequested(callBack: (reason: string) => void): Disposable {
this.listeners.push(callBack);
public onCancellationRequested(callback: (reason?: string) => void): Disposable {
this.listeners.push(callback);
const listeners = this.listeners;
return {
dispose() {
listeners.splice(listeners.indexOf(callBack), 1);
listeners.splice(listeners.indexOf(callback), 1);
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode-messenger-common/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export interface MessengerAPI {
*/
export interface CancellationToken {
readonly isCancellationRequested: boolean;
onCancellationRequested(callBack: (reason: string) => void): Disposable;
onCancellationRequested(callBack: (reason?: string) => void): Disposable;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions packages/vscode-messenger/tests/messenger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,22 @@ describe('Extension Messenger', () => {
expect((cancel as any).listeners.length).toBe(0);
});

test('Cancel request - no reason', async () => {
const messenger = new Messenger();
messenger.registerWebviewView(view1);
const cancel: CancellationTokenImpl = new CancellationTokenImpl();
setTimeout(() =>
cancel.cancel(), 300);
await messenger.sendRequest(simpleRequest, ViewParticipant(VIEW_TYPE_1), FORCE_HANDLER_TO_WAIT_PARAM, cancel)
.then(() => {
throw new Error('Expected to throw error');
}).catch((error) => {
expect(error.message).toBe('');
});
// check the internal cancelation listener attached in `sendRequestToWebview` was removed
expect((cancel as any).listeners.length).toBe(0);
});

test('Handle cancel request', async () => {
const messenger = new Messenger();
messenger.registerWebviewView(view1);
Expand Down

0 comments on commit 49d55e7

Please sign in to comment.