Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All resolver kinds and implementation types should receive all possible event types outside of transitions #4720

Open
Andarist opened this issue Jan 31, 2024 · 3 comments
Assignees

Comments

@Andarist
Copy link
Member

It's only correct to assume that everything can receive just any event type within setup and entry/exit actions, invoke inputs, etc.

Events that are missing are done actor events, done state events, error actor events, xstate.init, xstate.after and probably more

@Andarist Andarist self-assigned this Jan 31, 2024
@rhathas
Copy link

rhathas commented Feb 1, 2024

With Typegen in v4, we had no problems with events in actions. But in v5, we need to narrow event types to events manually (The corrent event types according to the places it has been used were provided by typegen). Well, it's managable.

But missing done/error events is a big problem at the moment. I have no idea how to provide the right event type to an action. Since the variables from done events of invoked services are in 'event.output' instead of 'event' (like it's in transition events), I cant't solve it by using a transition event with the same payload to fake it.

@rmgryan
Copy link

rmgryan commented Feb 1, 2024

I'm curious why there's not a flattened Event object versus different types of events?

For example, why not something like:

type DefaultEvent {
  id: string; // I think what is currently being sent as event.type is actually an identifier
  type: 'default';
  data?: Record<string, any>; // data passed to event from success could be standardized under a key like data
  error?: Record<string, any>; // data passed to event from error could be standardized under a key like error
}

type ErrorEvent {
  ...
  type: 'error';
  ...
}

type Event = DefaultEvent | ErrorEvent; // etc., discriminated union

It seems to me that the type being sent for events that result from onDone, onError, etc. are actually identifiers (i.e. xstate.done.after.index.machine.state) rather than pure types (i.e. "my-event"). A flattened event object would allow me to do something like:

type MyDoneEvent = DoneEvent & { id: 'my-done-event' };

And then from things like actions I could make sure my code is running against the proper event like:

if (event.type !== DoneEvent.type || event.id !== MyDoneEvent.id) {
  return context;
}

@rhathas
Copy link

rhathas commented Mar 1, 2024

@rmgryan I agree that event type on onDone/onError events is more like an identifier. Even a simple type: 'done-event' which is same across all onDone events allows me to do something like:

const results = event.type === 'done-event' ? event.output.data : event.data

To be honest a simple workaround would be great until it's solved throughly (i guess that we will wait typegen to work with v5?).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants