Class: GrandCentral::Store
- Inherits:
-
Object
- Object
- GrandCentral::Store
- Defined in:
- lib/grand_central/store.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
- #dispatch(action) ⇒ Object
-
#initialize(initial_state, &reducer) ⇒ Store
constructor
A new instance of Store.
- #on_dispatch(&block) ⇒ Object
- #run_callbacks(old_state, new_state, action = nil) ⇒ Object
Constructor Details
#initialize(initial_state, &reducer) ⇒ Store
Returns a new instance of Store.
5 6 7 8 9 |
# File 'lib/grand_central/store.rb', line 5 def initialize initial_state, &reducer @state = initial_state @reducer = reducer @dispatch_callbacks = [] end |
Instance Attribute Details
#state ⇒ Object (readonly)
Returns the value of attribute state.
3 4 5 |
# File 'lib/grand_central/store.rb', line 3 def state @state end |
Instance Method Details
#dispatch(action) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/grand_central/store.rb', line 11 def dispatch action old_state = state @state = @reducer.call state, action run_callbacks old_state, state, action action end |
#on_dispatch(&block) ⇒ Object
18 19 20 21 |
# File 'lib/grand_central/store.rb', line 18 def on_dispatch &block @dispatch_callbacks << block self end |
#run_callbacks(old_state, new_state, action = nil) ⇒ Object
23 24 25 26 27 |
# File 'lib/grand_central/store.rb', line 23 def run_callbacks old_state, new_state, action=nil @dispatch_callbacks.each do |callback| callback.call old_state, new_state, action end end |