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

see
StatefulActorHandler

for the class-based alternative

psalm-api
psalm-immutable
template

T of object

template

S

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<object, NS>
Same behavior, new state.
same()  : BehaviorWithState<T, S>
Keep both behavior and state.
state()  : S
The new state value. Only meaningful when hasNewState() returns true.
stopped()  : BehaviorWithState<T, S>
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

state()

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

public state() : S
Return values
S

        
On this page

Search results