UNPKG

7.28 kBTypeScriptView Raw
1import { Disposable } from '../disposable';
2import { Emitter, Event } from '../event';
3import { Channel } from '../message-rpc/channel';
4import { RequestHandler, RpcProtocol } from '../message-rpc/rpc-protocol';
5import { ConnectionHandler } from './handler';
6import { Deferred } from '../promise-util';
7export declare type RpcServer<Client> = Disposable & {
8 /**
9 * If this server is a proxy to a remote server then
10 * a client is used as a local object
11 * to handle RPC messages from the remote server.
12 */
13 setClient(client: Client | undefined): void;
14 getClient?(): Client | undefined;
15};
16export interface RpcConnectionEventEmitter {
17 readonly onDidOpenConnection: Event<void>;
18 readonly onDidCloseConnection: Event<void>;
19}
20export declare type RpcProxy<T> = T & RpcConnectionEventEmitter;
21export declare class RpcConnectionHandler<T extends object> implements ConnectionHandler {
22 readonly path: string;
23 readonly targetFactory: (proxy: RpcProxy<T>) => any;
24 readonly factoryConstructor: new () => RpcProxyFactory<T>;
25 constructor(path: string, targetFactory: (proxy: RpcProxy<T>) => any, factoryConstructor?: new () => RpcProxyFactory<T>);
26 onConnection(connection: Channel): void;
27}
28/**
29 * Factory for creating a new {@link RpcProtocol} for a given chanel and {@link RequestHandler}.
30 */
31export declare type RpcProtocolFactory = (channel: Channel, requestHandler: RequestHandler) => RpcProtocol;
32/**
33 * Factory for RPC proxy objects.
34 *
35 * A RPC proxy exposes the programmatic interface of an object through
36 * Theia's RPC protocol. This allows remote programs to call methods of this objects by
37 * sending RPC requests. This takes place over a bi-directional stream,
38 * where both ends can expose an object and both can call methods on each other'
39 * exposed object.
40 *
41 * For example, assuming we have an object of the following type on one end:
42 *
43 * class Foo {
44 * bar(baz: number): number { return baz + 1 }
45 * }
46 *
47 * which we want to expose through a RPC interface. We would do:
48 *
49 * let target = new Foo()
50 * let factory = new RpcProxyFactory<Foo>('/foo', target)
51 * factory.onConnection(connection)
52 *
53 * The party at the other end of the `connection`, in order to remotely call
54 * methods on this object would do:
55 *
56 * let factory = new RpcProxyFactory<Foo>('/foo')
57 * factory.onConnection(connection)
58 * let proxy = factory.createProxy();
59 * let result = proxy.bar(42)
60 * // result is equal to 43
61 *
62 * One the wire, it would look like this:
63 *
64 * --> { "type":"1", "id": 1, "method": "bar", "args": [42]}
65 * <-- { "type":"3", "id": 1, "res": 43}
66 *
67 * Note that in the code of the caller, we didn't pass a target object to
68 * RpcProxyFactory, because we don't want/need to expose an object.
69 * If we had passed a target object, the other side could've called methods on
70 * it.
71 *
72 * @param <T> - The type of the object to expose to RPC.
73 */
74export declare class RpcProxyFactory<T extends object> implements ProxyHandler<T> {
75 target?: any;
76 protected rpcProtocolFactory: RpcProtocolFactory;
77 protected readonly onDidOpenConnectionEmitter: Emitter<void>;
78 protected readonly onDidCloseConnectionEmitter: Emitter<void>;
79 protected rpcDeferred: Deferred<RpcProtocol>;
80 /**
81 * Build a new RpcProxyFactory.
82 *
83 * @param target - The object to expose to RPC methods calls. If this
84 * is omitted, the proxy won't be able to handle requests, only send them.
85 */
86 constructor(target?: any, rpcProtocolFactory?: RpcProtocolFactory);
87 protected waitForConnection(): void;
88 /**
89 * Connect a {@link Channel} to the factory by creating an {@link RpcProtocol} on top of it.
90 *
91 * This protocol will be used to send/receive RPC requests and
92 * responses.
93 */
94 listen(channel: Channel): void;
95 /**
96 * Process an incoming RPC method call.
97 *
98 * onRequest is called when the RPC connection received a method call
99 * request. It calls the corresponding method on [[target]].
100 *
101 * The return value is a Promise object that is resolved with the return
102 * value of the method call, if it is successful. The promise is rejected
103 * if the called method does not exist or if it throws.
104 *
105 * @returns A promise of the method call completion.
106 */
107 protected onRequest(method: string, ...args: any[]): Promise<any>;
108 /**
109 * Process an incoming RPC notification.
110 *
111 * Same as [[onRequest]], but called on incoming notifications rather than
112 * methods calls.
113 */
114 protected onNotification(method: string, ...args: any[]): void;
115 /**
116 * Create a Proxy exposing the interface of an object of type T. This Proxy
117 * can be used to do RPC method calls on the remote target object as
118 * if it was local.
119 *
120 * If `T` implements `RpcServer` then a client is used as a target object for a remote target object.
121 */
122 createProxy(): RpcProxy<T>;
123 /**
124 * Get a callable object that executes a RPC method call.
125 *
126 * Getting a property on the Proxy object returns a callable that, when
127 * called, executes a RPC call. The name of the property defines the
128 * method to be called. The callable takes a variable number of arguments,
129 * which are passed in the RPC method call.
130 *
131 * For example, if you have a Proxy object:
132 *
133 * let fooProxyFactory = RpcProxyFactory<Foo>('/foo')
134 * let fooProxy = fooProxyFactory.createProxy()
135 *
136 * accessing `fooProxy.bar` will return a callable that, when called,
137 * executes a RPC method call to method `bar`. Therefore, doing
138 * `fooProxy.bar()` will call the `bar` method on the remote Foo object.
139 *
140 * @param target - unused.
141 * @param p - The property accessed on the Proxy object.
142 * @param receiver - unused.
143 * @returns A callable that executes the RPC call.
144 */
145 get(target: T, p: PropertyKey, receiver: any): any;
146 /**
147 * Return whether the given property represents a notification.
148 *
149 * A property leads to a notification rather than a method call if its name
150 * begins with `notify` or `on`.
151 *
152 * @param p - The property being called on the proxy.
153 * @return Whether `p` represents a notification.
154 */
155 protected isNotification(p: PropertyKey): boolean;
156 protected serializeError(e: any): any;
157 protected deserializeError(capturedError: Error, e: any): any;
158}
159/**
160 * @deprecated since 1.39.0 use `RpcConnectionEventEmitter` instead
161 */
162export declare type JsonRpcConnectionEventEmitter = RpcConnectionEventEmitter;
163/**
164 * @deprecated since 1.39.0 use `RpcServer` instead
165 */
166export declare type JsonRpcServer<Client> = RpcServer<Client>;
167/**
168 * @deprecated since 1.39.0 use `RpcProxy` instead
169 */
170export declare type JsonRpcProxy<T> = RpcProxy<T>;
171/**
172 * @deprecated since 1.39.0 use `RpcConnectionHandler` instead
173 */
174export declare class JsonRpcConnectionHandler<T extends object> extends RpcConnectionHandler<T> {
175}
176/**
177 * @deprecated since 1.39.0 use `RpcProxyFactory` instead
178 */
179export declare class JsonRpcProxyFactory<T extends object> extends RpcProxyFactory<T> {
180}
181//# sourceMappingURL=proxy-factory.d.ts.map
\No newline at end of file