xstate
xstate copied to clipboard
`interpret`, `useInterpret` etc should return an Actor
Description
Writing the v5 docs, and it's making me think that interpret should return an Actor-compatible API ONLY. We have a slightly strange terminology when it comes to:
- Actors - in v4, we usually think of actors as something created by
spawn. An actor ref is what's held in context. - Services - again, in v4 we think of them as something created using invoke.
- Interpreters - something that's returned from
interpretoruseInterpret. To confuse things, we sometimes refer to this as a service.
Really, they're all referring to similar things - a running process that can send and receive events. We've had internal discussions to unify this language, but after an internal discussion with @Andarist he raised an interesting point:
What if we just made interpret return an Actor?
Andarist pointed out that the Interpreter type returned from interpret extends an Actor, and it doesn't really have that many other useful things on it. So if we return an Actor from interpret, we can condense the docs and teach less lingo.
Expected result
Not applicable
Actual result
Not applicable
Reproduction
Not applicable
Additional context
No response
Agree with this. What is returned from interpret() should just be an ActorRef instead of something special. It was not done this way in v4 for "legacy"/historical reasons (machine interpretation came before invoke/spawn/etc.).
I agree! Just got bitten by this when I was trying to type a function that took the output of useInterpret as an argument and returns some ActorRef from within it. I tried to use ActorRefFrom but then realised that wasn't the right type. It definitely feels a little convoluted and if it could be simplified that would be awesome!
Resolved by #3455