Skip to content

Commit

Permalink
Make it possible to export container dependant variables
Browse files Browse the repository at this point in the history
This patch introduces the initialize function inside a module, this
function is called with the root container of the application at
application startup.

This enables the logger module to export a logger object fetched via
container.get<ILogger>(ILogger) such that this new logger object can be
exported to code that does not use injection.

Signed-off-by: Antoine Tremblay <antoine.tremblay@ericsson.com>
  • Loading branch information
Antoine Tremblay committed Sep 25, 2017
1 parent 61c9513 commit c720ad4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
14 changes: 9 additions & 5 deletions dev-packages/cli/src/generator/backend-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@ const { Container, injectable } = require('inversify');
const { BackendApplication } = require('@theia/core/lib/node');
const { backendApplicationModule } = require('@theia/core/lib/node/backend-application-module');
const { messagingBackendModule } = require('@theia/core/lib/node/messaging/messaging-backend-module');
const { loggerBackendModule } = require('@theia/core/lib/node/logger-backend-module');
const loggerModule = require('@theia/core/lib/node/logger-backend-module');
const container = new Container();
container.load(backendApplicationModule);
container.load(messagingBackendModule);
container.load(loggerBackendModule);
container.load(loggerModule.loggerBackendModule);
loggerModule.initialize(container);
function load(raw) {
return Promise.resolve(raw.default).then(module =>
container.load(module)
)
return Promise.resolve(raw.default).then(module => {
container.load(module);
if (raw.initialize !== undefined) {
raw.initialize(container);
}
})
}
function start(port, host) {
Expand Down
9 changes: 6 additions & 3 deletions dev-packages/cli/src/generator/frontend-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ container.load(messagingFrontendModule);
container.load(loggerFrontendModule);
function load(raw) {
return Promise.resolve(raw.default).then(module =>
container.load(module)
)
return Promise.resolve(raw.default).then(module => {
container.load(module);
if (raw.initialize !== undefined) {
raw.initialize(container);
}
})
}
function start() {
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/node/logger-backend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ import { BunyanLoggerServer } from './bunyan-logger-server';
import { LoggerWatcher } from '../common/logger-watcher';
import * as yargs from 'yargs';

/* This is to be initialized from container composition root.
It can be used outside of the inversify context. */
export let logger: ILogger;

/* This is to be called only from the container composition root. */
export function initialize(container: Container) {
logger = container.get<ILogger>(ILogger);
}

export const loggerBackendModule = new ContainerModule(bind => {
bind(ILogger).to(Logger).inSingletonScope().whenTargetIsDefault();
bind(LoggerWatcher).toSelf().inSingletonScope();
Expand Down

0 comments on commit c720ad4

Please sign in to comment.