withCleanup
Wraps a client with a cleanup function, making it Disposable.
Plugin authors can use this to register teardown logic (e.g. closing
connections or clearing timers) that runs when the client is disposed.
If the client already implements Symbol.dispose, the existing dispose
logic is chained so that it runs after the new cleanup function.
Cleanups run in reverse order of registration, disposal is idempotent, and if more than one
cleanup throws then the errors are aggregated into a SuppressedError chain. Runtimes that have
not shipped explicit resource management are supported too, though a using declaration needs a
Symbol.dispose polyfill there.
The return type is an ExtendedClient, which flattens the merged shape into a single object literal so chained calls do not accumulate nested intersections in editor tooltips and error messages.
Type Parameters
| Type Parameter | Description |
|---|---|
TClient extends object | The type of the original client. |
Parameters
| Parameter | Type | Description |
|---|---|---|
client | TClient | The client to wrap. |
cleanup | () => void | The cleanup function to run when the client is disposed. |
Returns
{ [K in string | number | symbol]: (Omit<TClient, typeof dispose> & Disposable)[K] }
A new client that extends TClient and implements Disposable.
Examples
Register a cleanup function in a plugin that opens a WebSocket connection.
Disposing without a using declaration
using requires explicit resource management, which Safari has not shipped as of Safari 27.
Either dispose the client yourself, as below, or polyfill Symbol.dispose — installing the
polyfill before any client is created, since a client registers its dispose method under
whatever Symbol.dispose was at the time.
See
Remarks
See https://caniuse.com/mdn-javascript_builtins_disposablestack for platform
availability of DisposableStack.