Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

91
Views
How to invoke a machine with context of the machine that invokes that machine?

Firstly, here’s the code. I need to fix.


Basically, there are two machines:

  • currentStateMachine:
    • currently, I want to change the state manually (running send commands) once a second based on the real state;
    • later, it might be better to run a callback function based on which the state would change, however, currently I have no idea if it could work like that;
    • I use context to store the current timestamp and the state;
    • PROBLEM: how to invoke intervalsMachine with the context of currentStateMachine?
    • QUESTION: will currentStateMachine wait for invoke completion before accepting another state event? Or will it wait until the state is processed and only after that a new event is processed?
  • intervalsMachine:
    • it creates an interval from the current states whenever a new state is received;
    • it uses context to store startTime of the interval, its length and the state during this interval;
    • the interval calculation is simplified for demostration, in will be much more complex.

TL;DR

  1. How to invoke intervalsMachine (invoked machine) with the context of currentStateMachine (machine that invokes intervalsMachine)?

  2. Will currentStateMachine wait for invoke completion before accepting another state event? Or will it wait until the state is processed and only after that a new event is processed?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use the data property with invoke to specify context to be taken from the parent.

const timerMachine = createMachine({
  id: 'timer',
  context: {
    duration: 1000 // default duration
  }
  /* ... */
});

const parentMachine = createMachine({
  id: 'parent',
  initial: 'active',
  context: {
    customDuration: 3000
  },
  states: {
    active: {
      invoke: {
        id: 'timer',
        src: timerMachine,
        // Deriving child context from parent context
        data: {
          duration: (context, event) => context.customDuration
        }
      }
    }
  }
});

https://xstate.js.org/docs/guides/communication.html#invoking-with-context

Another option is you can use the (ctx, evt) => ... pattern for the src option.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!