-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add trigger information to
SideEffect
to allow for differentiated b…
…ehavior
- Loading branch information
Showing
15 changed files
with
146 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 5 additions & 12 deletions
17
example/src/main/scala/endless/example/logic/VehicleSideEffect.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,21 @@ | ||
package endless.example.logic | ||
|
||
import cats.Applicative | ||
import cats.effect.{Concurrent, Ref} | ||
import cats.{Applicative, Monad} | ||
import cats.syntax.flatMap.* | ||
import cats.syntax.functor.* | ||
import endless.core.entity.SideEffect.Trigger | ||
import endless.core.entity.{Effector, SideEffect} | ||
import endless.example.algebra.VehicleAlg | ||
import endless.example.data.Vehicle | ||
|
||
import scala.concurrent.duration.* | ||
|
||
class VehicleSideEffect[F[_]: Concurrent](justRecoveredRef: Ref[F, Boolean]) | ||
extends SideEffect[F, Vehicle, VehicleAlg] { | ||
def apply(effector: Effector[F, Vehicle, VehicleAlg]): F[Unit] = { | ||
class VehicleSideEffect[F[_]: Monad] extends SideEffect[F, Vehicle, VehicleAlg] { | ||
def apply(trigger: Trigger, effector: Effector[F, Vehicle, VehicleAlg]): F[Unit] = { | ||
lazy val aggressivePassivation = effector.enablePassivation(1.second) | ||
for { | ||
justRecovered <- justRecoveredRef.getAndUpdate(_ => false) | ||
_ <- Applicative[F].whenA(justRecovered)(effector.self.incrementRecoveryCount) | ||
_ <- Applicative[F].whenA(trigger.isAfterRecovery)(effector.self.incrementRecoveryCount) | ||
_ <- aggressivePassivation | ||
} yield () | ||
} | ||
} | ||
|
||
object VehicleSideEffect { | ||
def apply[F[_]: Concurrent](): F[SideEffect[F, Vehicle, VehicleAlg]] = | ||
Ref[F].of(true).map(new VehicleSideEffect[F](_)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.