Skip to content

Commit 4c8a21e

Browse files
committed
Add an automated subscription lifecycle trigger
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
1 parent 28e3e05 commit 4c8a21e

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/main/java/org/sert2521/gamename/util/SwitchUtils.kt

+19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import org.strongback.SwitchReactor
44
import org.strongback.command.Command
55
import org.strongback.command.Requirable
66
import org.strongback.components.Switch
7+
import java.util.concurrent.atomic.AtomicBoolean
78
import java.util.function.Supplier
89

910
fun SwitchReactor.whileTriggeredSubmit(switch: Switch, supplier: Supplier<Command>) {
@@ -25,6 +26,24 @@ fun SwitchReactor.whileTriggeredSubmit(switch: Switch, supplier: Supplier<Comman
2526
})
2627
}
2728

29+
fun SwitchReactor.onTriggeredLifecycleSubmit(
30+
switch: Switch,
31+
supplier: Supplier<Command>
32+
) = onTriggeredSubmit(switch, object : Supplier<Command> {
33+
private val cancelRequireable = object : Requirable {}
34+
private val isRunning = AtomicBoolean()
35+
private val requirements: Set<Requirable> =
36+
supplier.get().requirements.toMutableSet().apply {
37+
add(cancelRequireable)
38+
}
39+
40+
override fun get(): Command = if (isRunning.getAndSet(!isRunning.get())) {
41+
Command.cancel(cancelRequireable)
42+
} else {
43+
CommandWrapper(supplier.get(), overrideRequirements = requirements)
44+
}
45+
})
46+
2847
private class CommandWrapper(
2948
private val original: Command,
3049
overrideTimeoutInSeconds: Double = original.timeoutInSeconds,

0 commit comments

Comments
 (0)