summaryrefslogtreecommitdiff
path: root/app/boot/wire-up-components-into.js
blob: 337d2c71604edae27411ceaf666cdde825d3f2f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import * as commands from '../services/commands';
import * as queries from '../services/queries';
import Api from '../infrastructure/api';
import ApplicationStorage from '../infrastructure/application-storage';
import Configuration from '../infrastructure/configuration';
import EventAggregator from '../infrastructure/event-aggregator';
import Registry from '../infrastructure/registry';
import Router from '../infrastructure/router'

export default class WireUpComponentsInto {
  constructor(registry = new Registry()) {
    this.registry = registry;
  }

  run() {
    this.registry.register('eventAggregator', EventAggregator).asSingleton();
    this.registry.register('router', (container) => {
      return new Router({
        eventAggregator: container.resolve('eventAggregator'),
        registry: this.registry
      });
    }).asSingleton();
    this.registry.register('applicationStorage', ApplicationStorage).asSingleton();
    const overrides = { 
      API_HOST: 'http://localhost:3000',
      ENV: 'development',
      LOCAL_ONLY: 'true',
    };
    this.registry.register('configuration', (container) => new Configuration(overrides)).asSingleton();
    this.registry.register('api', Api).asSingleton();

    this.registerSubscribers(commands);
    this.registerSubscribers(queries);

    this.registry.resolveAll("subscriber").forEach((subscriber) => {
      subscriber.subscribeTo(this.registry.resolve('eventAggregator'));
    });
    return this.registry;
  }

  registerSubscribers(subscribers) {
    for (let subscriber in subscribers) {
      this.registry.register('subscriber', subscribers[subscriber]).asSingleton();
    }
  }
}