Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: streamlined approach for Behaviors #1444 #1445

Merged
merged 5 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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