Skip to content

lspeasy / core/src / CancellationTokenSource

Class: CancellationTokenSource

Defined in: packages/core/src/utils/cancellation.ts:102

Controller that creates and manages a CancellationToken.

Remarks

Holds the mutable end of the cancellation pair: call .cancel() to signal cancellation; pass .token (read-only) to handlers and async operations. Always call .dispose() when the operation completes to free internal event-emitter resources, regardless of whether cancellation was requested.

Example

ts
import { CancellationTokenSource } from '@lspeasy/core';
import { LSPClient } from '@lspeasy/client';

const source = new CancellationTokenSource();
const promise = client.sendRequest('textDocument/hover', params, source.token);

// Cancel from user interaction
cancelButton.addEventListener('click', () => source.cancel());

try {
  const result = await promise;
} catch {
  // Cancelled
} finally {
  source.dispose();
}

Constructors

Constructor

ts
new CancellationTokenSource(): CancellationTokenSource;

Defined in: packages/core/src/utils/cancellation.ts:107

Returns

CancellationTokenSource

Accessors

token

Get Signature

ts
get token(): CancellationToken;

Defined in: packages/core/src/utils/cancellation.ts:137

Get the token

Returns

CancellationToken

Methods

cancel()

ts
cancel(): void;

Defined in: packages/core/src/utils/cancellation.ts:144

Signal cancellation

Returns

void


dispose()

ts
dispose(): void;

Defined in: packages/core/src/utils/cancellation.ts:156

Dispose the source

Returns

void

Released under the MIT License.