Class: Redux::Store
- Inherits:
-
Object
- Object
- Redux::Store
- Defined in:
- lib/redux/store.rb
Instance Attribute Summary collapse
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
- #dispatch(action) ⇒ Object
-
#initialize(initial_state = nil, &reducer) ⇒ Store
constructor
A new instance of Store.
- #subscribe(&listener) ⇒ Object
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
#state ⇒ Object (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 |