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
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>|nullhasNewState()
Whether the handler returned a new state (even if that state is null).
public
hasNewState() : bool
Return values
boolisStopped()
public
isStopped() : bool
Return values
boolnext()
Same behavior, new state.
public
static next(NS $state) : BehaviorWithState<object, NS>
Parameters
- $state : NS
Tags
Return values
BehaviorWithState<object, NS>same()
Keep both behavior and state.
public
static same() : BehaviorWithState<T, S>
Return values
BehaviorWithState<T, S>state()
The new state value. Only meaningful when hasNewState() returns true.
public
state() : S
Return values
Sstopped()
Stop the actor.
public
static stopped() : BehaviorWithState<T, S>
Return values
BehaviorWithState<T, S>withBehavior()
Switch both behavior and state.
public
static withBehavior(Behavior<string|int, U> $behavior, NS $state) : BehaviorWithState<U, NS>
Parameters
- $behavior : Behavior<string|int, U>
- $state : NS