Commit 9923fad

mo khan <mo@mokhan.ca>
2016-12-06 04:30:43
extract class to wire up the container.
1 parent 9f5a458
Changed files (4)
app/boot/wire-up-components-into.js
@@ -0,0 +1,42 @@
+import EventAggregator from '../infrastructure/event-aggregator';
+import FetchWorkouts from '../queries/fetch-workouts';
+import LoginCommand from '../commands/login-command';
+import Registry from '../infrastructure/registry';
+import Router from '../infrastructure/router'
+
+export default class WireUpComponentsInto {
+  constructor(registry = new Registry()) {
+    this.registry = registry;
+  }
+
+  run() {
+    registry.registry('eventAggregator', (container) => {
+      return new EventAggregator();
+    }).asSingleton();
+
+    registry.register('router', (container) => {
+      return new Router({
+        eventAggregator: container.resolve('eventAggregator')
+      });
+    }).asSingleton();
+    registerCommandsInto(this.registry);
+    registerQueriesInto(this.registry);
+    return this.registry;
+  }
+
+  registerCommandsInto(registry) {
+    registry.registry('command', (container) => {
+      //eventAggregator.subscribe("LOGIN", command);
+      return new LoginCommand(container.resolve('eventAggregator'));
+    }).asSingleton();
+
+    // register each command with the event aggregator
+  }
+
+  registerQueriesInto(registry) {
+    registry.registry('query', (container) => {
+      //eventAggregator.subscribe("FETCH_WORKOUTS", query);
+      return new FetchWorkouts(container.resolve('eventAggregator'));
+    }).asSingleton();
+  }
+}
app/components/application-shell.js
@@ -1,17 +1,13 @@
 import React, { Component } from 'react';
-import { AppRegistry, Text, ListView, View, Image, TextInput, Navigator } from 'react-native';
-import LoginScreen from '../screens/login-screen'
-import Router from '../infrastructure/router'
-import EventAggregator from '../infrastructure/event-aggregator';
-import LoginCommand from '../commands/login-command';
-import FetchWorkouts from '../queries/fetch-workouts';
+import { Navigator } from 'react-native';
+import LoginScreen from '../screens/login-screen';
+import WireUpComponentsInto from '../boot/wire-up-components-into';
 
 export default class ApplicationShell extends Component {
   constructor(props) {
     super(props);
-    const eventAggregator = new EventAggregator();
-    this.router = new Router({eventAggregator});
-    this.registerCommands(eventAggregator);
+    this.registry = new WireUpComponentsInto().run();
+    this.router = this.registry.resolve('router');
   }
 
   render() {
@@ -23,12 +19,4 @@ export default class ApplicationShell extends Component {
       />
     );
   }
-
-  registerCommands(eventAggregator) {
-    let command = new LoginCommand(eventAggregator)
-    eventAggregator.subscribe("LOGIN", command);
-
-    let query = new FetchWorkouts(eventAggregator)
-    eventAggregator.subscribe("FETCH_WORKOUTS", query);
-  }
 }
app/infrastructure/__tests__/container_spec.js → app/infrastructure/__tests__/registry_spec.js
@@ -1,10 +1,10 @@
-import Container from '../container';
+import Registry from '../registry';
 
-describe("Container", () => {
+describe("Registry", () => {
   let subject = null;
 
   beforeEach(() => {
-    subject = new Container();
+    subject = new Registry();
   });
 
   describe("#resolve", () => {
app/infrastructure/container.js → app/infrastructure/registry.js
@@ -20,7 +20,7 @@ class Registration {
   }
 }
 
-export default class Container {
+export default class Registry {
   constructor() {
     this.components = {};
   }