Actors in XState implement an RxJS-compatible Observable interface. Instead of directly subscribing to the actor, you can use RxJS from()
:
// Create the actor (also works with `fromPromise`, ...)
const machine = setup({ ... }).createMachine({ ... });
const actor = createActor(machine).start();
// Subscribe to it, the normal way
actor.subscribe((snapshot) => console.log(snapshot));
// Subscribe to it using RxJS
from(actor).subscribe((snapshot) => console.log(snapshot));
Now that's not much of a difference. But it gets more powerful when you use operators to transform and filter the snapshot, or combine it with other observables.
from(actor)
.pipe(
startWith(actor.getSnapshot()),
map((snapshot) => snapshot.context.value),
filter((value) => checkValue(value)),
distinctUntilChanged()
)
.subscribe((value) => console.log(value));