Class: Redux::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/redux/store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_state = nil, &reducer) ⇒ Store

Returns a new instance of Store.



5
6
7
8
9
10
# File 'lib/redux/store.rb', line 5

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

Instance Attribute Details

#stateObject (readonly)

Returns the value of attribute state.



3
4
5
# File 'lib/redux/store.rb', line 3

def state
  @state
end

Instance Method Details

#dispatch(action) ⇒ Object



12
13
14
15
# File 'lib/redux/store.rb', line 12

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

#subscribe(&listener) ⇒ Object



17
18
19
20
# File 'lib/redux/store.rb', line 17

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