xstate icon indicating copy to clipboard operation
xstate copied to clipboard

`interpret`, `useInterpret` etc should return an Actor

Open mattpocock opened this issue 3 years ago • 2 comments

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:

  1. Actors - in v4, we usually think of actors as something created by spawn. An actor ref is what's held in context.
  2. Services - again, in v4 we think of them as something created using invoke.
  3. Interpreters - something that's returned from interpret or useInterpret. 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

mattpocock avatar Apr 05 '22 13:04 mattpocock

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.).

davidkpiano avatar Apr 05 '22 15:04 davidkpiano

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!

johtso avatar May 02 '22 21:05 johtso

Resolved by #3455

davidkpiano avatar Feb 13 '23 01:02 davidkpiano