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

XState state.exit.actions & micro transitions will be executed in the next state #4

Open
huan opened this issue Apr 18, 2022 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@huan
Copy link
Owner

huan commented Apr 18, 2022

If the action is sensitive to the current state, then a workaround will be needed.

For example:

createMachine({
  states: {
    idle: {},
    busy: {
      always: {
        10: 'idle',
      },
      exit: {
        actions: [
          Mailbox.actions.send(...), // <- this will be executed with state `idle`
        ],
      },
    },
  },
})

In the above code, the Mailbox.actions.send(...) will be executed within the state idle, instead of in the state busy.

Related discussion:

@huan huan added the documentation Improvements or additions to documentation label Apr 18, 2022
@huan
Copy link
Owner Author

huan commented Apr 24, 2022

Due to the above behavior, I have to react to the ACTOR_REPLY event in both idle and busy states:

mailbox/src/impls/wrap.ts

Lines 159 to 176 in 50a2ca5

[duck.Type.ACTOR_REPLY]: {
actions: [
actions.log<context.Context, duck.Event['ACTOR_REPLY']>(
(ctx, e, { _event }) => [
'states.Idle.on.ACTOR_REPLY [',
e.payload.message.type,
']@',
_event.origin,
' -> [',
context.request.type(ctx),
']@',
context.request.address(ctx),
].join(''),
MAILBOX_ADDRESS_NAME,
),
context.sendChildResponse(MAILBOX_ADDRESS_NAME),
],
},

That's because if an actor(child) machine calls the Mailbox.actions.reply(e) in an exit entry and then transitions to idle, then the ACTOR_REPLY event will be sent to the Mailbox in its idle state (instead of busy)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant