Add basic reducer class Obredux::Reducer
This commit is contained in:
parent
f1786e5877
commit
d60ed0f02b
2 changed files with 23 additions and 10 deletions
|
@ -20,4 +20,24 @@ module Obredux
|
|||
@state = reducer_klass.new(state, action).call
|
||||
end
|
||||
end
|
||||
|
||||
class Reducer
|
||||
def initialize(state, action)
|
||||
@state = state
|
||||
@action = action
|
||||
end
|
||||
|
||||
def call
|
||||
@state = initial_state if state.equal? UNDEFINED
|
||||
reduce
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :state, :action
|
||||
|
||||
def initial_state
|
||||
raise NotImplementedError, "#{self.class}#initial_state"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,12 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Reducer
|
||||
attr_reader :state, :action
|
||||
|
||||
def initialize(state, action)
|
||||
@state = state
|
||||
@action = action
|
||||
end
|
||||
class Reducer < Obredux::Reducer
|
||||
private
|
||||
|
||||
def initial_state
|
||||
{
|
||||
|
@ -83,9 +78,7 @@ class Reducer
|
|||
}.freeze
|
||||
end
|
||||
|
||||
def call
|
||||
@state = initial_state if state == Obredux::UNDEFINED
|
||||
|
||||
def reduce
|
||||
case action
|
||||
when Actions::LoadFriends
|
||||
load_friends
|
||||
|
|
Reference in a new issue