Sawtaytoes
5/3/2019 - 10:42 AM

Hello world

Hello world

<script>
  import { createStore } from './fakeRedux.js'
  
  let count = 0
  
  const stateModifiers = {
    DECREMENT: () => {
      count -= 1
    },
    INCREMENT: () => {
      count += 1
    },
  }

  const store = (
    createStore(
      stateModifiers
    )
  )
</script>

<h1>Count: {count}</h1>

<button
  on:click={() => {
    store.dispatch({ type: 'DECREMENT' })
  }}
>
  +
</button>

<button
  on:click={() => {
    store.dispatch({ type: 'INCREMENT' })
  }}
>
  -
</button>
export 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,
  }
}