var each = require('savoy').each;
var promise = require('./promise');
var assign = require('./assign');

var resolved = function(resolve) {
  resolve();
};

module.exports = function(initialState) {
  var state = assign(initialState || {});
  var listeners = [];
  var notifyListeners = function() {
    each(listeners, function(listener) {
      listener(state);
    });
  };
  return {
    getState: function() {
      return state;
    },
    addListener: function(listener) {
      if (listeners.indexOf(listener) !== -1) {
        return false;
      }
      listeners.push(listener);
      return true;
    },
    removeListener: function(listener) {
      var index = listeners.indexOf(listener);
      if (index === -1) {
        return false;
      }
      listeners.splice(index, 1, listener);
      return true;
    },
    dispatch: function dispatch(action) {
      var key = action.key;
      var result = action(state[key] || {}, dispatch);
      var setState = function(result) {
        state[key] = result;
        notifyListeners();
      };
      if (typeof result.then === 'function') {
        return result.then(setState);
      }
      setState(result);
      return promise(resolved);
    }
  };
};

@@ -1,38 +1,4 @@
-var each = require('savoy').each;%0A
 var 
@@ -120,49 +120,98 @@
 %7D;%0A%0A
-module.exports = function(initialState) %7B
+var noop = function() %7B%7D;%0A%0Amodule.exports = function(options) %7B%0A  options = options %7C%7C %7B%7D;
 %0A  v
@@ -228,16 +228,24 @@
  assign(
+options.
 initialS
@@ -275,573 +275,92 @@
 ener
-s
  = 
-%5B%5D;%0A  var notifyListeners = function() %7B%0A    each(listeners, function(listener) %7B%0A      listener(state);%0A    %7D);%0A  %7D;%0A  return %7B%0A    getState: function() %7B%0A      return state;%0A    %7D,%0A    addListener: function(listener) %7B%0A      if (listeners.indexOf(listener) !== -1) %7B%0A        return false;%0A      %7D%0A      listeners.push(listener);%0A      return true;%0A    %7D,%0A    removeListener: function(listener) %7B%0A      var index = listeners.indexOf(listener);%0A      if (index === -1) %7B%0A        return false;%0A      %7D%0A      listeners.splice(index, 1, listener);%0A      return tru
+options.listener %7C%7C noop;%0A  return %7B%0A    getState: function() %7B%0A      return stat
 e;%0A 
@@ -571,24 +571,22 @@
     
-notifyL
+l
 istener
-s
 (
+state
 );%0A 

Refactor `store`