UNPKG

3.38 kBTypeScriptView Raw
1import { CancellationTokenSource } from '../cancellation';
2import { DisposableCollection } from '../disposable';
3import { Emitter, Event } from '../event';
4import { Deferred } from '../promise-util';
5import { Channel } from './channel';
6import { RpcMessage, RpcMessageDecoder, RpcMessageEncoder } from './rpc-message-encoder';
7/**
8 * Handles request messages received by the {@link RPCProtocol}.
9 */
10export declare type RequestHandler = (method: string, args: any[]) => Promise<any>;
11/**
12 * Initialization options for a {@link RpcProtocol}.
13 */
14export interface RpcProtocolOptions {
15 /**
16 * The message encoder that should be used. If `undefined` the default {@link RpcMessageEncoder} will be used.
17 */
18 encoder?: RpcMessageEncoder;
19 /**
20 * The message decoder that should be used. If `undefined` the default {@link RpcMessageDecoder} will be used.
21 */
22 decoder?: RpcMessageDecoder;
23 /**
24 * The runtime mode determines whether the RPC protocol is bi-directional (default) or acts as a client or server only.
25 */
26 mode?: 'default' | 'clientOnly' | 'serverOnly';
27}
28/**
29 * Establish a RPC protocol on top of a given channel. By default the rpc protocol is bi-directional, meaning it is possible to send
30 * requests and notifications to the remote side (i.e. acts as client) as well as receiving requests and notifications from the remote side (i.e. acts as a server).
31 * Clients can get a promise for a remote request result that will be either resolved or
32 * rejected depending on the success of the request. Keeps track of outstanding requests and matches replies to the appropriate request
33 * Currently, there is no timeout handling for long running requests implemented.
34 * The bi-directional mode can be reconfigured using the {@link RpcProtocolOptions} to construct an RPC protocol instance that acts only as client or server instead.
35 */
36export declare class RpcProtocol {
37 readonly channel: Channel;
38 readonly requestHandler: RequestHandler | undefined;
39 static readonly CANCELLATION_TOKEN_KEY = "add.cancellation.token";
40 protected readonly pendingRequests: Map<number, Deferred<any>>;
41 protected nextMessageId: number;
42 protected readonly encoder: RpcMessageEncoder;
43 protected readonly decoder: RpcMessageDecoder;
44 protected readonly mode: 'default' | 'clientOnly' | 'serverOnly';
45 protected readonly onNotificationEmitter: Emitter<{
46 method: string;
47 args: any[];
48 }>;
49 protected readonly cancellationTokenSources: Map<number, CancellationTokenSource>;
50 get onNotification(): Event<{
51 method: string;
52 args: any[];
53 }>;
54 protected toDispose: DisposableCollection;
55 constructor(channel: Channel, requestHandler: RequestHandler | undefined, options?: RpcProtocolOptions);
56 handleMessage(message: RpcMessage): void;
57 protected handleReply(id: number, value: any): void;
58 protected handleReplyErr(id: number, error: any): void;
59 sendRequest<T>(method: string, args: any[]): Promise<T>;
60 sendNotification(method: string, args: any[]): void;
61 sendCancel(requestId: number): void;
62 protected handleCancel(id: number): void;
63 protected handleRequest(id: number, method: string, args: any[]): Promise<void>;
64 protected handleNotify(id: number, method: string, args: any[]): Promise<void>;
65}
66//# sourceMappingURL=rpc-protocol.d.ts.map
\No newline at end of file