Arkadii Ivanov
07/12/2022, 9:20 AMRouter
API - the childFactory
callback is called during the Router
initialization, and it's not possible to obtain the `Router`'s reference safely from the callback.
E.g. consider the following snippet:
private val router =
router(initialConfiguration = Config.Main) {
// Can't use router here
}
Also when using separate function for the child factory, there is a foot gun:
private val router = router(initialConfiguration = Config.Main, childFactory = ::child)
private fun child(...): Child {
// The router property may be null at this point
DetailsComponent(onFinished = router::pop) // This may crash with NPE when onFinished is called
DetailsComponent(onFinished = { router.pop() }) // This is OK
}
I have been working on the new API for a couple of weeks, there are multiple ideas with various pros and cons. The API on the screenshot is what I liked the most. The idea is to introduce separate ChildNavigator
entity which is just a relay of navigation events. The childStack
function subscribes to ChildNavigator
events and handles all the routing job, and returns Value<ChildStack>
. ChildStack
is essentially the same thing as RouterState
currently. This way we have the reference to ChildNavigator
beforehand. But there is no such a single entity as Router
that could navigate
and provide RouterState
at the same time.
What do you think?Adam Brown
07/14/2022, 6:17 AMAdam Brown
07/14/2022, 6:17 AMstack
and navigation
we get around it?Adam Brown
07/14/2022, 6:18 AMnavigation
is just a mutator of the stack
obj thenAdam Brown
07/14/2022, 6:19 AMArkadii Ivanov
07/14/2022, 8:03 AMStackNavigation
is just a simple relay. It's like observable and consumer at the same time. The childStack
function receives StackNavigation
and subscribes to its events. Then when you call navigation.navigate {}
(or push
, pop
, etc), its immediately emits an Event
with the transformer
and onComplete
supplied). The internal logic of childStack
receives the event does all the job. And updates Value<ChildStack>
. There is already an open PR - https://github.com/arkivanov/Decompose/pull/140Andrew Steinmetz
07/14/2022, 7:07 PMStackNavigation
to be an interesting api, although if this is just to solve for the method reference issue I personally would prefer to just use the lambda reference to avoid separating out the the two in the router for this edge case. Although if there is a desire to use method reference, then this API seems like a reasonable change to enable that.Arkadii Ivanov
07/14/2022, 7:28 PMAndrew Steinmetz
07/14/2022, 7:36 PMArkadii Ivanov
07/14/2022, 7:39 PM