UNPKG

4.14 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2017 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-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.bindContribution = exports.bindContributionProvider = exports.Bindable = exports.ContributionProvider = void 0;
19const contribution_filter_1 = require("./contribution-filter");
20exports.ContributionProvider = Symbol('ContributionProvider');
21class ContainerBasedContributionProvider {
22 constructor(serviceIdentifier, container) {
23 this.serviceIdentifier = serviceIdentifier;
24 this.container = container;
25 }
26 getContributions(recursive) {
27 if (this.services === undefined) {
28 const currentServices = [];
29 let filterRegistry;
30 let currentContainer = this.container;
31 // eslint-disable-next-line no-null/no-null
32 while (currentContainer !== null) {
33 if (currentContainer.isBound(this.serviceIdentifier)) {
34 try {
35 currentServices.push(...currentContainer.getAll(this.serviceIdentifier));
36 }
37 catch (error) {
38 console.error(error);
39 }
40 }
41 if (filterRegistry === undefined && currentContainer.isBound(contribution_filter_1.ContributionFilterRegistry)) {
42 filterRegistry = currentContainer.get(contribution_filter_1.ContributionFilterRegistry);
43 }
44 // eslint-disable-next-line no-null/no-null
45 currentContainer = recursive === true ? currentContainer.parent : null;
46 }
47 this.services = filterRegistry ? filterRegistry.applyFilters(currentServices, this.serviceIdentifier) : currentServices;
48 }
49 return this.services;
50 }
51}
52var Bindable;
53(function (Bindable) {
54 function isContainer(arg) {
55 return typeof arg !== 'function'
56 // https://github.com/eclipse-theia/theia/issues/3204#issue-371029654
57 // In InversifyJS `4.14.0` containers no longer have a property `guid`.
58 && ('guid' in arg || 'parent' in arg);
59 }
60 Bindable.isContainer = isContainer;
61})(Bindable = exports.Bindable || (exports.Bindable = {}));
62function bindContributionProvider(bindable, id) {
63 const bindingToSyntax = (Bindable.isContainer(bindable) ? bindable.bind(exports.ContributionProvider) : bindable(exports.ContributionProvider));
64 bindingToSyntax
65 .toDynamicValue(ctx => new ContainerBasedContributionProvider(id, ctx.container))
66 .inSingletonScope().whenTargetNamed(id);
67}
68exports.bindContributionProvider = bindContributionProvider;
69/**
70 * Helper function to bind a service to a list of contributions easily.
71 * @param bindable a Container or the bind function directly.
72 * @param service an already bound service to refer the contributions to.
73 * @param contributions array of contribution identifiers to bind the service to.
74 */
75// eslint-disable-next-line @typescript-eslint/no-explicit-any
76function bindContribution(bindable, service, contributions) {
77 const bind = Bindable.isContainer(bindable) ? bindable.bind.bind(bindable) : bindable;
78 for (const contribution of contributions) {
79 bind(contribution).toService(service);
80 }
81}
82exports.bindContribution = bindContribution;
83//# sourceMappingURL=contribution-provider.js.map
\No newline at end of file