
class ProcessManager {
  constructor(options) {
    this.logger = options.logger;
    this.events = options.events;
    this.plugins = options.plugins;
    this.processes = {};

    this._registerAsPlugin();
    this._registerEvents();
  }

  _registerAsPlugin() {
    const self = this;
    self.plugin = this.plugins.createPlugin('processManager', {});
    self.plugin.registerAPICall(
      'get',
      '/embark-api/processes',
      (req, res) => {
        const formatter = (acc, processName) => {
          acc.push({state: self.processes[processName].state, name: processName});
          return acc;
        };
        res.send(Object.keys(self.processes).reduce(formatter, []));
      }
    );
  }

  _registerEvents() {
    const self = this;
    self.events.setCommandHandler('processes:register', (name, cb) => {
      this.processes[name] = {
        state: 'unstarted',
        cb: cb
      };
    });

    self.events.setCommandHandler('processes:launch', (name, cb) => {
      let process = self.processes[name];
      if (process.state !== 'unstarted') {
        return cb();
      }
      process.state = 'starting';
      process.cb.apply(process.cb, [
        () => {
          process.state = 'running';
          cb();
        }
      ]);
    });
  }
}

module.exports = ProcessManager;

@@ -1,8 +1,130 @@
+%0Aconst ProcessState = %7B%0A  Unstarted: 'unstarted',%0A  Starting: 'starting',%0A  Running: 'running',%0A  Stopping: 'stopping',%0A%7D%0A
 %0Aclass P
@@ -946,64 +946,231 @@
 %3E %7B%0A
-      this.processes%5Bname%5D = %7B%0A        state: 'u
+%0A      let launchFn, stopFn;%0A%0A      if (typeof cb === 'object') %7B%0A        launchFn = cb.launchFn;%0A        stopFn = cb.stopFn;%0A      %7D%0A%0A      this.processes%5Bname%5D = %7B%0A        name: name,%0A        state: ProcessState.U
 nstarted
 ',%0A 
@@ -1161,25 +1161,24 @@
 te.Unstarted
-'
 ,%0A        cb
@@ -1179,18 +1179,77 @@
     cb: 
-cb
+launchFn %7C%7C cb,%0A        stopFn: stopFn %7C%7C function noop () %7B%7D
 %0A      %7D
@@ -1325,24 +1325,61 @@
 e, cb) =%3E %7B%0A
+      cb = cb %7C%7C function noop() %7B%7D;%0A
       let pr
@@ -1436,26 +1436,38 @@
 ate !== 
-'u
+ProcessState.U
 nstarted
 ') %7B%0A   
@@ -1458,17 +1458,16 @@
 nstarted
-'
 ) %7B%0A    
@@ -1513,26 +1513,37 @@
 state = 
-'s
+ProcessState.S
 tarting
-'
 ;%0A      
@@ -1582,16 +1582,23 @@
        (
+...args
 ) =%3E %7B%0A 
@@ -1626,32 +1626,507 @@
 e = 
-'running';%0A          cb(
+ProcessState.Running;%0A          cb.apply(cb, args);%0A        %7D%0A      %5D);%0A    %7D);%0A%0A    self.events.setCommandHandler('processes:stop', (name, cb) =%3E %7B%0A      let process = self.processes%5Bname%5D;%0A      cb = cb %7C%7C function noop() %7B%7D;%0A      if (process.state !== ProcessState.Running) %7B%0A        return cb();%0A      %7D%0A      process.state = ProcessState.Stopping;%0A      process.stopFn.apply(process.stopFn, %5B%0A        (...args) =%3E %7B%0A          process.state = ProcessState.Unstarted;%0A          cb.apply(cb, args
 );%0A 

introduce `processes:stop` handlers