Replies: 1 comment 2 replies
-
Example MiddlewareProtocol: class MyMiddleware: MiddlewareProtocol {
typealias InputActionType = String
typealias OutputActionType = String
typealias StateType = String
func handle(action: InputActionType, from dispatcher: ActionSource, state: @escaping GetState<StateType>) -> IO<OutputActionType> {
let stateBeforeReducer = state()
let dispatcherString = "\(dispatcher.file):\(dispatcher.line) (\(dispatcher.function))"
return IO { output in
let stateAfterReducer = state()
print("Action: \(action) from \(dispatcherString) changed the state from \(stateBeforeReducer) to \(stateAfterReducer)")
output.dispatch("Dispatching this. If this is Main Queue, it will run sync, so you can wrap this in withAnimation block if you want")
}
}
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey!!
I re-worked the Middleware protocol (spoiler: it's gonna be deprecated), but because I want to allow a smooth transition to the new protocol (MiddlewareProtocol), I'm keeping the old one as it was before.
The PR is https://github.com/SwiftRex/SwiftRex/pull/109/files and the branch is MiddlewareRefactorSecondAttempt
I would appreciate if you could try to use this branch in your projects before I merge it into develop. If you use EffectMiddleware you should not be affected at all. If you use Middleware protocol directly, it should continue working although you're going to receive the deprecation warning.
However, during my tests I have to explicitly provide the typealias for InputActionType, OutputActionType and StateType, somehow the compiler lost track of them. I assume this is not a huge breaking change but please let me know if this is a huge problem for you.
To migrate from Middleware to MiddlewareProtocol:
func handle(action: InputActionType, from dispatcher: ActionSource, afterReducer: inout AfterReducer)
tofunc handle(action: InputActionType, from dispatcher: ActionSource, state: @escaping GetState<StateType>) -> IO<OutputActionType>
GetState
you used to get from receiveContext.return IO
(AnyActionHandler) -> Void
, that gives way to dispatch actions, and it runs after the reducerDispatchQueue.main.async
anymore, so you can run withAnimation blocks from thereYou don't have to perform these steps right now, only when 1.0 is released, for now only the typealiases should be enough.
Please test this if you want to give me feedback on this topic. I'm planning to merge this next weekend, together with more tests that I'm writing.
If you want to support 1.0, you can also help me on tests and docs.
Unless something big comes up, THIS is the final public API now.
Beta Was this translation helpful? Give feedback.
All reactions