Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions way/src/commonMain/kotlin/ru/kode/way/NavigationService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ class NavigationService<R : Any>(
* 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<Path>() else null
fun resolve(from: Path?) = resolveTransition(
regions = state.regions,
nodeBuilder = nodeBuilder,
Expand All @@ -366,11 +368,11 @@ class NavigationService<R : Any>(
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)
}
Expand All @@ -391,8 +393,9 @@ class NavigationService<R : Any>(
* 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].
*
Expand Down
25 changes: 20 additions & 5 deletions way/src/commonMain/kotlin/ru/kode/way/TargetResolution.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path>? = 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 } }
Expand Down Expand Up @@ -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,
Expand All @@ -78,6 +84,7 @@ internal fun resolveTransition(
extensionPoints = extensionPoints,
allRegions = regions,
history = history,
consulted = consulted,
)
acc + resolved
}
Expand Down Expand Up @@ -117,6 +124,7 @@ internal fun resolveTransition(
nodeBuilder = nodeBuilder,
allRegions = regions,
history = history,
consulted = consulted,
)
acc + resolved
}
Expand Down Expand Up @@ -159,6 +167,7 @@ internal fun resolveTransition(
nodeBuilder = nodeBuilder,
allRegions = regions,
history = history,
consulted = consulted,
)
}

Expand All @@ -178,8 +187,9 @@ private fun resolveParallelTransition(
nodeBuilder: NodeBuilder,
allRegions: Map<RegionId, Region>,
history: Map<Path, HistoryRecord> = emptyMap(),
consulted: MutableSet<Path>? = null,
): ResolvedTransition {
val transition = buildTransition(event, parallelNode, parallelNodePath, extensionPoints)
val transition = buildTransition(event, parallelNode, parallelNodePath, extensionPoints, consulted)
return resolveParallelInner(
parallelNode = parallelNode,
parallelNodePath = parallelNodePath,
Expand Down Expand Up @@ -339,6 +349,7 @@ private fun resolveTransitionInRegion(
extensionPoints: List<NodeExtensionPoint>,
allRegions: Map<RegionId, Region>,
history: Map<Path, HistoryRecord> = emptyMap(),
consulted: MutableSet<Path>? = null,
): ResolvedTransition = when (transition) {
is EnqueueEvent -> ResolvedTransition(
targetPaths = mapOf(regionId to activePath),
Expand Down Expand Up @@ -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,
Expand All @@ -478,6 +489,7 @@ private fun resolveTransitionInRegion(
extensionPoints,
allRegions,
history,
consulted,
)
}
}
Expand Down Expand Up @@ -882,7 +894,10 @@ private fun buildTransition(
node: Node,
path: Path,
extensionPoints: List<NodeExtensionPoint>,
): Transition = if (event is InitEvent) {
consulted: MutableSet<Path>? = null,
): Transition = if (consulted != null && !consulted.add(path)) {
Ignore
} else if (event is InitEvent) {
when (node) {
is FlowNode<*> -> {
NavigateTo(node.initial)
Expand Down
63 changes: 58 additions & 5 deletions way/src/commonTest/kotlin/ru/kode/way/ParallelNodeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<NavigationState>()
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<Event>()
val sut = buildPar02Service(
createMainNode = {
TestParallelNode(
parallelTransitions = listOf(TestParallelTransitionSpec({ it == TestEvent("S") }, Stay)),
onTransitionCallback = { parallelEvents.add(it) },
)
},
)
val states = mutableListOf<NavigationState>()
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<Event>()
val sut = buildPar02Service(
Expand Down Expand Up @@ -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",
Expand All @@ -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") {
Expand Down
Loading