Skip to content

Commit

Permalink
feat: streamlined approach for Behaviors #1444 (#1445)
Browse files Browse the repository at this point in the history
* feat: streamlined approach for Behaviors #1444

* rename method

* scaladsl and test

* simpler test

* add @SInCE annotation
  • Loading branch information
Roiocam authored Aug 28, 2024
1 parent 5a3f6f1 commit db8d20d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,22 @@ class ReceiveBehaviorSpec extends Messages with BecomeWithLifecycle with Stoppab
}
}

class ReceiveMessageWithSameBehaviorSpec extends Messages {
override def behavior(monitor: ActorRef[Event]): (Behavior[Command], Aux) = behv(monitor) -> null
private def behv(monitor: ActorRef[Event]): Behavior[Command] = {
SBehaviors
.receiveMessageWithSame[Command] {
case Miss =>
monitor ! Missed
case Ignore =>
monitor ! Ignored
case Ping =>
monitor ! Pong
case _ =>
}
}
}

class ImmutableWithSignalScalaBehaviorSpec extends Messages with BecomeWithLifecycle with Stoppable {

override def behavior(monitor: ActorRef[Event]): (Behavior[Command], Aux) = behv(monitor) -> null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,29 @@ object Behaviors {
def receiveMessage[T](onMessage: pekko.japi.Function[T, Behavior[T]]): Behavior[T] =
new BehaviorImpl.ReceiveBehavior((_, msg) => onMessage.apply(msg))

/**
* Simplified version of [[receiveMessage]] with only a single argument - the message
* to be handled, but it doesn't produce a return value of next behavior.
* Useful for when the behavior doesn't want to change in runtime.
*
* Construct an actor behavior that can react to incoming messages but not to
* lifecycle signals. After spawning this actor from another actor (or as the
* guardian of an [[pekko.actor.typed.ActorSystem]]) it will be executed within an
* [[ActorContext]] that allows access to the system, spawning and watching
* other actors, etc.
*
* Compared to using [[AbstractBehavior]] this factory is a more functional style
* of defining the `Behavior`. Processing the next message will not result in
* different behavior than this one
*
* @since 1.1.0
*/
def receiveMessageWithSame[T](onMessage: pekko.japi.Procedure[T]): Behavior[T] =
new BehaviorImpl.ReceiveBehavior((_, msg) => {
onMessage.apply(msg)
same[T]
})

/**
* Construct an actor behavior that can react to both incoming messages and
* lifecycle signals. After spawning this actor from another actor (or as the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,27 @@ object Behaviors {
def receiveMessage[T](onMessage: T => Behavior[T]): Receive[T] =
new ReceiveMessageImpl(onMessage)

/**
* Simplified version of [[receiveMessage]] with only a single argument - the message
* to be handled, but it doesn't produce a return value of next behavior.
* Useful for when the behavior doesn't want to change in runtime.
*
* Construct an actor behavior that can react to incoming messages but not to
* lifecycle signals. After spawning this actor from another actor (or as the
* guardian of an [[pekko.actor.typed.ActorSystem]]) it will be executed within an
* [[ActorContext]] that allows access to the system, spawning and watching
* other actors, etc.
*
* Compared to using [[AbstractBehavior]] this factory is a more functional style
* of defining the `Behavior`. Processing the next message will not result in
* different behavior than this one
*
* @since 1.1.0
*/
def receiveMessageWithSame[T](onMessage: T => Unit): Receive[T] = {
new ReceiveMessageImpl(onMessage.andThen(_ => same))
}

/**
* Construct an actor `Behavior` from a partial message handler which treats undefined messages as unhandled.
*/
Expand Down

0 comments on commit db8d20d

Please sign in to comment.