lspeasy / core/src / CancellationToken
Interface: CancellationToken
Defined in: packages/core/src/utils/cancellation.ts:52
Read-only handle for observing cancellation state.
Remarks
Pass a CancellationToken to long-running request handlers so they can honour client-initiated cancellation ($/cancelRequest notification). The token's isCancellationRequested property changes from false to true exactly once; it never resets.
Use CancellationTokenNone (exported as a constant) when no cancellation support is needed — it is a singleton that is never cancelled.
Use When
You are implementing a RequestHandler that performs async work (file I/O, database queries, tree-sitter parsing) and want to stop early when the client cancels.
Avoid When
The handler is synchronous and returns in less than a few milliseconds. Polling isCancellationRequested in a tight loop has negligible benefit.
Never
NEVER ignore the cancellation token on long-running requests. If a client sends $/cancelRequest and the server does not stop processing, the client times out and receives no usable feedback. Always check token.isCancellationRequested at async yield points.
Example
import { LSPServer } from '@lspeasy/server';
const server = new LSPServer();
server.onRequest('textDocument/hover', async (params, token) => {
const lines = await readLargeFile(params.textDocument.uri);
if (token.isCancellationRequested) return null;
// ... compute hover
return { contents: { kind: 'plaintext', value: 'info' } };
});Properties
| Property | Modifier | Type | Description | Defined in |
|---|---|---|---|---|
isCancellationRequested | readonly | boolean | true once cancellation has been requested; never resets to false. | packages/core/src/utils/cancellation.ts:56 |
Methods
onCancellationRequested()
onCancellationRequested(callback): Disposable;Defined in: packages/core/src/utils/cancellation.ts:64
Registers a callback invoked when (or immediately if already) cancelled.
Parameters
| Parameter | Type | Description |
|---|---|---|
callback | () => void | Called when cancellation is requested. |
Returns
A Disposable to unregister the callback.