#How to access contexts in fromPromise() v5 handler?

1 messages · Page 1 of 1 (latest)

old plover
#

In v4 I could simply use an async function as a service, and got the context as a parameter.
No in v5 there is fromPromise but it doesn't receive the context as parameter. How do I access the state of my machine in fromPromise?

neon idol
#

Correct, this is because actors (and other implementation sources) should ideally not be dependent on their parent machine state. Instead, you pass in the relevant data via input:

invoke: {
  src: fromPromise(async ({ input }) => {
    console.log(input.msg); // 'hello'
    // ...
  }),
  input: ({ context, event }) => ({
    msg: context.message
  })
}
old plover
#

Ah that makes a lot of sense thanks! For some reason the argument provided to the input callback is of type any. Is this not properly typed yet, or do we have an issue with our configuration?

#

Also: I can edit this in code, but next time I'll export my state machine, I will lose this input configuration again. Can I configure my input in Stately Studio as well? Or is there a separate location where I can configure the input?

neon idol
#

As far as typing, there is a way to strongly type actor input, but it is a bit boilerplatey right now. We're working on a setup() function that will make this a lot easier. But for now, this is how you'd do it: https://stately.ai/docs/actors#typescript

When you run a state machine, it becomes an actor: a running process that can receive events, send events and change its behavior based on the events it receives, which can cause effects outside of the actor.

calm palm
#

hey @old plover . hope you got the help you needed from David here.
We’re working on a way to help explain Stately better and would love your feedback. By taking 5 minutes, we hope it gets some of your questions answered too. No personal identification will be collected. You can read Maze’s privacy policy here (https://maze.co/privacy-policy/). To start the test, click here (https://t.maze.co/200526303). Thanks!