summaryrefslogtreecommitdiff
path: root/app/boot/wire-up-components-into.js
blob: b4cbc6657df8691f206b9ca096a01f2bebc0b1c8 (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
import * as commands from '../services/commands';
import * as queries from '../services/queries';
import Api from '../infrastructure/api';
import ApplicationStorage from '../infrastructure/application-storage';
import Config from 'react-native-config';
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')
      });
    }).asSingleton();
    this.registry.register('applicationStorage', ApplicationStorage).asSingleton();
    let host = Config.API_HOST;
    this.registry.register('sessionsApi', (container) => new Api(host, '/sessions', container.resolve('applicationStorage'))).asSingleton();
    this.registry.register('workoutsApi', (container) => new Api(host, '/workouts', container.resolve('applicationStorage'))).asSingleton();
    this.registerSubscribers(commands);
    this.registerSubscribers(queries);
    return this.registry;
  }

  registerSubscribers(subscribers) {
    for (let subscriber in subscribers) {
      //console.log(`registering: ${subscriber}`);
      this.registry.register('subscriber', subscribers[subscriber]).asSingleton();
    }
    this.registry.resolveAll("subscriber").forEach((subscriber) => {
      subscriber.subscribeTo(this.registry.resolve('eventAggregator'));
    });
  }
}