Sawtaytoes
4/25/2019 - 3:57 AM

Stateless Redux

var createStore = function(stateModifiers) {
  var localState = {
    subscribers: [],
  }

  var subscribe = function(subscribers) {
    localState
    .subscribers = (
      localState
      .subscribers
      .concat(subscribers)
    )
    
    return {
      unsubscribe: function() {
        subscribers
        .forEach(function(subscriber) {
          const index = (
            localState
            .subscribers
            .indexOf(subscriber)
          )
          
          localState
          .subscribers
          .splice(index, 1)
        })
      }
    }
  }
  
  var createDispatch = function(stateModifiers) {
    return function(action) {
      if (!action.type) {
        console
        .error(
            "No type for action",
            action,
        )
      }

      if (window.__isDebugging) {
        console
        .info(
          action.type,
          action,
        )
      }

      stateModifiers[action.type]
      && stateModifiers[action.type](action)

      localState
      .subscribers
      .forEach(function(subscriber) {
        subscriber(action)
      })

      return action
    }
  }
  
  return {
    dispatch: createDispatch(stateModifiers),
    subscribe: subscribe,
  }
}