UNPKG

2.85 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 RpcServer}.
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/**
25 * Establish a bi-directional RPC protocol on top of a given channel. Bi-directional means to send
26 * sends requests and notifications to the remote side as well as receiving requests and notifications from the remote side.
27 * Clients can get a promise for a remote request result that will be either resolved or
28 * rejected depending on the success of the request. Keeps track of outstanding requests and matches replies to the appropriate request
29 * Currently, there is no timeout handling for long running requests implemented.
30 */
31export declare class RpcProtocol {
32 readonly channel: Channel;
33 readonly requestHandler: RequestHandler;
34 static readonly CANCELLATION_TOKEN_KEY = "add.cancellation.token";
35 protected readonly pendingRequests: Map<number, Deferred<any>>;
36 protected nextMessageId: number;
37 protected readonly encoder: RpcMessageEncoder;
38 protected readonly decoder: RpcMessageDecoder;
39 protected readonly onNotificationEmitter: Emitter<{
40 method: string;
41 args: any[];
42 }>;
43 protected readonly cancellationTokenSources: Map<number, CancellationTokenSource>;
44 get onNotification(): Event<{
45 method: string;
46 args: any[];
47 }>;
48 protected toDispose: DisposableCollection;
49 constructor(channel: Channel, requestHandler: RequestHandler, options?: RpcProtocolOptions);
50 handleMessage(message: RpcMessage): void;
51 protected handleReply(id: number, value: any): void;
52 protected handleReplyErr(id: number, error: any): void;
53 sendRequest<T>(method: string, args: any[]): Promise<T>;
54 sendNotification(method: string, args: any[]): void;
55 sendCancel(requestId: number): void;
56 protected handleCancel(id: number): void;
57 protected handleRequest(id: number, method: string, args: any[]): Promise<void>;
58 protected handleNotify(id: number, method: string, args: any[]): Promise<void>;
59}
60//# sourceMappingURL=rpc-protocol.d.ts.map
\No newline at end of file