// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. import { EventEmitter } from 'events'; import { Container, decorate, injectable, interfaces } from 'inversify'; import { traceWarning } from '../common/logger'; import { Abstract, IServiceContainer, Newable } from './types'; // This needs to be done once, hence placed in a common location. // Used by UnitTestSockerServer and also the extension unit tests. // Place within try..catch, as this can only be done once (it's // possible another extension would perform this before our extension). try { decorate(injectable(), EventEmitter); } catch (ex) { traceWarning('Failed to decorate EventEmitter for DI (possibly already decorated by another Extension)', ex); } @injectable() export class ServiceContainer implements IServiceContainer { constructor(private container: Container) {} public get(serviceIdentifier: interfaces.ServiceIdentifier, name?: string | number | symbol): T { return name ? this.container.getNamed(serviceIdentifier, name) : this.container.get(serviceIdentifier); } public getAll( serviceIdentifier: string | symbol | Newable | Abstract, name?: string | number | symbol | undefined ): T[] { return name ? this.container.getAllNamed(serviceIdentifier, name) : this.container.getAll(serviceIdentifier); } }