@@ -4,6 +4,7 @@ import org.strongback.SwitchReactor
4
4
import org.strongback.command.Command
5
5
import org.strongback.command.Requirable
6
6
import org.strongback.components.Switch
7
+ import java.util.concurrent.atomic.AtomicBoolean
7
8
import java.util.function.Supplier
8
9
9
10
fun SwitchReactor.whileTriggeredSubmit (switch : Switch , supplier : Supplier <Command >) {
@@ -25,6 +26,24 @@ fun SwitchReactor.whileTriggeredSubmit(switch: Switch, supplier: Supplier<Comman
25
26
})
26
27
}
27
28
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
+
28
47
private class CommandWrapper (
29
48
private val original : Command ,
30
49
overrideTimeoutInSeconds : Double = original.timeoutInSeconds,
0 commit comments