From 49d55e7de04d817f0f795571015bf4a3abed445c Mon Sep 17 00:00:00 2001 From: dankeboy36 <111981763+dankeboy36@users.noreply.github.com> Date: Tue, 17 Dec 2024 12:15:16 +0100 Subject: [PATCH] fix: use empty cancellation `reason` as default (#32) * fix: use empty cancellation `reason` as default Let clients cancel the token without any reason via a convenient way. Signed-off-by: dankeboy36 * fix: remove superfluous type declaration it can be inferred from the default empty `string` Signed-off-by: dankeboy36 * Make reason parameter optional + test --------- Signed-off-by: dankeboy36 Co-authored-by: Dennis Huebner --- .../vscode-messenger-common/src/cancellation.ts | 14 +++++++------- packages/vscode-messenger-common/src/messages.ts | 2 +- .../vscode-messenger/tests/messenger.test.ts | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/packages/vscode-messenger-common/src/cancellation.ts b/packages/vscode-messenger-common/src/cancellation.ts index b2fb0b9..289f566 100644 --- a/packages/vscode-messenger-common/src/cancellation.ts +++ b/packages/vscode-messenger-common/src/cancellation.ts @@ -22,18 +22,18 @@ export class Deferred { /** * 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 = []; } @@ -41,12 +41,12 @@ export class CancellationTokenImpl implements CancellationToken { 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); } }; } diff --git a/packages/vscode-messenger-common/src/messages.ts b/packages/vscode-messenger-common/src/messages.ts index e450c2d..41e7ea3 100644 --- a/packages/vscode-messenger-common/src/messages.ts +++ b/packages/vscode-messenger-common/src/messages.ts @@ -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; } /** diff --git a/packages/vscode-messenger/tests/messenger.test.ts b/packages/vscode-messenger/tests/messenger.test.ts index 6296830..00637d3 100644 --- a/packages/vscode-messenger/tests/messenger.test.ts +++ b/packages/vscode-messenger/tests/messenger.test.ts @@ -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);