import { EventEmitter } from 'events';

import { Logger } from './util';
import { ServiceConstructor, ServiceContainer } from './service';

export class Kaptan extends EventEmitter {
  public readonly logger: Logger;
  public readonly services: ServiceContainer;

  constructor(label: string = 'kaptan') {
    super();

    this.logger = new Logger(label);
    this.services = new ServiceContainer(this);
  }

  public use(service: ServiceConstructor, options: {[key:string]: any} = {}) {
    if (!service.Options) {
      service.Options = {};
    }

    service.Options = { ...service.Options, ...options };
    this.services.add(service);
  }

  public start() {
    this.services.spawn();
  }
}

import { EventEmitter } from 'events';

import { Logger } from './util';
import { Service, ServiceConstructor, ServiceContainer } from './service';

export class Kaptan extends EventEmitter {
  public readonly logger: Logger;
  public readonly services: ServiceContainer;

  constructor(label: string = 'kaptan') {
    super();

    this.logger = new Logger(label);
    this.services = new ServiceContainer(this);
  }

  public use(service: ServiceConstructor, options: {[key:string]: any} = {}) {
    service = Service.copy(Service.getServiceName(service), service);
    
    if (!service.Options) {
      service.Options = {};
    }

    service.Options = { ...service.Options, ...options };
    this.services.add(service);
  }

  public start() {
    this.services.spawn();
  }
}

Add duplicate service to the services instead of the original one
Add duplicate service to the services instead of the original one

