You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Consider supporting/merging with the cancellationtoken npm package #855
vscode-jsonrpc defines its own CancellationToken interface which is modeled after the .NET type by the same name. While this is a fine type to use, it's tucked inside an RPC library which doesn't make it particularly nice to use in other settings.
There is a cancellationtoken npm package that focuses just on such a token. It is a more capable token, offering throwIfCancelled(), timeouts, optional reasons for cancellation, an api to race a token against another promise, and combining multiple tokens into one as a race or as a union of all. While it can do everything the .NET version can, the API is designed to be more javascript-esque, so it feels more native.
Given these two options, in some libraries I would tend to prefer the latter token. But where I have to interop with vscode-jsonrpc this complicates the story and leads to potential bugs when the wrong token is given or expected.
Can vscode-jsonrpc be updated to support this cancellationtoken npm package?
In particular, I'm thinking you could do duck-type checking to detect which kind it is and work with both. When creating a token to pass to RPC servers, perhaps you could create a token which fulfills the API obligations of both so that the RPC server can be implemented to receive either one.
Here are adapters I wrote that makes either token look like the other, to demonstrate how simple this is. Note that creating a union type to provide to RPC server methods would probably be more complicated.
importCancellationTokenfrom'cancellationtoken';import{CancellationTokenasvscodeCancellationToken,CancellationTokenSourceasvscodeCancellationTokenSource,}from'vscode-jsonrpc';exportclassCancellationTokenAdapters{/** Tests whether an object satisfies the {@link vscodeCancellationToken} interface. */staticIsVSCode(value: any): value is vscodeCancellationToken{returnvscodeCancellationToken.is(value);}/** Tests whether an object satisfies the {@link CancellationToken} interface. */staticIsCancellationToken(value: any): value is CancellationToken{returnvalue&&typeofvalue.throwIfCancelled==='function'&&typeofvalue.onCancelled==='function'&&value.isCancelled!==undefined&&value.canBeCancelled!==undefined;}/** Returns a {@link CancellationToken} that is linked to the given {@link vscodeCancellationToken}. */staticVSCodeToCancellationToken(cancellationToken: vscodeCancellationToken): CancellationToken{if(cancellationToken.isCancellationRequested){returnCancellationToken.CANCELLED;}elseif(cancellationToken===vscodeCancellationToken.None){returnCancellationToken.CONTINUE;}else{constresult=CancellationToken.create();cancellationToken.onCancellationRequested(_=>result.cancel());returnresult.token;}}/** Returns a {@link vscodeCancellationToken} that is linked to the given {@link CancellationToken}. */staticCancellationTokenToVSCode(cancellationToken: CancellationToken): vscodeCancellationToken{if(cancellationToken.isCancelled){returnvscodeCancellationToken.Cancelled;}elseif(cancellationToken.canBeCancelled){constcts=newvscodeCancellationTokenSource();cancellationToken.onCancelled(_=>cts.cancel());returncts.token;}else{returnvscodeCancellationToken.None;}}}
Activity
dkattan commentedon Dec 29, 2021
I just noticed this same thing, +1 for this