Skip to content

lspeasy / server/src / MessageDispatcher

Class: MessageDispatcher

Defined in: packages/server/src/dispatcher.ts:45

Routes incoming JSON-RPC requests and notifications to their registered handlers.

Remarks

MessageDispatcher is an internal component of LSPServer. It maintains separate handler registries for requests (which require a response) and notifications (fire-and-forget), and handles cancellation via AbortController.

Most users should interact with LSPServer.onRequest / LSPServer.onNotification instead of using MessageDispatcher directly.

Never

NEVER register the same method in both the request and notification handler registries — the dispatcher uses separate lookup tables and the method will only match one path, silently ignoring the other.

NEVER call dispatch before calling setClientCapabilities if your handler reads context.clientCapabilities — the value will be undefined until the initialize request is processed.

Constructors

Constructor

ts
new MessageDispatcher(logger): MessageDispatcher;

Defined in: packages/server/src/dispatcher.ts:55

Parameters

ParameterType
loggerLogger

Returns

MessageDispatcher

Methods

cancelRequest()

ts
cancelRequest(id): void;

Defined in: packages/server/src/dispatcher.ts:238

Cancel a pending request.

Parameters

ParameterTypeDescription
idstring | numberThe request ID to cancel.

Returns

void


clear()

ts
clear(): void;

Defined in: packages/server/src/dispatcher.ts:250

Clear all handlers

Returns

void


dispatch()

ts
dispatch(
   message, 
   transport, 
cancellationTokens): Promise<void>;

Defined in: packages/server/src/dispatcher.ts:117

Dispatch an incoming message to the registered handler.

Parameters

ParameterTypeDescription
messageMessageThe incoming JSON-RPC message to route.
transportTransportThe transport to send the response or error on.
cancellationTokensMap<string | number, AbortController>Map of pending request IDs to their AbortControllers.

Returns

Promise<void>


registerNotification()

ts
registerNotification<Params>(method, handler): void;

Defined in: packages/server/src/dispatcher.ts:78

Register a typed notification handler for the given LSP method.

Type Parameters

Type Parameter
Params

Parameters

ParameterTypeDescription
methodstringThe LSP method string (e.g. 'textDocument/didOpen').
handlerNotificationHandler<Params>The handler function to invoke for matching notifications.

Returns

void

See

NotificationHandler for the handler signature.


registerRequest()

ts
registerRequest<Params, Result>(method, handler): void;

Defined in: packages/server/src/dispatcher.ts:65

Register a typed request handler for the given LSP method.

Type Parameters

Type Parameter
Params
Result

Parameters

ParameterTypeDescription
methodstringThe LSP method string (e.g. 'textDocument/hover').
handlerRequestHandler<Params, Result>The handler function to invoke for matching requests.

Returns

void

See

RequestHandler for the handler signature.


setClientCapabilities()

ts
setClientCapabilities(capabilities): void;

Defined in: packages/server/src/dispatcher.ts:106

Set client capabilities (from initialize request)

Parameters

ParameterType
capabilitiesClientCapabilities

Returns

void


unregisterNotification()

ts
unregisterNotification(method): void;

Defined in: packages/server/src/dispatcher.ts:98

Unregister a notification handler.

Parameters

ParameterTypeDescription
methodstringThe LSP method string whose handler should be removed.

Returns

void


unregisterRequest()

ts
unregisterRequest(method): void;

Defined in: packages/server/src/dispatcher.ts:88

Unregister a request handler.

Parameters

ParameterTypeDescription
methodstringThe LSP method string whose handler should be removed.

Returns

void

Released under the MIT License.