@eclipse-glsp/protocol
Version:
The protocol definition for client-server communication in GLSP
60 lines • 2.75 kB
JavaScript
;
/********************************************************************************
* Copyright (c) 2024 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.bindLazyInjector = exports.DefaultLazyInjector = exports.LazyInjector = void 0;
const inversify_1 = require("inversify");
exports.LazyInjector = Symbol('LazyInjector');
/**
* Default implementation of the {@link LazyInjector} interface. This implementation
* will be bound when using the {@link bindLazyInjector} function.
*/
class DefaultLazyInjector {
constructor(container) {
this.container = container;
this.cache = new Map();
}
get(serviceIdentifier) {
const service = this.getOptional(serviceIdentifier);
if (service === undefined) {
throw new Error('No matching bindings found for serviceIdentifier:' + (0, inversify_1.getServiceIdentifierAsString)(serviceIdentifier));
}
return service;
}
getOptional(serviceIdentifier) {
if (this.cache.has(serviceIdentifier)) {
return this.cache.get(serviceIdentifier);
}
const service = this.container.isBound(serviceIdentifier) ? this.container.get(serviceIdentifier) : undefined;
this.cache.set(serviceIdentifier, service);
return service;
}
getAll(serviceIdentifier) {
if (this.cache.has(serviceIdentifier)) {
return this.cache.get(serviceIdentifier);
}
const services = this.container.isBound(serviceIdentifier) ? this.container.getAll(serviceIdentifier) : [];
this.cache.set(serviceIdentifier, services);
return services;
}
}
exports.DefaultLazyInjector = DefaultLazyInjector;
function bindLazyInjector(context) {
const bind = typeof context === 'object' ? context.bind.bind(context) : context;
bind(exports.LazyInjector).toDynamicValue(ctx => new DefaultLazyInjector(ctx.container));
}
exports.bindLazyInjector = bindLazyInjector;
//# sourceMappingURL=lazy-injector.js.map