Skip to content

Actor <TLogic>

An Actor is a running process that can receive events, send events and change its behavior based on the events it receives, which can cause effects outside of the actor. When you run a state machine, it becomes an actor.

Implements

Index

Constructors

constructor

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


    Type parameters

    Parameters

    • logic: TLogic

      The logic to create an actor from

    • optionaloptions: ActorOptions<TLogic>

      Actor options

    Returns Actor<TLogic>

Properties

publicoptional_parent

_parent?: ActorRef<any, any>

publicoptional_syncSnapshot

_syncSnapshot?: boolean

publicclock

clock: Clock

The clock that is responsible for setting and clearing timeouts, such as delayed events and transitions.

publicid

id: string

The unique identifier for this actor relative to its parent.

publiclogic

logic: TLogic

The logic to create an actor from

publicoptions

options: Readonly<ActorOptions<TLogic>>

publicref

ref: ActorRef<SnapshotFrom<TLogic>, EventFromLogic<TLogic>>

publicsessionId

sessionId: string

The globally unique process ID for this invocation.

publicsrc

src: string | AnyActorLogic

publicsystem

system: AnyActorSystem

The system to which this actor belongs.

Methods

public[observable]

publicgetPersistedSnapshot

  • getPersistedSnapshot(): Snapshot<unknown>
  • Obtain the internal state of the actor, which can be persisted.

    @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

    Returns Snapshot<unknown>

publicgetSnapshot

  • Read an actor’s snapshot synchronously.

    @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

    Returns SnapshotFrom<TLogic>

publicsend

  • Sends an event to the running Actor to trigger a transition.


    Parameters

    Returns void

publicstart

  • Starts the Actor from the initial state


    Returns Actor<TLogic>

publicstop

  • Stops the Actor and unsubscribe all listeners.


    Returns Actor<TLogic>

publicsubscribe

  • Subscribe an observer to an actor’s snapshot values.

    @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
    @example
    // Observer as a plain function
    const subscription = actor.subscribe((snapshot) => {
    console.log(snapshot);
    });
    @example
    // 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:

    @example
    const subscription = actor.subscribe((snapshot) => {
    // ...
    });

    // Unsubscribe the observer
    subscription.unsubscribe();

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


    Parameters

    • observer: Observer<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

publictoJSON

  • toJSON(): { id: string; xstate$$type: number }
  • Returns { id: string; xstate$$type: number }

    • id: string
    • xstate$$type: number