Class: GrandCentral::Store

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

Direct Known Subclasses

VersionedStore

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#stateObject (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