UNPKG

4.28 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 WITH Classpath-exception-2.0
16// *****************************************************************************
17var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21 return c > 3 && r && Object.defineProperty(target, key, r), r;
22};
23Object.defineProperty(exports, "__esModule", { value: true });
24exports.AbstractConnectionProvider = void 0;
25const inversify_1 = require("inversify");
26const event_1 = require("../event");
27const proxy_factory_1 = require("./proxy-factory");
28const channel_1 = require("../message-rpc/channel");
29/**
30 * Factor common logic according to `ElectronIpcConnectionProvider` and
31 * `WebSocketConnectionProvider`. This class handles channels in a somewhat
32 * generic way.
33 */
34let AbstractConnectionProvider = class AbstractConnectionProvider {
35 constructor() {
36 this.onIncomingMessageActivityEmitter = new event_1.Emitter();
37 // A set of channel opening functions that are executed if the backend reconnects to restore the
38 // the channels that were open before the disconnect occurred.
39 this.reconnectChannelOpeners = [];
40 }
41 /**
42 * Create a proxy object to remote interface of T type
43 * over an electron ipc connection for the given path.
44 *
45 * An optional target can be provided to handle
46 * notifications and requests from a remote side.
47 */
48 static createProxy(container, path, target) {
49 throw new Error('abstract');
50 }
51 get onIncomingMessageActivity() {
52 return this.onIncomingMessageActivityEmitter.event;
53 }
54 createProxy(path, arg) {
55 const factory = arg instanceof proxy_factory_1.JsonRpcProxyFactory ? arg : new proxy_factory_1.JsonRpcProxyFactory(arg);
56 this.listen({
57 path,
58 onConnection: c => factory.listen(c)
59 });
60 return factory.createProxy();
61 }
62 initializeMultiplexer() {
63 const mainChannel = this.createMainChannel();
64 mainChannel.onMessage(() => this.onIncomingMessageActivityEmitter.fire());
65 this.channelMultiplexer = new channel_1.ChannelMultiplexer(mainChannel);
66 }
67 /**
68 * Install a connection handler for the given path.
69 */
70 listen(handler, options) {
71 this.openChannel(handler.path, channel => {
72 handler.onConnection(channel);
73 }, options);
74 }
75 async openChannel(path, handler, options) {
76 if (!this.channelMultiplexer) {
77 throw new Error('The channel multiplexer has not been initialized yet!');
78 }
79 const newChannel = await this.channelMultiplexer.open(path);
80 newChannel.onClose(() => {
81 const { reconnecting } = Object.assign({ reconnecting: true }, options);
82 if (reconnecting) {
83 this.reconnectChannelOpeners.push(() => this.openChannel(path, handler, options));
84 }
85 });
86 handler(newChannel);
87 }
88};
89AbstractConnectionProvider = __decorate([
90 (0, inversify_1.injectable)()
91], AbstractConnectionProvider);
92exports.AbstractConnectionProvider = AbstractConnectionProvider;
93//# sourceMappingURL=abstract-connection-provider.js.map
\No newline at end of file