Archived
1
0
Fork 0

Add reducer combinations

This commit is contained in:
Braiden Vasco 2017-07-28 03:51:44 +00:00
parent 6989160300
commit 29f4296596
2 changed files with 29 additions and 6 deletions

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true
module Obredux
UNDEFINED = Object.new.freeze
UNDEFINED = {}.freeze
class Action; end
@ -22,14 +22,39 @@ module Obredux
end
class Reducer
def self.combine(options = nil)
@combine ||= {}
return @combine if options.nil?
options.each do |key, reducer_klass|
@combine[key] = reducer_klass
end
end
def initialize(state, action)
@state = state
@action = action
end
def call
@state = initial_state if state.equal? UNDEFINED
init = state.equal? UNDEFINED
@state ||= {}.freeze
unless self.class.combine.empty?
@state = state.merge(
self.class.combine.map do |key, reducer_klass|
[
key,
reducer_klass.new(state[key], action).call,
]
end.to_h.freeze,
).freeze
end
@state = state.merge initial_state if init
reduce
end

View file

@ -3,6 +3,8 @@
require 'reducers/data'
class Reducer < Obredux::Reducer
combine data: Reducers::Data
class << self
attr_reader :screen_width, :screen_height
@ -93,10 +95,6 @@ private
end
def reduce
@state = state.merge(
data: Reducers::Data.new(state[:data], action).call,
)
case action
when Actions::FriendMessage
friend_message