Commit 667f6c9

mo khan <mo@mokhan.ca>
2016-12-22 18:30:19
auto resolve command dependencies.
1 parent b35b6fd
app/boot/wire-up-components-into.js
@@ -1,8 +1,10 @@
+import * as commands from '../services/commands';
+import * as queries from '../services/queries';
+import Api from '../infrastructure/api';
+import ApplicationStorage from '../infrastructure/application-storage';
 import EventAggregator from '../infrastructure/event-aggregator';
 import Registry from '../infrastructure/registry';
 import Router from '../infrastructure/router'
-import * as commands from '../services/commands';
-import * as queries from '../services/queries';
 
 export default class WireUpComponentsInto {
   constructor(registry = new Registry()) {
@@ -16,6 +18,10 @@ export default class WireUpComponentsInto {
         eventAggregator: container.resolve('eventAggregator')
       });
     }).asSingleton();
+    this.registry.register('applicationStorage', ApplicationStorage).asSingleton();
+    this.registry.register('sessionsApi', () => {
+      return new Api('/sessions');
+    }).asSingleton();
     this.registerCommandsInto(this.registry);
     this.registerQueriesInto(this.registry);
     return this.registry;
@@ -24,9 +30,7 @@ export default class WireUpComponentsInto {
   registerCommandsInto(registry) {
     for (let command in commands) {
       console.log(`registering: ${command}`);
-      registry.register('command', (container) => {
-        return new commands[command](container.resolve('eventAggregator'));
-      }).asSingleton();
+      registry.register('command', commands[command]).asSingleton();
     }
     registry.resolveAll("command").forEach((command) => {
       command.subscribeTo(registry.resolve('eventAggregator'));
app/services/commands/login-command.js
@@ -1,12 +1,10 @@
-import Api from '../../infrastructure/api';
-import ApplicationStorage from '../../infrastructure/application-storage';
 import * as events from '../events';
 
 export default class LoginCommand {
-  constructor(eventAggregator, api = new Api('/sessions'), storage = new ApplicationStorage()) {
+  constructor(eventAggregator, sessionsApi, applicationStorage) {
     this.eventAggregator = eventAggregator;
-    this.api = api;
-    this.storage = storage;
+    this.api = sessionsApi;
+    this.applicationStorage = applicationStorage;
   }
 
   subscribeTo(eventAggregator) {
@@ -19,7 +17,7 @@ export default class LoginCommand {
   }
 
   onResponse(json) {
-    this.storage.save('authentication_token', json.authentication_token);
+    this.applicationStorage.save('authentication_token', json.authentication_token);
     this.eventAggregator.publish({
       event: events.LOGGED_IN,
       ...json
app/services/commands/logout-command.js
@@ -1,13 +1,13 @@
-import ApplicationStorage from '../../infrastructure/application-storage';
+import * as events from '../events';
 
 export default class LogoutCommand {
-  constructor(eventAggregator, applicationStorage = new ApplicationStorage()) {
+  constructor(eventAggregator, applicationStorage) {
     this.eventAggregator = eventAggregator;
     this.applicationStorage = applicationStorage;
   }
 
   subscribeTo(eventAggregator) {
-    eventAggregator.subscribe('LOGOUT', this);
+    eventAggregator.subscribe(events.LOGOUT, this);
   }
 
   notify(event) {