Search code examples
typescriptstate-machinexstate

how in XState machine to do action send('EVENT_NAME', {to:'something from context'})?


I need send event to another spawned state machine that its ID I have as a string in a variable in the context. (it is not parent state machine and not child)

Like

context.sendTo = 'B_id'

how to do send() with parameter from context?

and how to put send('MY_EVENT_NAME', {to: <something from context> }) in the MachineOptions actions section?


// this does not work...
const myMachineOptions:Partial<MachineOptions<any,any>> = 
{
  actions:{
     mySend: (context, event)=>send('MY_EVENT_NAME', {to: context.sendTo })
  }
}

P.S.

it like in the Pass values when sending events from one machine to another in xState

but what I need to by dynamic is not the message body but the to: part


Solution

  • following the help from the developers of the XState https://github.com/davidkpiano/xstate/issues/1110

    actions {
        actionSendMyEvent: 
                 send('MY_EVENT_NAME', { to: context => context.sendTo })
        }
    

    Huge thanks to the XState developers and maintainers!!!