Nexus API Reference

BehaviorWithState
in package

Read onlyYes
FinalYes

Result type returned from a stateful actor handler.

When an actor is defined with Behavior::withState() or StatefulActorHandler::handle(), the handler returns a BehaviorWithState<T, S> on every message. The four factory methods encode all legal outcomes: advance state, keep state unchanged, stop the actor, or atomically swap both behavior and state.

Example:

$behavior = Behavior::withState(
    0,
    static function (ActorContext $ctx, object $msg, int $count): BehaviorWithState {
        return match (true) {
            $msg instanceof Increment => BehaviorWithState::next($count + 1),
            $msg instanceof Reset     => BehaviorWithState::next(0),
            $msg instanceof GetCount  => (static function () use ($ctx, $count): BehaviorWithState {
                $ctx->sender()?->tell(new CountValue($count));
                return BehaviorWithState::same();
            })(),
            default => BehaviorWithState::same(),
        };
    },
);
Tags
see
Behavior::withState()

for the factory that creates stateful behaviors

StatefulActorHandler

for the class-based alternative

psalm-api
psalm-immutable
template-covariant

of object

Table of Contents

Methods

behavior()  : Behavior<string|int, T>|null
The new behavior to switch to, or null to keep the current behavior.
hasNewState()  : bool
Whether the handler returned a new state (even if that state is null).
isStopped()  : bool
next()  : BehaviorWithState<never, NS>
Same behavior, new state.
same()  : BehaviorWithState<never, never>
Keep both behavior and state.
state()  : S
The new state value. Only meaningful when hasNewState() returns true.
stopped()  : BehaviorWithState<never, never>
Stop the actor.
withBehavior()  : BehaviorWithState<U, NS>
Switch both behavior and state.

Methods

behavior()

The new behavior to switch to, or null to keep the current behavior.

public behavior() : Behavior<string|int, T>|null
Return values
Behavior<string|int, T>|null

hasNewState()

Whether the handler returned a new state (even if that state is null).

public hasNewState() : bool
Return values
bool

same()

Keep both behavior and state.

public static same() : BehaviorWithState<never, never>

never is the bottom type: under covariance the returned marker is a subtype of every BehaviorWithState<T, S> instantiation (the stateful analog of Akka's Behaviors.same).

Return values
BehaviorWithState<never, never>

state()

The new state value. Only meaningful when hasNewState() returns true.

public state() : S
Return values
S
On this page

Search results