Archived
1
0
Fork 0

Change middleware interface

This commit is contained in:
Braiden Vasco 2017-08-12 01:58:10 +00:00
parent 7aa66ceb2e
commit c878e5578b
2 changed files with 9 additions and 26 deletions

View file

@ -21,7 +21,7 @@ class Main
private private
def store def store
@store ||= Obredux::Store.new Reducer, [Obredux::Thunk] @store ||= Obredux::Store.new Reducer, [Obredux::Thunk.new]
end end
def state def state

View file

@ -8,34 +8,19 @@ module Obredux
class Init < Action; end class Init < Action; end
class Middleware class Middleware
def initialize(store) def call(_store, _action)
self.store = store
end
def call(_action)
raise NotImplementedError, "#{self.class}#call" raise NotImplementedError, "#{self.class}#call"
end end
private
def store=(value)
raise TypeError, "expected #store to be a #{Store}" unless value.is_a? Store
@store = value
end
end end
class Thunk < Middleware class Thunk < Middleware
def call(action) def call(store, action)
return action unless action.is_a? Action return action unless action.is_a? Action
action.call dispatch_method action.call store.public_method :dispatch
end
def dispatch_method
store.public_method :dispatch
end end
class Action < Action class Action < Action
def call(dispatch) def call(_dispatch)
raise NotImplementedError, "#{self.class}#call" raise NotImplementedError, "#{self.class}#call"
end end
end end
@ -56,7 +41,7 @@ module Obredux
raise TypeError, "expected action to be an #{Action}" unless action.is_a? Action raise TypeError, "expected action to be an #{Action}" unless action.is_a? Action
action = middleware.inject(action) do |old_action, fn| action = middleware.inject(action) do |old_action, fn|
new_action = fn.call old_action new_action = fn.call self, old_action
break if new_action.nil? break if new_action.nil?
new_action new_action
end end
@ -81,11 +66,9 @@ module Obredux
end end
def middleware=(value) def middleware=(value)
@middleware = value.map do |middleware_klass| @middleware = value.map do |item|
unless middleware_klass.is_a?(Class) && middleware_klass < Middleware raise TypeError, "expected #middleware to be an array of #{Middleware} classes" unless item.is_a? Middleware
raise TypeError, "expected #middleware to be an array of #{Middleware} classes" item
end
middleware_klass.new self
end.freeze end.freeze
end end
end end