1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.ServiceConnectionProvider = exports.RemoteConnectionProvider = exports.LocalConnectionProvider = void 0;
|
19 | const tslib_1 = require("tslib");
|
20 | const inversify_1 = require("inversify");
|
21 | const common_1 = require("../../common");
|
22 | const channel_1 = require("../../common/message-rpc/channel");
|
23 | const promise_util_1 = require("../../common/promise-util");
|
24 | const connection_source_1 = require("./connection-source");
|
25 | exports.LocalConnectionProvider = Symbol('LocalConnectionProvider');
|
26 | exports.RemoteConnectionProvider = Symbol('RemoteConnectionProvider');
|
27 |
|
28 |
|
29 |
|
30 | let 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 |
|
62 |
|
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 |
|
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);
|
103 | ServiceConnectionProvider = (0, tslib_1.__decorate)([
|
104 | (0, inversify_1.injectable)()
|
105 | ], ServiceConnectionProvider);
|
106 | exports.ServiceConnectionProvider = ServiceConnectionProvider;
|
107 |
|
\ | No newline at end of file |