1 | ;
|
2 | // *****************************************************************************
|
3 | // Copyright (C) 2018 TypeFox 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 | // *****************************************************************************
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.ConnectionContainerModule = void 0;
|
19 | /* eslint-disable @typescript-eslint/no-explicit-any */
|
20 | const inversify_1 = require("inversify");
|
21 | const common_1 = require("../../common");
|
22 | /**
|
23 | * ### Connection Container Module
|
24 | *
|
25 | * It provides bindings which are scoped per a connection, e.g.
|
26 | * in order to allow backend services to access frontend service within the same connection.
|
27 | *
|
28 | * #### Binding a frontend service
|
29 | * ```ts
|
30 | * const myConnectionModule = ConnectionContainerModule.create(({ bindFrontendService }) => {
|
31 | * bindFrontendService(myFrontendServicePath, MyFrontendService);
|
32 | * });
|
33 | *
|
34 | * export const myBackendApplicationModule = new ContainerModule(bind => {
|
35 | * bind(ConnectionContainerModule).toConstantValue(myConnectionModule);
|
36 | * }
|
37 | * ```
|
38 | *
|
39 | * #### Exposing a backend service
|
40 | * ```ts
|
41 | * const myConnectionModule2 = ConnectionContainerModule.create(({ bind, bindBackendService }) => {
|
42 | * bind(MyBackendService).toSelf().inSingletonScope();
|
43 | * bindBackendService(myBackendServicePath, MyBackendService);
|
44 | * });
|
45 | *
|
46 | * export const myBackendApplicationModule2 = new ContainerModule(bind => {
|
47 | * bind(ConnectionContainerModule).toConstantValue(myConnectionModule2);
|
48 | * }
|
49 | * ```
|
50 | *
|
51 | * #### Injecting a frontend service
|
52 | * ```ts
|
53 | * @injectable()
|
54 | * export class MyBackendService {
|
55 | * @inject(MyFrontendService)
|
56 | * protected readonly myFrontendService: MyFrontendService;
|
57 | * }
|
58 | * ```
|
59 | */
|
60 | exports.ConnectionContainerModule = Object.assign(Symbol('ConnectionContainerModule'), {
|
61 | create(callback) {
|
62 | return new inversify_1.ContainerModule((bind, unbind, isBound, rebind) => {
|
63 | const bindFrontendService = (path, serviceIdentifier) => {
|
64 | const serviceFactory = new common_1.JsonRpcProxyFactory();
|
65 | const service = serviceFactory.createProxy();
|
66 | bind(common_1.ConnectionHandler).toConstantValue({
|
67 | path,
|
68 | onConnection: connection => serviceFactory.listen(connection)
|
69 | });
|
70 | return bind(serviceIdentifier).toConstantValue(service);
|
71 | };
|
72 | const bindBackendService = (path, serviceIdentifier, onActivation) => {
|
73 | bind(common_1.ConnectionHandler).toDynamicValue(context => new common_1.JsonRpcConnectionHandler(path, proxy => {
|
74 | const service = context.container.get(serviceIdentifier);
|
75 | return onActivation ? onActivation(service, proxy) : service;
|
76 | })).inSingletonScope();
|
77 | };
|
78 | callback({ bind, unbind, isBound, rebind, bindFrontendService, bindBackendService });
|
79 | });
|
80 | }
|
81 | });
|
82 | //# sourceMappingURL=connection-container-module.js.map |
\ | No newline at end of file |