Idea: Allow EffectRouter to be passed to Mobius.loop directly#114
Idea: Allow EffectRouter to be passed to Mobius.loop directly#114
Conversation
|
This also allows effect routers to be written without explicitly naming the effect and event types, although I admit it ends up looking a bit weird: loop = Mobius.loop(
update: update,
effectHandler: EffectRouter()
.routeEffects(equalTo: .testEffect).to(testEffectHandler)
)
.start(from: 0) |
Codecov Report
@@ Coverage Diff @@
## master #114 +/- ##
==========================================
+ Coverage 92.3% 92.32% +0.02%
==========================================
Files 46 46
Lines 1442 1446 +4
==========================================
+ Hits 1331 1335 +4
Misses 111 111
Continue to review full report at Codecov.
|
|
I very much like this idea! The |
jeppes
left a comment
There was a problem hiding this comment.
Thanks for this! I've been wanting to make this change too.
A few things about the implementation though - I think it would be simpler to just add an overload for the loop builder constructor that takes an EffectRouter instead. That way we can avoid the _EffectHandlerConvertible type and _asEffectHandlerConnectable() can be replaced with a call to the existing asConnectable
| } | ||
|
|
||
| extension EffectRouter: _EffectHandlerConvertible { | ||
| public func _asEffectHandlerConnectable() -> AnyConnectable<Effect, Event> { |
There was a problem hiding this comment.
Should the _asEffectHandlerConnectable() methods be public?
There was a problem hiding this comment.
Yes; this is necessary since protocols can’t have multiple levels of access control. In order to publicly conform to the protocol the method must be implemented publicly.
|
@jeppes Do you expect we’ll need the |
|
@JensAyton I think we either need to keep |
|
Sorry to bump an old thread, but was something like this ruled out? extension EffectRouter: Connectable {
public func connect(_ consumer: @escaping Consumer<Event>) -> Connection<Effect> {
return compose(routes: routes).connect(consumer)
}
}It seems like it would "just work" and avoid the need for any other API changes. |
The
asConnectableat the end of anEffectRoutersetup is an annoying abstraction leak. Supporting an array of multipleupdatetypes and multipleeffectHandlertypes is pretty unattractive, though.Here’s a third option: use an underscored protocol to provide a common supertype to
EffectRouter<Effect, Event>andConnectable where Input == Effect, Output == Event. I’m not convinced myself, the extra associated types are a pain, but I thought it was worth looking at.@jeppes