Skip to content

Abstract Class: AbstractActor<TLogic, TEvent>

API / @xmachines/play-actor / AbstractActor

Defined in: packages/play-actor/src/abstract-actor.ts:180

Abstract base class for Play Architecture actors.

Provides signal-driven state observation that integrates with XState ecosystem tooling (devtools, inspection) while exposing reactive signals for Infrastructure layer communication.

Extends

Extended by

Type Parameters

Type ParameterDefault typeDescription
TLogic extends AnyActorLogic-XState actor logic type
TEvent extends EventObjectEventObjectEvent type constraint (defaults to EventObject)

Constructors

Constructor

new AbstractActor<TLogic, TEvent>(logic, options?): AbstractActor<TLogic, TEvent>;

Defined in: xstate

Creates a new actor instance for the given logic with the provided options, if any.

Parameters

ParameterTypeDescription
logicTLogicThe logic to create an actor from
options?ActorOptions<TLogic>Actor options

Returns

AbstractActor<TLogic, TEvent>

Inherited from

Actor<TLogic>.constructor

Properties

PropertyModifierTypeDescriptionInherited fromDefined in
_parent?publicAnyActorRef-Actor._parent-
clockpublicClockThe clock that is responsible for setting and clearing timeouts, such as delayed events and transitions.Actor.clock-
idpublicstringThe unique identifier for this actor relative to its parent.Actor.id-
logicpublicTLogic-Actor.logic-
optionspublicReadonly<ActorOptions<TLogic>>-Actor.options-
refpublicActorRef<SnapshotFrom<TLogic>, EventFromLogic<TLogic>, EmittedFrom<TLogic>>-Actor.ref-
sessionIdpublicstringThe globally unique process ID for this invocation.Actor.sessionId-
srcpublic| string | AnyActorLogic-Actor.src-
stateabstractState<unknown>Reactive snapshot of current actor state. Infrastructure observes this signal to react to state changes without directly coupling to the actor’s internal state machine implementation.-packages/play-actor/src/abstract-actor.ts:190
systempublicAnyActorSystemThe system to which this actor belongs.Actor.system-
systemIdpublicstring | undefined-Actor.systemId-

Methods

[observable]()

observable: InteropSubscribable<SnapshotFrom<TLogic>>;

Defined in: xstate

Returns

InteropSubscribable<SnapshotFrom<TLogic>>

Inherited from

Actor.[observable]

getPersistedSnapshot()

getPersistedSnapshot(): Snapshot<unknown>;

Defined in: xstate

Obtain the internal state of the actor, which can be persisted.

Returns

Snapshot<unknown>

Remarks

The internal state can be persisted from any actor, not only machines.

Note that the persisted state is not the same as the snapshot from Actor.getSnapshot. Persisted state represents the internal state of the actor, while snapshots represent the actor’s last emitted value.

Can be restored with ActorOptions.state

See

https://stately.ai/docs/persistence

Inherited from

Actor.getPersistedSnapshot;

getSnapshot()

getSnapshot(): SnapshotFrom<TLogic>;

Defined in: xstate

Read an actor’s snapshot synchronously.

Returns

SnapshotFrom<TLogic>

Remarks

The snapshot represent an actor’s last emitted value.

When an actor receives an event, its internal state may change. An actor may emit a snapshot when a state transition occurs.

Note that some actors, such as callback actors generated with fromCallback, will not emit snapshots.

See

Inherited from

Actor.getSnapshot;

on()

on<TType>(type, handler): Subscription;

Defined in: xstate

Type Parameters

Type Parameter
TType extends any

Parameters

ParameterType
typeTType
handler(emitted) => void

Returns

Subscription

Inherited from

Actor.on;

select()

select<TSelected>(selector, equalityFn?): Readable<TSelected>;

Defined in: xstate

Type Parameters

Type Parameter
TSelected

Parameters

ParameterType
selector(snapshot) => TSelected
equalityFn?(a, b) => boolean

Returns

Readable<TSelected>

Inherited from

Actor.select;

send()

abstract send(event): void;

Defined in: packages/play-actor/src/abstract-actor.ts:197

Send event to Actor.

Constrained to TEvent for type safety in concrete implementations.

Parameters

ParameterType
eventTEvent

Returns

void

Overrides

Actor.send;

start()

start(): this;

Defined in: xstate

Starts the Actor from the initial state

Returns

this

Inherited from

Actor.start;

stop()

stop(): this;

Defined in: xstate

Stops the Actor and unsubscribe all listeners.

Returns

this

Inherited from

Actor.stop;

subscribe()

Call Signature

subscribe(observer): Subscription;

Defined in: xstate

Subscribe an observer to an actor’s snapshot values.

Parameters
ParameterTypeDescription
observerObserver<SnapshotFrom<TLogic>>Either a plain function that receives the latest snapshot, or an observer object whose .next(snapshot) method receives the latest snapshot
Returns

Subscription

Remarks

The observer will receive the actor’s snapshot value when it is emitted. The observer can be:

  • A plain function that receives the latest snapshot, or
  • An observer object whose .next(snapshot) method receives the latest snapshot
Examples
// Observer as a plain function
const subscription = actor.subscribe((snapshot) => {
console.log(snapshot);
});
// Observer as an object
const subscription = actor.subscribe({
next(snapshot) {
console.log(snapshot);
},
error(err) {
// ...
},
complete() {
// ...
},
});

The return value of actor.subscribe(observer) is a subscription object that has an .unsubscribe() method. You can call subscription.unsubscribe() to unsubscribe the observer:

const subscription = actor.subscribe((snapshot) => {
// ...
});
// Unsubscribe the observer
subscription.unsubscribe();

When the actor is stopped, all of its observers will automatically be unsubscribed.

Inherited from
Actor.subscribe;

Call Signature

subscribe(
nextListener?,
errorListener?,
completeListener?): Subscription;

Defined in: xstate

Subscribe an observer to an actor’s snapshot values.

Parameters
ParameterType
nextListener?(snapshot) => void
errorListener?(error) => void
completeListener?() => void
Returns

Subscription

Remarks

The observer will receive the actor’s snapshot value when it is emitted. The observer can be:

  • A plain function that receives the latest snapshot, or
  • An observer object whose .next(snapshot) method receives the latest snapshot
Examples
// Observer as a plain function
const subscription = actor.subscribe((snapshot) => {
console.log(snapshot);
});
// Observer as an object
const subscription = actor.subscribe({
next(snapshot) {
console.log(snapshot);
},
error(err) {
// ...
},
complete() {
// ...
},
});

The return value of actor.subscribe(observer) is a subscription object that has an .unsubscribe() method. You can call subscription.unsubscribe() to unsubscribe the observer:

const subscription = actor.subscribe((snapshot) => {
// ...
});
// Unsubscribe the observer
subscription.unsubscribe();

When the actor is stopped, all of its observers will automatically be unsubscribed.

Inherited from
Actor.subscribe;

toJSON()

toJSON(): object;

Defined in: xstate

Returns

object

NameTypeDefined in
idstring-
xstate$$typenumber-

Inherited from

Actor.toJSON;