From 124ea6944110440bc7cd2afa0f79c0b014880ff6 Mon Sep 17 00:00:00 2001 From: rinekri Date: Tue, 6 Oct 2026 13:41:16 +0100 Subject: [PATCH] Ask each node once when a sink's event falls back to the whole tree (0.10.3) An event sent through a node's eventSink which nobody handled in the sink's scope was resolved again from the whole tree, so the scope's nodes got it twice: transition() and the onPreTransition hooks ran again. A node doing its work in transition() and answering Ignore did that work twice. The scoped resolution and the fallback now share the set of paths already asked; a node in it is skipped and counts as Ignore. --- CHANGELOG.md | 9 +++ README.md | 9 +-- gradle.properties | 2 +- .../kotlin/ru/kode/way/NavigationService.kt | 13 ++-- .../kotlin/ru/kode/way/TargetResolution.kt | 25 ++++++-- .../kotlin/ru/kode/way/ParallelNodeTest.kt | 63 +++++++++++++++++-- 6 files changed, 101 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63caceb..1edff30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## 0.10.3 - 2026-10-06 + +### Fixed + +* An event sent through a node's `eventSink` which nobody handled in the sink's scope was delivered to the scope's + nodes a second time by the whole-tree fallback: `transition()` and the `onPreTransition` hooks ran twice, so a node + doing its work in `transition()` and answering `Ignore` (or a parallel answering `Stay`) did that work twice. The + fallback now skips the nodes which were already asked, so every node gets the event at most once. + ## 0.10.2 - 2026-10-05 ### Fixed diff --git a/README.md b/README.md index e780e3c..85df3cd 100644 --- a/README.md +++ b/README.md @@ -253,10 +253,11 @@ enable it (recommended for debug builds and tests). `NodeHost(nodeBuilder, ...)` - bubble up on `Ignore` through the node to its ancestors within the region, and (except `Event.Back`) also reach every parallel enclosing the node and bubble up through each parallel's ancestors, like `service.send` (same region order, same merge); -- a non-Back event which nothing handled in that scope (no target, no enqueued event) falls back to the whole tree, - resolved exactly as `service.send` would, so a screen in region A can send an event only a node in sibling region B - handles. An event handled in scope is not sent again. The scope's nodes are consulted twice on a fallback - (`transition` and `onPreTransition` run again), and a parallel answering `Stay` falls through as well; +- a non-Back event which nothing handled in that scope (no target, no enqueued event) falls back to the rest of the + tree, in the order `service.send` would ask it, so a screen in region A can send an event only a node in sibling + region B handles. An event handled in scope is not sent again, and the nodes already asked in scope are not asked + again on the fallback, so each node gets the event at most once. A node which does its work and answers `Ignore` + does not stop the fallback, and neither does a parallel answering `Stay`; - `Event.Back` stays in the node's region (never reaching enclosing parallels or sibling regions): a parallel's sink routes it through that parallel's `DispatchBackTo`, a sink of a node inside a region keeps it in that region; - are checked when dispatched, not when sent: if the node has left navigation or was recreated meanwhile, the event diff --git a/gradle.properties b/gradle.properties index acb34c7..36a0a39 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ kotlin.native.ignoreDisabledTargets=true android.useAndroidX=true -versionName=0.10.2 +versionName=0.10.3 pomGroupId=ru.kode pomDescription=Navigation library based on statechart-like node graphs pomUrl=https://github.com/appKODE/way diff --git a/way/src/commonMain/kotlin/ru/kode/way/NavigationService.kt b/way/src/commonMain/kotlin/ru/kode/way/NavigationService.kt index 1def4e9..d167c5e 100644 --- a/way/src/commonMain/kotlin/ru/kode/way/NavigationService.kt +++ b/way/src/commonMain/kotlin/ru/kode/way/NavigationService.kt @@ -355,6 +355,8 @@ class NavigationService( * target needs a payload nobody has, before anything is mounted or exited. */ private fun resolveCheckedTransition(state: NavigationState, event: Event, source: Path?): ResolvedTransition { + // Shared by the scoped resolution and its fallback, so no node is asked about the event twice. + val consulted = if (source != null && event != Event.Back) mutableSetOf() else null fun resolve(from: Path?) = resolveTransition( regions = state.regions, nodeBuilder = nodeBuilder, @@ -366,11 +368,11 @@ class NavigationService( intermediateParallels = state._intermediateParallels, history = state._history, source = from, + consulted = consulted, ) var resolvedTransition = resolve(source) - // An event nobody handled in the sink's scope falls back to the whole tree, like send (Back never does). - // Known limits: the scope's nodes are consulted twice (transition and onPreTransition run again), and a parallel - // answering Stay resolves to EMPTY as well, so it falls through too. + // An event nobody handled in the sink's scope goes on to the rest of the tree, in the order send would ask it + // (Back never does). A parallel answering Stay resolves to EMPTY as well, so the event goes on past it too. if (source != null && event != Event.Back && resolvedTransition == ResolvedTransition.EMPTY) { resolvedTransition = resolve(null) } @@ -391,8 +393,9 @@ class NavigationService( * region under it, so its active children handle the event first, and the parallels under it handle it too. The * event then bubbles up on [Ignore] through the node to its ancestors within the region and, except for * [Event.Back], also reaches every parallel enclosing the node and bubbles up through each parallel's ancestors, - * like [send] (same region order, same merge). If nothing in that scope handled the event, it falls back to the whole - * tree, exactly as [send] would resolve it, so a sibling region can handle it. Back never falls back: it stays in the node's + * like [send] (same region order, same merge). If nothing in that scope handled the event, it falls back to the rest + * of the tree, in the order [send] would ask it, so a sibling region can handle it; the nodes already asked are not + * asked again. Back never falls back: it stays in the node's * region, never reaching enclosing parallels or sibling regions: routed through the node's own `DispatchBackTo` if it is a * parallel, kept in the node's region otherwise. The root node's sink is equivalent to [send]. * diff --git a/way/src/commonMain/kotlin/ru/kode/way/TargetResolution.kt b/way/src/commonMain/kotlin/ru/kode/way/TargetResolution.kt index 6cd3dcc..444f167 100644 --- a/way/src/commonMain/kotlin/ru/kode/way/TargetResolution.kt +++ b/way/src/commonMain/kotlin/ru/kode/way/TargetResolution.kt @@ -15,9 +15,15 @@ internal fun resolveTransition( * parallel's sink at the active leaves of the regions under it (and the parallels under it). The event then bubbles * up on `Ignore` within the region and, except for Back, also reaches the parallels enclosing [source], like a plain * `send` does. `null` means the whole tree, which is what a plain `send` does. The caller re-resolves with `null` when - * a non-Back event resolved to nothing here, so events nobody handles in this scope reach sibling regions. + * a non-Back event resolved to nothing here, so events nobody handles in this scope reach the rest of the tree. */ source: Path? = null, + /** + * Paths of the nodes already asked about [event]. A node in it is not asked again (neither its `transition` nor the + * `onPreTransition` hooks run) and counts as `Ignore`; every node asked here is added. The caller shares one set + * between the scoped resolution and its whole-tree fallback, so each node handles the event at most once. + */ + consulted: MutableSet? = null, ): ResolvedTransition { // Assumes a screen path lives in exactly one region's nodes. val sourceScreen = source?.takeIf { s -> regions.values.any { it.nodes[s] is ScreenNode } } @@ -65,7 +71,7 @@ internal fun resolveTransition( val transition = if (event is RootFinishRequestEvent) { region._rootFinishTransitionBuilder(event.result) } else { - buildTransition(event, node, start, extensionPoints) + buildTransition(event, node, start, extensionPoints, consulted) } val resolved = resolveTransitionInRegion( regionId = regionId, @@ -78,6 +84,7 @@ internal fun resolveTransition( extensionPoints = extensionPoints, allRegions = regions, history = history, + consulted = consulted, ) acc + resolved } @@ -117,6 +124,7 @@ internal fun resolveTransition( nodeBuilder = nodeBuilder, allRegions = regions, history = history, + consulted = consulted, ) acc + resolved } @@ -159,6 +167,7 @@ internal fun resolveTransition( nodeBuilder = nodeBuilder, allRegions = regions, history = history, + consulted = consulted, ) } @@ -178,8 +187,9 @@ private fun resolveParallelTransition( nodeBuilder: NodeBuilder, allRegions: Map, history: Map = emptyMap(), + consulted: MutableSet? = null, ): ResolvedTransition { - val transition = buildTransition(event, parallelNode, parallelNodePath, extensionPoints) + val transition = buildTransition(event, parallelNode, parallelNodePath, extensionPoints, consulted) return resolveParallelInner( parallelNode = parallelNode, parallelNodePath = parallelNodePath, @@ -339,6 +349,7 @@ private fun resolveTransitionInRegion( extensionPoints: List, allRegions: Map, history: Map = emptyMap(), + consulted: MutableSet? = null, ): ResolvedTransition = when (transition) { is EnqueueEvent -> ResolvedTransition( targetPaths = mapOf(regionId to activePath), @@ -469,7 +480,7 @@ private fun resolveTransitionInRegion( val node = nodes[parentPath] ?: error("expected node to exist at path \"${parentPath}\"") resolveTransitionInRegion( regionId, - buildTransition(event, node, parentPath, extensionPoints), + buildTransition(event, node, parentPath, extensionPoints, consulted), parentPath, activePath, nodes, @@ -478,6 +489,7 @@ private fun resolveTransitionInRegion( extensionPoints, allRegions, history, + consulted, ) } } @@ -882,7 +894,10 @@ private fun buildTransition( node: Node, path: Path, extensionPoints: List, -): Transition = if (event is InitEvent) { + consulted: MutableSet? = null, +): Transition = if (consulted != null && !consulted.add(path)) { + Ignore +} else if (event is InitEvent) { when (node) { is FlowNode<*> -> { NavigateTo(node.initial) diff --git a/way/src/commonTest/kotlin/ru/kode/way/ParallelNodeTest.kt b/way/src/commonTest/kotlin/ru/kode/way/ParallelNodeTest.kt index ec12b33..0695487 100644 --- a/way/src/commonTest/kotlin/ru/kode/way/ParallelNodeTest.kt +++ b/way/src/commonTest/kotlin/ru/kode/way/ParallelNodeTest.kt @@ -653,12 +653,65 @@ class ParallelNodeTest : ShouldSpec() { sut.eventSink(betaActive).send(TestEvent("goToScreen2")) - // The parallel is consulted in scope (Ignore) and again on the whole-tree fallback, which reaches alpha. - parallelEvents.count { it == TestEvent("goToScreen2") } shouldBe 2 + // The parallel is consulted in scope (Ignore) and skipped on the whole-tree fallback, which reaches alpha. + parallelEvents.count { it == TestEvent("goToScreen2") } shouldBe 1 states.last().regionByName("par02Alpha")!!.active.lastSegment().name shouldBe "par02AlphaScreen2" states.last().regionByName("par02Alpha")!!.active shouldNotBe alphaActive } + should("a flow which ignored a sink's event in scope is not asked again when the event falls back") { + var alphaAsked = 0 + var betaAsked = 0 + val sut = buildPar02Service( + alphaTransitions = listOf( + TestFlowTransitionSpec( + eventMatcher = { + if (it == TestEvent("X")) alphaAsked++ + false + }, + transition = Ignore, + ), + ), + betaTransitions = listOf( + TestFlowTransitionSpec( + eventMatcher = { + if (it == TestEvent("X")) betaAsked++ + false + }, + transition = Ignore, + ), + ), + ) + val states = mutableListOf() + sut.addTransitionListener { states.add(it) } + sut.start() + + sut.eventSink(states.last().regionByName("par02Alpha")!!.active).send(TestEvent("X")) + + alphaAsked shouldBe 1 + betaAsked shouldBe 1 + } + + should("a parallel answering Stay to its own sink's event is asked once and the event still reaches the tree") { + val parallelEvents = mutableListOf() + val sut = buildPar02Service( + createMainNode = { + TestParallelNode( + parallelTransitions = listOf(TestParallelTransitionSpec({ it == TestEvent("S") }, Stay)), + onTransitionCallback = { parallelEvents.add(it) }, + ) + }, + ) + val states = mutableListOf() + sut.addTransitionListener { states.add(it) } + sut.start() + val mainPath = states.last().regionByName("par02Alpha")!!.active.dropLast(2) + + sut.eventSink(mainPath).send(TestEvent("S")) + + parallelEvents.count { it == TestEvent("S") } shouldBe 1 + } + should("a screen sink's event nobody handles in scope reaches a flow in the sibling region") { val parallelEvents = mutableListOf() val sut = buildPar02Service( @@ -1500,8 +1553,8 @@ class ParallelNodeTest : ShouldSpec() { sut.eventSink(alphaPath).send(TestEvent("X")) - // Nobody handles X in the scope, so it falls back to the whole tree: the scope's nodes are consulted twice and - // par04Beta's leaves once, on the fallback. + // Nobody handles X in the scope, so it falls back to the whole tree: the scope's nodes are consulted once, in + // scope, and par04Beta's leaves once, on the fallback. val scope = listOf( "par04InnerAScreen2", "par04InnerAScreen1", @@ -1512,7 +1565,7 @@ class ParallelNodeTest : ShouldSpec() { "par04Main", "par04App", ) - consulted.shouldContainExactlyInAnyOrder(scope + scope + listOf("par04BetaScreen", "par04Beta")) + consulted.shouldContainExactlyInAnyOrder(scope + listOf("par04BetaScreen", "par04Beta")) } should("same event dispatched to two active sub-regions is handled independently by each") {