UNPKG

@eclipse-glsp/protocol

Version:

The protocol definition for client-server communication in GLSP

96 lines 4.51 kB
"use strict"; /******************************************************************************** * Copyright (c) 2023-2026 EclipseSource and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. * * This Source Code may also be made available under the following Secondary * Licenses when the conditions for such availability set forth in the Eclipse * Public License v. 2.0 are satisfied: GNU General Public License, version 2 * with the GNU Classpath Exception which is available at * https://www.gnu.org/software/classpath/license.html. * * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 ********************************************************************************/ Object.defineProperty(exports, "__esModule", { value: true }); exports.FeatureModule = void 0; const inversify_1 = require("inversify"); const array_util_1 = require("../utils/array-util"); /** * A `FeatureModule` is a specialized {@link ContainerModule} that can declare dependencies to other {@link FeatureModule}. * A feature module will only be loaded into a container if all of its required modules haven been loaded before. T * Each feature module binds its `featureId` be default. This enables querying of existing container to check wether a * feature module has been loaded into this container. */ class FeatureModule extends inversify_1.ContainerModule { constructor(registry, options = {}) { var _a; super((bind, unbind, isBound, ...rest) => { if (this.configure(bind, isBound)) { registry(bind, unbind, isBound, ...rest); this.debugLog(`Loading of feature module with id '${this.featureId.toString()}' completed`); } }); this.featureId = (_a = options.featureId) !== null && _a !== void 0 ? _a : this.createFeatureId(); this.requires = options.requires; } createFeatureId() { return Symbol(this.id); } /** * Configures the feature module i.e. checks if the requirements are met. * If this is the case the {@link FeatureModule.featureId} will be bound and the module will be loaded * @param bind container bind function * @param isBound container isBound function * @returns `true` if all requirements are met and the module is loaded. `false` otherwise */ configure(bind, isBound) { this.debugLog(`Trying to load feature module with id '${this.featureId.toString()}'`); if (this.isLoaded({ isBound })) { const message = `Could not load feature module. Another module with id '${this.featureId.toString()}' is already loaded`; this.debugLog(message); throw new Error(message); } if (this.checkRequirements(isBound)) { this.debugLog(`Requirements are met, continue loading of feature module with id '${this.featureId.toString()}'`); bind(this.featureId).toConstantValue(this.featureId); return true; } return false; } debugLog(message, ...optionalParams) { if (FeatureModule.DEBUG_LOG_ENABLED) { console.log(message, ...optionalParams); } } /** * Checks if all required {@link FeatureModule}s are already loaded/bound in the container. * @param isBound The `isBound` property of the module callback. Used to check the required modules. * @returns `true` if all requirements are met, `false` otherwise */ checkRequirements(isBound) { var _a; const requires = (0, array_util_1.asArray)((_a = this.requires) !== null && _a !== void 0 ? _a : []); if (requires.length === 0) { return true; } const missing = requires.filter(module => !module.isLoaded({ isBound })); if (missing.length > 0) { this.debugLog(`Could not load feature module. Required modules are not loaded. Feature ids: ${missing.map(m => m.featureId.toString()).join(', ')}`); return false; } return true; } isLoaded(context) { return context.isBound(this.featureId); } } exports.FeatureModule = FeatureModule; /** * Global flag to enable/disable additional debug log output when loading feature modules * Default is `false`. */ FeatureModule.DEBUG_LOG_ENABLED = false; //# sourceMappingURL=feature-module.js.map