Skip to content

Abstract Class: AbstractActor<TLogic, TEvent>

API / @xmachines/play-actor / AbstractActor

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

The abstract base class of an actor of the Play Architecture.

It observes the state through the signals, and it works with the tools of the XState ecosystem, such as the devtools and the inspection. It also exposes the reactive signals of the communication with the infrastructure layer.

A subclass IS the actor. Give the logic and its options to super(logic, options), then observe this. A separate actor beside this instance leaves this instance as an empty second actor. Every inherited member then answers from that empty actor, until you forward each one: system, sessionId, clock, the internal _send that receives the traffic of sendTo(), and each member that a later XState version adds.

Extends

Extended by

Type Parameters

Type ParameterDefault typeDescription
TLogic extends AnyActorLogic-The type of the XState actor logic
TEvent extends EventObjectEventObjectThe constraint of the event type. The default is 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>The reactive snapshot of the current actor state. The infrastructure observes this signal, and it reacts to each state change. It therefore holds no coupling to the internal state machine of the actor.-packages/play-actor/src/abstract-actor.ts:207
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:223

Sends an event to the Actor.

The constraint is TEvent, which gives the type safety of a concrete implementation.

A note for an implementation that wraps send, to check the event or to notify a hook around it: this declaration is abstract for one reason only, to narrow the event type, and TypeScript forbids a super call to an abstract member. Reach the implementation of XState with Actor.prototype.send.call(this, event) instead. A concrete declaration permits super.send(), but it also forces an override modifier in every subclass that exists now, and that is a breaking change for an adapter outside this repository.

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;