Sha256: 68e3ffb1fef6734b2bc7ec68039e5cab63b7c326cb4fdb21bc8a0f96b5654bac

Contents?: true

Size: 470 Bytes

Versions: 3

Compression:

Stored size: 470 Bytes

Contents

module Redux
  class Store
    attr_reader :state

    def initialize(initial_state = nil, &reducer)
      @state     = initial_state
      @reducer   = reducer || ->(*){}
      @listeners = []
      dispatch({})
    end

    def dispatch(action)
      @state = @reducer.call(@state, action)
      @listeners.each{ |listener| listener.call() }
    end

    def subscribe(&listener)
      @listeners << listener
      ->{ @listeners.delete(listener) }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
redux-0.1.2 lib/redux/store.rb
redux-0.1.1 lib/redux/store.rb
redux-0.1.0 lib/redux/store.rb