上一篇:Theia APIs——事件html
import { ContainerModule } from 'inversify'; import { ConnectionHandler, JsonRpcConnectionHandler } from "../../messaging/common"; import { ILoggerServer, ILoggerClient } from '../../application/common/logger-protocol'; export const loggerServerModule = new ContainerModule(bind => { bind(ConnectionHandler).toDynamicValue(ctx => new JsonRpcConnectionHandler<ILoggerClient>("/services/logger", client => { const loggerServer = ctx.container.get<ILoggerServer>(ILoggerServer); loggerServer.setClient(client); return loggerServer; }) ).inSingletonScope() });
咱们来详细看一下:前端
import { ConnectionHandler, JsonRpcConnectionHandler } from "../../messaging/common";
import { MessageConnection } from "vscode-jsonrpc"; export const ConnectionHandler = Symbol('ConnectionHandler'); export interface ConnectionHandler { readonly path: string; onConnection(connection: MessageConnection): void; }
import { ILoggerServer, ILoggerClient } from '../../application/common/logger-protocol';
文件logger-protocol.ts包含了服务器和客户端须要实现的接口。node
bind<ConnectionHandler>(ConnectionHandler).toDynamicValue(ctx => {
constructor( @inject(ContributionProvider) @named(ConnectionHandler) protected readonly handlers: ContributionProvider<ConnectionHandler>) { } onStart(server: http.Server): void { for (const handler of this.handlers.getContributions()) { const path = handler.path; try { createServerWebSocketConnection({ server, path }, connection => handler.onConnection(connection)); } catch (error) { console.error(error) } } }
new JsonRpcConnectionHandler<ILoggerClient>("/services/logger", client => {
咱们来看看这个类的实现作了哪些事情:webpack
export class JsonRpcConnectionHandler<T extends object> implements ConnectionHandler { constructor( readonly path: string, readonly targetFactory: (proxy: JsonRpcProxy<T>) => any ) { } onConnection(connection: MessageConnection): void { const factory = new JsonRpcProxyFactory<T>(this.path); const proxy = factory.createProxy(); factory.target = this.targetFactory(proxy); factory.listen(connection); } }
onConnection(connection: MessageConnection): void { const factory = new JsonRpcProxyFactory<T>(this.path); const proxy = factory.createProxy(); factory.target = this.targetFactory(proxy); factory.listen(connection);
咱们一行一行来看:git
const factory = new JsonRpcProxyFactory<T>(this.path);
上面这一行在路径"/services/logger"上建立了一个JsonRpcProxy。github
const proxy = factory.createProxy();
而后,咱们从工厂建立了一个代理对象,它将使用ILoggerClient接口来调用JSON-RPC链接的另外一端。web
factory.target = this.targetFactory(proxy);
上面这一行将调用咱们在参数中传递的函数,因此:express
client => { const loggerServer = ctx.container.get<ILoggerServer>(ILoggerServer); loggerServer.setClient(client); return loggerServer; }
factory.listen(connection);
'/services/*': { target: 'ws://localhost:3000', ws: true },
import { ContainerModule, Container } from 'inversify'; import { WebSocketConnectionProvider } from '../../messaging/browser/connection'; import { ILogger, LoggerFactory, LoggerOptions, Logger } from '../common/logger'; import { ILoggerServer } from '../common/logger-protocol'; import { LoggerWatcher } from '../common/logger-watcher'; export const loggerFrontendModule = new ContainerModule(bind => { bind(ILogger).to(Logger).inSingletonScope(); bind(LoggerWatcher).toSelf().inSingletonScope(); bind(ILoggerServer).toDynamicValue(ctx => { const loggerWatcher = ctx.container.get(LoggerWatcher); const connection = ctx.container.get(WebSocketConnectionProvider); return connection.createProxy<ILoggerServer>("/services/logger", loggerWatcher.getLoggerClient()); }).inSingletonScope(); });
其中最重要的几行:json
bind(ILoggerServer).toDynamicValue(ctx => { const loggerWatcher = ctx.container.get(LoggerWatcher); const connection = ctx.container.get(WebSocketConnectionProvider); return connection.createProxy<ILoggerServer>("/services/logger", loggerWatcher.getLoggerClient()); }).inSingletonScope();
咱们一行一行来看:后端
const loggerWatcher = ctx.container.get(LoggerWatcher);
const connection = ctx.container.get(WebSocketConnectionProvider);
上面这一行得到了一个websocket链接,它将被用来建立一个代理。
return connection.createProxy<ILoggerServer>("/services/logger", loggerWatcher.getLoggerClient());
咱们将一个本地对象做为第二个参数传入,用来处理来自远程对象的JSON-RPC消息。有时,本地对象依赖于代理,在代理实例化以前没法实例化。这种状况下,代理接口应该实现JsonRpcServer,而本地对象应该做为客户端来提供。
export type JsonRpcServer<Client> = Disposable & { setClient(client: Client | undefined): void; }; export interface ILoggerServer extends JsonRpcServery<ILoggerClient> { // ... } const serverProxy = connection.createProxy<ILoggerServer>("/services/logger"); const client = loggerWatcher.getLoggerClient(); serverProxy.setClient(client);
createProxy<T extends object>(path: string, target?: object, options?: WebSocketOptions): T { const factory = new JsonRpcProxyFactory<T>(path, target); this.listen(factory, options); return factory.createProxy(); }
import { loggerServerModule } from 'theia-core/lib/application/node/logger-server-module';
而后将其载入到主容器。
container.load(loggerServerModule);
import { loggerFrontendModule } from 'theia-core/lib/application/browser/logger-frontend-module';
container.load(frontendLanguagesModule);