Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: SendTo doesn't see local actors when combined with "target" transition #5051

Open
niba opened this issue Aug 20, 2024 · 1 comment
Open
Labels

Comments

@niba
Copy link

niba commented Aug 20, 2024

XState version

XState version 5

Description

When combining the target property in your event with the sendTo action, you're unable to send an event to your local state actor.

On my machine, I want to first clear all of my callbacks/subscriptions and then transition to another state. However, when I try to achieve this, I get the following error: Unable to send event to actor 'ACTOR_NAME' from machine 'MACHINE_NAME'.

Here is the section of my machine

   recording: {
        invoke: {
          id: "listeningOnEvents",
          src: "listenOnSteps",
        },
       on: {
          STOP: {
            target: "stoping",
            actions: sendTo("listeningOnEvents", { type: "STOP" }),
         },
    }}

Xstate searches for actors in children array which is empty (it is only empty when event has target property)
targetActorRef = snapshot.children[resolvedTarget.slice(2)];

As a workaround, I can use the raise action to trigger another event that only performs the state transition. Why doesn't the original approach work?

Expected result

My event can communicate with a local actor

Actual result

SendTo doesn't see a local actor

Reproduction

https://stackblitz.com/edit/github-ntq4he-jy7jad?file=src%2FfeedbackMachine.ts

Additional context

No response

@niba niba added the bug label Aug 20, 2024
@Andarist
Copy link
Member

Andarist commented Sep 6, 2024

The execution order of actions is roughly always this:

[...exitActions, ...transitionActions, ...entryActions]

And in this example, a transition action tries to send an event to an actor that was just stopped by an exit action.

@Andarist Andarist added question and removed bug labels Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants