UNPKG

4.6 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2020 Ericsson and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.ServiceConnectionProvider = exports.RemoteConnectionProvider = exports.LocalConnectionProvider = void 0;
19const tslib_1 = require("tslib");
20const inversify_1 = require("inversify");
21const common_1 = require("../../common");
22const channel_1 = require("../../common/message-rpc/channel");
23const promise_util_1 = require("../../common/promise-util");
24const connection_source_1 = require("./connection-source");
25exports.LocalConnectionProvider = Symbol('LocalConnectionProvider');
26exports.RemoteConnectionProvider = Symbol('RemoteConnectionProvider');
27/**
28 * This class manages the channels for remote services in the back end
29 */
30let ServiceConnectionProvider = class ServiceConnectionProvider {
31 constructor() {
32 this.channelHandlers = new Map();
33 this.channelReadyDeferred = new promise_util_1.Deferred();
34 }
35 static createProxy(container, path, arg) {
36 return container.get(exports.RemoteConnectionProvider).createProxy(path, arg);
37 }
38 static createLocalProxy(container, path, arg) {
39 return container.get(exports.LocalConnectionProvider).createProxy(path, arg);
40 }
41 static createHandler(container, path, arg) {
42 const remote = container.get(exports.RemoteConnectionProvider);
43 const local = container.get(exports.LocalConnectionProvider);
44 remote.createProxy(path, arg);
45 if (remote !== local) {
46 local.createProxy(path, arg);
47 }
48 }
49 createProxy(path, arg) {
50 const factory = arg instanceof common_1.RpcProxyFactory ? arg : new common_1.RpcProxyFactory(arg);
51 this.listen(path, (_, c) => factory.listen(c), true);
52 return factory.createProxy();
53 }
54 get channelReady() {
55 return this.channelReadyDeferred.promise;
56 }
57 init() {
58 this.connectionSource.onConnectionDidOpen(channel => this.handleChannelCreated(channel));
59 }
60 /**
61 * This method must be invoked by subclasses when they have created the main channel.
62 * @param mainChannel
63 */
64 handleChannelCreated(channel) {
65 channel.onClose(() => {
66 this.handleChannelClosed(channel);
67 });
68 this.channelMultiplexer = new channel_1.ChannelMultiplexer(channel);
69 this.channelReadyDeferred.resolve();
70 for (const entry of this.channelHandlers.entries()) {
71 this.openChannel(entry[0], entry[1]);
72 }
73 }
74 handleChannelClosed(channel) {
75 this.channelReadyDeferred = new promise_util_1.Deferred();
76 }
77 /**
78 * Install a connection handler for the given path.
79 */
80 listen(path, handler, reconnect) {
81 this.openChannel(path, handler).then(() => {
82 if (reconnect) {
83 this.channelHandlers.set(path, handler);
84 }
85 });
86 }
87 async openChannel(path, handler) {
88 await this.channelReady;
89 const newChannel = await this.channelMultiplexer.open(path);
90 handler(path, newChannel);
91 }
92};
93(0, tslib_1.__decorate)([
94 (0, inversify_1.postConstruct)(),
95 (0, tslib_1.__metadata)("design:type", Function),
96 (0, tslib_1.__metadata)("design:paramtypes", []),
97 (0, tslib_1.__metadata)("design:returntype", void 0)
98], ServiceConnectionProvider.prototype, "init", null);
99(0, tslib_1.__decorate)([
100 (0, inversify_1.inject)(connection_source_1.ConnectionSource),
101 (0, tslib_1.__metadata)("design:type", Object)
102], ServiceConnectionProvider.prototype, "connectionSource", void 0);
103ServiceConnectionProvider = (0, tslib_1.__decorate)([
104 (0, inversify_1.injectable)()
105], ServiceConnectionProvider);
106exports.ServiceConnectionProvider = ServiceConnectionProvider;
107//# sourceMappingURL=service-connection-provider.js.map
\No newline at end of file