SwiftRex: Best option for actions triggering other actions ?
Context
In some applications, certain events / actions are a signal (no pun intended) for other actions to be performed. I.e. something like :
Action --> Action(s) -/…eventually…/-> State change
Following on the recent discussions around AppLifecycleMiddleware and CoreLocationMiddleware, let’s imagine we have a didFinishLaunchingWithOptions action that was dispatched by the AppLifecycleMiddleware. My app now wants to initiate several side effects, like turning on location updates, remote notifications and sending a ping to a remote system.
Since those should technically happen as a result of the application finishing to launch :
- How do I dispatch those other actions from, potentially, several different Middleware ?
- Do I need to create some sort of a RoutingMiddleware ?
- Is the EffectMiddleware a good candidate for this task ?
- Something else, not Middleware related ?
Thanks, N.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 28 (17 by maintainers)
Commits related to this issue
- Making the extension public to see if this will solve the issue seen in : https://github.com/SwiftRex/SwiftRex/issues/87#issuecomment-715469545 — committed to npvisual/AppLifecycleMiddleware by npvisual 4 years ago
- Testing Luiz' suggestion to see if this will solve the issue seen in : https://github.com/SwiftRex/SwiftRex/issues/87#issuecomment-715469545 — committed to npvisual/AppLifecycleMiddleware by npvisual 4 years ago
- Testing Luiz' suggestion to see if this will solve the issue seen in : https://github.com/SwiftRex/SwiftRex/issues/87#issuecomment-715469545 — committed to npvisual/AppLifecycleMiddleware by npvisual 4 years ago
- Changed the location of the generated files to avoid the error found as part of : https://github.com/SwiftRex/SwiftRex/issues/87#issuecomment-715469545 — committed to SwiftRex/CoreLocationMiddleware by npvisual 4 years ago
Please try this on your Playground and tell me what you think. The version with custom operator can also be used, just omitted now for simplicity.
Looks like the BridgeMiddleware can’t be combined via the
<>operator because it needs to conform toMiddleware, so I added that to the code your provided above :I also added the private variable
outputso it can be used in thehandlemethod. In addition thehandlemethod has to conform to what’s defined in theMiddlewareprotocol spec, in addition to declaring thereceiveContextmethod :And then I modified the
handlemethod to dispatch the matching action :Works pretty well when I match the
.didBecomeActiveto CoreLocation’s.requestAuthorizationStatus:Version with custom operator:
It’s soooo interesting to see that ArrowMap is actually a simple flatMap on Optional (Kleisli arrow):
((In) -> Value?) -> ((Value) -> Out?) -> ((In) -> Out?)This is how I do it today (using EffectMiddleware):
This is more or less my proposal:
This would be the usage:
I haven’t tested that, I’m writing by heart and may not exactly work as expected, a TDD approach would be better. Also I don’t see how to extract values from inbound action and put it into the outbound action, because I need the Equatable to ensure it’s matching the expected type, and I can’t do that with associated values. Furthermore, actions need to be equatable which is super hard to achieve.
I am trying to think how KeyPath could be useful, but I still don’t know how to compare these enum values to ensure they are the same tree, otherwise ignoring the incoming action. Maybe something with Mirror could help.
So suggestions are welcome.