Module: Redux::Reducers

Defined in:
lib/redux/reducers.rb

Class Method Summary collapse

Class Method Details

.add_application_reducers_to_storeObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/redux/reducers.rb', line 3

def self.add_application_reducers_to_store
  unless @_application_reducers_added
    @_application_reducers_added = true
    app_reducer = Redux.create_reducer do |prev_state, action|
      case action[:type]
      when 'APPLICATION_STATE'
        if action.key?(:set_state)
          action[:set_state]
        else
          new_state = {}.merge!(prev_state) # make a copy of state
          if action.key?(:collected)
            action[:collected].each do |act|
              new_state.merge!(act[:name] => act[:value])
            end
          else
            new_state.merge!(action[:name] => action[:value])
          end
          new_state
        end
      else
        prev_state.nil? ? {} : prev_state
      end
    end

    class_reducer = Redux.create_reducer do |prev_state, action|
      case action[:type]
      when 'CLASS_STATE'
        if action.key?(:set_state)
          action[:set_state]
        else
          new_state = {}.merge!(prev_state) # make a copy of state
          if action.key?(:collected)
            action[:collected].each do |act|
              new_state[act[:class]] = {} unless new_state.key?(act[:class])
              new_state[act[:class]].merge!(act[:name] => act[:value])
            end
          else
            new_state[action[:class]] = {} unless new_state.key?(action[:class])
            new_state[action[:class]].merge!(action[:name] => action[:value])
          end
          new_state
        end
      else
        prev_state.nil? ? {} : prev_state
      end
    end
    Redux::Store.preloaded_state_merge!(application_state: {}, class_state: {})
    Redux::Store.add_reducers(application_state: app_reducer, class_state: class_reducer)
  end
end