Sha256: 8b7d39625c10be575f869df66a1225e0b6e5cbc99aa7667976d604a64411f21e

Contents?: true

Size: 1.96 KB

Versions: 3

Compression:

Stored size: 1.96 KB

Contents

module Redux
  module Reducers
    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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
isomorfeus-redux-4.2.0 lib/redux/reducers.rb
isomorfeus-redux-4.1.18 lib/redux/reducers.rb
isomorfeus-redux-4.1.17 lib/redux/reducers.rb