@eclipse-glsp/protocol
Version:
The protocol definition for client-server communication in GLSP
78 lines • 2.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.bindAsService = exports.lazyBind = exports.bindOrRebind = void 0;
/**
* Checks wether the given service identifier is already bound in the given context
* then either calls the `bind` or `rebind` function respectively.
*
* As this is just a convenience function
* ```ts
* bindOrRebind({bind,isBound,rebind}, MyService).to(SomeOtherService);
* ```
* is equivalent to:
* ```
* if (isBound(MyService)) {
* rebind(MyService).to(SomeOtherService);
* } else {
* bind(MyService).to(SomeOtherService);
* }
* ```
* @param context The binding context
* @param serviceIdentifier The service identifier
* @returns The result of the `bind` or `rebind` function
*/
function bindOrRebind(context, serviceIdentifier) {
if (context.isBound(serviceIdentifier)) {
return context.rebind(serviceIdentifier);
}
return context.bind(serviceIdentifier);
}
exports.bindOrRebind = bindOrRebind;
/**
* Only binds the given service identifier if it's not already bound in the given context.
*
* As this is just a convenience function
* ```ts
* lazyBind({bind,isBound}, MyService)?.to(SomeOtherService);
* ```
* is equivalent to:
* ```
* if (!isBound(MyService)) {
* bind(MyService).to(SomeOtherService);
* }
* ```
* @param context The binding context
* @param serviceIdentifier The service identifier
* @returns The result of the `bind` function or `undefined` if the service was already bound
*/
function lazyBind(context, serviceIdentifier) {
if (context.isBound(serviceIdentifier)) {
return undefined;
}
return context.bind(serviceIdentifier);
}
exports.lazyBind = lazyBind;
/**
* Binds the given service identifier to the given target service in the given context.
* In addition, the target service is bound to itself in singleton scope. This ensures
* that services can be rebound individually even if they are multi-injected.
*
* As this is just a convenience function
* ```ts
* bindAsService(bind,SomeOtherService,MyServiceImpl);
* ```
* is equivalent to:
* ```ts
* bind(MyServiceImpl).toSelf.inSingletonScope():
* bind(SomeOtherService).toService(MyServiceImpl);
* ```
* @param serviceIdentifier
* @param toServiceIdentifier
*/
function bindAsService(context, serviceIdentifier, targetService) {
const bind = typeof context === 'object' ? context.bind.bind(context) : context;
bind(targetService).toSelf().inSingletonScope();
bind(serviceIdentifier).toService(targetService);
}
exports.bindAsService = bindAsService;
//# sourceMappingURL=inversify-util.js.map