-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Idea: Allow EffectRouter to be passed to Mobius.loop directly #114
base: master
Are you sure you want to change the base?
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
@@ -72,6 +72,12 @@ public struct EffectRouter<Effect, Event> { | |||
} | |||
} | |||
|
|||
extension EffectRouter: _EffectHandlerConvertible { | |||
public func _asEffectHandlerConnectable() -> AnyConnectable<Effect, Event> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the _asEffectHandlerConnectable()
methods be public?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
asConnectable
at the end of anEffectRouter
setup is an annoying abstraction leak. Supporting an array of multipleupdate
types and multipleeffectHandler
types 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