Aller au contenu
Français

Behaviors

Ce contenu n’est pas encore disponible dans votre langue.

const Behaviors: object

Defined in: src/typed/Behaviors.ts:64

Factory for building Behaviors — the functional facade over the OO Actor API. Use these combinators to compose an actor’s logic as a tree of values rather than as an imperative class.

get empty(): EmptyBehavior

Sentinel: accept messages but do nothing — useful as a placeholder.

EmptyBehavior

get ignore(): IgnoreBehavior

Sentinel: drop every incoming message silently.

IgnoreBehavior

get same(): SameBehavior

Sentinel: keep the current behavior.

SameBehavior

get stopped(): StoppedBehavior

Sentinel: stop the actor.

StoppedBehavior

get unhandled(): UnhandledBehavior

Sentinel: mark the message as unhandled (goes to dead letters).

UnhandledBehavior

intercept<T>(inner, interceptor): Behavior<T>

Wrap inner so interceptor runs first on every message. The interceptor observes, transforms, or drops — it decides whether inner runs at all:

const b = Behaviors.intercept(inner, (context, message, next) => {
if (isNoise(message)) return Behaviors.same; // drop, inner never sees it
return next(context, transform(message)); // transform + delegate
});

The wrapper survives the inner behavior’s transitions: whatever the interceptor returns becomes the new inner behavior and is re-wrapped, so an inner Behaviors.receive that swaps itself out stays intercepted. The only way out is Behaviors.stopped, where there is nothing left to intercept.

Nesting runs outermost-first — in intercept(intercept(leaf, first), second) it is second that sees the message first, and first only runs if second delegates.

Errors thrown by the interceptor are treated exactly like errors from the inner handler: they reach an enclosing supervise.

User messages only; lifecycle signals go straight to receiveWithSignal’s handler and are not intercepted.

T

Behavior<T>

BehaviorInterceptor<T>

Behavior<T>

logMessages<T>(inner, options?): Behavior<T>

Log every message before inner handles it — debug by default:

const traced = Behaviors.logMessages(inner);
const audited = Behaviors.logMessages(inner, {
level: 'info',
formatter: (message) => `order ${message.orderId}`,
});

The built-in line is received <kind> for the project’s tagged messages, falling back to the class name and then to typeof — a bare object literal has a constructor.name of Object, which would say nothing.

The line is only built when the actor’s logger would actually emit it, so leaving this in place on a system logging at warn costs one comparison per message rather than a formatted string.

T

Behavior<T>

LogMessagesOptions<T> = {}

Behavior<T>

monitor<T>(observer, inner): Behavior<T>

Forward every message to observer before inner handles it — a tap for test probes and audit trails:

const probe = kit.createTestProbe();
const monitored = Behaviors.monitor(probe, inner);

Forward-then-deliver is the deliberate order: the monitor sees a message even if handling it crashes the actor, which is the case you most want a trace of. Delivery to the monitor is fire-and-forget and its failures are swallowed — a broken tap must not take the actor down with it.

T

ActorRef<T>

Behavior<T>

Behavior<T>

receive<T>(handler): ReceiveBehavior<T>

Standard receive — gets both context and message.

T

(context, message) => Behavior<T>

ReceiveBehavior<T>

receiveMessage<T>(handler): ReceiveBehavior<T>

Receive when you don’t need the context — message-only shortcut.

T

(message) => Behavior<T>

ReceiveBehavior<T>

receiveWithSignal<T>(handler, onSignal): ReceiveBehavior<T>

Receive with an additional signal handler.

T

(context, message) => Behavior<T>

(context, signal) => Behavior<T>

ReceiveBehavior<T>

setup<T>(factory): Behavior<T>

Run factory once with the actor’s context; the returned Behavior is the first one the actor adopts. Use this to capture context.self or spawn children in the “constructor”.

T

(context) => Behavior<T>

Behavior<T>

supervise<T>(child): SuperviseBuilder<T>

Wrap a behavior with a supervisor strategy. Any error thrown from the wrapped handler is routed through strategy — the behavior is restarted (reset to its initial form), stopped, resumed, or escalated.

T

Behavior<T>

SuperviseBuilder<T>

withStash<T>(capacity, factory): Behavior<T>

Expose a capacity-bounded stash buffer. The inner behavior can stash user messages (e.g. during init) and call stash.unstashAll() later.

T

number

(stash) => Behavior<T>

Behavior<T>

withTimers<T>(factory): Behavior<T>

Expose the per-actor TimerScheduler to the behavior.

T

(timers) => Behavior<T>

Behavior<T>