Skip to content

ActorOptions <TLogic>

Index

Properties

optionalclock

clock?: Clock

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

@remarks

You can create your own “clock”. The clock interface is an object with two functions/methods:

  • setTimeout - same arguments as window.setTimeout(fn, timeout)
  • clearTimeout - same arguments as window.clearTimeout(id)

By default, the native setTimeout and clearTimeout functions are used.

For testing, XState provides SimulatedClock.

@see

optionaldevTools

devTools?: boolean | DevToolsAdapter

If true, states and events will be logged to Redux DevTools.

Default: false

optionalid

id?: string

The custom id for referencing this service.

optionalinput

input?: InputFrom<TLogic>

The input data to pass to the actor.

optionalinspect

inspect?: Observer<InspectionEvent> | (inspectionEvent: InspectionEvent) => void

A callback function or observer object which can be used to inspect actor system updates.

@remarks

If a callback function is provided, it can accept an inspection event argument. The types of inspection events that can be observed include:

  • @xstate.actor - An actor ref has been created in the system
  • @xstate.event - An event was sent from a source actor ref to a target actor ref in the system
  • @xstate.snapshot - An actor ref emitted a snapshot due to a received event
@example
import { createMachine } from 'xstate';

const machine = createMachine({
// ...
});

const actor = createActor(machine, {
inspect: (inspectionEvent) => {
if (inspectionEvent.actorRef === actor) {
// This event is for the root actor
}

if (inspectionEvent.type === '@xstate.actor') {
console.log(inspectionEvent.actorRef);
}

if (inspectionEvent.type === '@xstate.event') {
console.log(inspectionEvent.sourceRef);
console.log(inspectionEvent.actorRef);
console.log(inspectionEvent.event);
}

if (inspectionEvent.type === '@xstate.snapshot') {
console.log(inspectionEvent.actorRef);
console.log(inspectionEvent.event);
console.log(inspectionEvent.snapshot);
}
}
});

Alternately, an observer object ({ next?, error?, complete? }) can be provided:

@example
const actor = createActor(machine, {
inspect: {
next: (inspectionEvent) => {
if (inspectionEvent.actorRef === actor) {
// This event is for the root actor
}

if (inspectionEvent.type === '@xstate.actor') {
console.log(inspectionEvent.actorRef);
}

if (inspectionEvent.type === '@xstate.event') {
console.log(inspectionEvent.sourceRef);
console.log(inspectionEvent.actorRef);
console.log(inspectionEvent.event);
}

if (inspectionEvent.type === '@xstate.snapshot') {
console.log(inspectionEvent.actorRef);
console.log(inspectionEvent.event);
console.log(inspectionEvent.snapshot);
}
}
}
});

optionallogger

logger?: (...args: any[]) => void

Type declaration

    • (...args: any[]): void
    • Specifies the logger to be used for log(...) actions. Defaults to the native console.log method.


      Parameters

      • rest...args: any[]

      Returns void

optionalsnapshot

snapshot?: Snapshot<unknown>

Initializes actor logic from a specific persisted internal state.

@remarks

If the state is compatible with the actor logic, when the actor is started it will be at that persisted state. Actions from machine actors will not be re-executed, because they are assumed to have been already executed. However, invocations will be restarted, and spawned actors will be restored recursively.

Can be generated with Actor.getPersistedSnapshot.

@see

optionalsrc

src?: string | AnyActorLogic

The source definition.

optionalstate

state?: Snapshot<unknown>
@deprecated

Use snapshot instead.

optionalsystemId

systemId?: string

The system ID to register this actor under