Skip to content

Commit 644cd6c

Browse files
authored
feat: impl SequenceNode that executes its children sequentially (#7)
* feat: impl leaf node DelayNode * feat: impl SequenceNode that executes its children sequentially * refactor: apply assertSoftly * chore: apply spotless check * fix: remove unnecessary test case
1 parent a93b0cd commit 644cd6c

6 files changed

Lines changed: 378 additions & 6 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (C) 2026 compose-jindong
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.jindong.node
17+
18+
/**
19+
* A leaf node representing a time delay with no haptic output.
20+
*
21+
* This node has no children and returns an empty list when [collectEvents] is called.
22+
* The duration is used by parent nodes (e.g., [SequenceNode]) to advance timing.
23+
*
24+
* @property durationMs Duration of the delay in milliseconds
25+
*/
26+
internal class DelayNode(
27+
val durationMs: Long,
28+
) : HapticNode {
29+
override val children: MutableList<HapticNode> = mutableListOf()
30+
31+
override fun collectEvents(startTimeMs: Long): List<ScheduledHapticEvent> = emptyList()
32+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (C) 2026 compose-jindong
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.jindong.node
17+
18+
/**
19+
* A container node that executes its children sequentially.
20+
*
21+
* Each child starts after the previous one completes. The timing is calculated
22+
* by tracking the maximum end time of events from each child.
23+
*
24+
* Special handling for [DelayNode]: advances time without generating events.
25+
*
26+
* Example:
27+
* ```
28+
* Sequence {
29+
* Haptic(100.ms) // starts at 0ms, ends at 100ms
30+
* Delay(50.ms) // no event, advances to 150ms
31+
* Haptic(200.ms) // starts at 150ms, ends at 350ms
32+
* }
33+
* ```
34+
*/
35+
internal class SequenceNode : HapticNode {
36+
override val children: MutableList<HapticNode> = mutableListOf()
37+
38+
override fun collectEvents(startTimeMs: Long): List<ScheduledHapticEvent> {
39+
val (_, events) = children.fold(
40+
initial = TimingState(currentTimeMs = startTimeMs, events = emptyList()),
41+
) { state, child ->
42+
processChildNode(child, state)
43+
}
44+
return events
45+
}
46+
47+
private fun processChildNode(child: HapticNode, state: TimingState): TimingState = if (child is DelayNode) {
48+
state.copy(currentTimeMs = state.currentTimeMs + child.durationMs)
49+
} else {
50+
val childEvents = child.collectEvents(state.currentTimeMs)
51+
val nextStartTime = calculateNextStartTime(childEvents, state.currentTimeMs)
52+
TimingState(
53+
currentTimeMs = nextStartTime,
54+
events = state.events + childEvents,
55+
)
56+
}
57+
58+
private fun calculateNextStartTime(
59+
events: List<ScheduledHapticEvent>,
60+
currentTimeMs: Long,
61+
): Long = if (events.isEmpty()) {
62+
currentTimeMs
63+
} else {
64+
events.maxOf { it.startTimeMs + it.durationMs }
65+
}
66+
67+
private data class TimingState(
68+
val currentTimeMs: Long,
69+
val events: List<ScheduledHapticEvent>,
70+
)
71+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (C) 2026 compose-jindong
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.jindong.node
17+
18+
import io.kotest.assertions.assertSoftly
19+
import io.kotest.core.spec.style.FunSpec
20+
import io.kotest.matchers.collections.shouldBeEmpty
21+
import io.kotest.matchers.shouldBe
22+
23+
class DelayNodeTest :
24+
FunSpec({
25+
test("collectEvents should return empty list") {
26+
val node = DelayNode(durationMs = 100)
27+
28+
val events = node.collectEvents(startTimeMs = 0)
29+
30+
events.shouldBeEmpty()
31+
}
32+
33+
test("collectEvents should return empty list regardless of startTimeMs") {
34+
val node = DelayNode(durationMs = 200)
35+
36+
val events = node.collectEvents(startTimeMs = 500)
37+
38+
events.shouldBeEmpty()
39+
}
40+
41+
test("children should always be empty for leaf node") {
42+
val node = DelayNode(durationMs = 150)
43+
44+
node.children.shouldBeEmpty()
45+
}
46+
47+
test("durationMs should be preserved") {
48+
val node = DelayNode(durationMs = 300)
49+
50+
node.durationMs shouldBe 300
51+
}
52+
53+
test("should support zero duration") {
54+
val node = DelayNode(durationMs = 0)
55+
56+
assertSoftly {
57+
node.durationMs shouldBe 0
58+
node.collectEvents(startTimeMs = 0).shouldBeEmpty()
59+
}
60+
}
61+
})

jindong/src/commonTest/kotlin/io/github/jindong/node/HapticEventNodeTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class HapticEventNodeTest :
5353
val events = node.collectEvents(startTimeMs = 0)
5454

5555
events shouldHaveSize 1
56-
with(events[0]) {
56+
assertSoftly(events.single()) {
5757
durationMs shouldBe 250
5858
intensity shouldBe 0.3f
5959
iosParameters?.sharpness shouldBe 0.7f

jindong/src/commonTest/kotlin/io/github/jindong/node/ScheduledHapticEventTest.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ class ScheduledHapticEventTest :
5454
iosParameters = iosParams,
5555
)
5656

57-
event.startTimeMs shouldBe 100
58-
event.durationMs shouldBe 200
59-
event.intensity shouldBe 0.5f
60-
event.iosParameters shouldBe iosParams
61-
event.iosParameters?.sharpness shouldBe 0.9f
57+
assertSoftly(event) {
58+
startTimeMs shouldBe 100
59+
durationMs shouldBe 200
60+
intensity shouldBe 0.5f
61+
iosParameters shouldBe iosParams
62+
iosParameters?.sharpness shouldBe 0.9f
63+
}
6264
}
6365
})

0 commit comments

Comments
 (0)