[V5] Event Schemas #1241
Replies: 6 comments 8 replies
-
This looks amazing and a great addition for complex systems. Anything that improves the visualizer is a huge win. |
Beta Was this translation helpful? Give feedback.
-
Looks great! How does it compare to the event "schema" we define in TypeScript with the discriminatory type. Will they be used together or will the JSON-based event schema replace the need to define distinct types in TypeScript? Alternatively, could the visualizer not read a TypeScript-based event schema which most of us are already defining? Posting multiple comments so we have an easy time replying. |
Beta Was this translation helpful? Give feedback.
-
What happens when the machine receives an event not defined in the schema. Should it log a warning, ignore the event silently or throw an error as would be the case with strictMode: true? Possibly an important question to answer before moving forward with the strictMode depreciation which I personally have never used and would almost consider an anti-pattern for creating reusable machines. |
Beta Was this translation helpful? Give feedback.
-
Tangible related and very interesting to me would be to define a schema for other machines. Essentially when spawning or invoking a machine, I'd love to be able to force the machine who is spawning a child to implement an arbitrary number of events that will be emitted from child to parent (using Just to give an example, I may have a UserPickerDropdownMachine and component. It may fetch users, display them and allow to select one or multiple users. As it doesn't perform any additional business logic when users are picked, it always wants to send an update to its parent machine once a user has been selected. I'm looking for something like a contract that enforces the |
Beta Was this translation helpful? Give feedback.
-
Personally, I started using What surprised me the most that This is a really handy tool for typos. (Please, don't deprecate it) Now imagine I would have to instead define |
Beta Was this translation helpful? Give feedback.
-
I don't feel like this would be that much helpful feature - probably only used by a super small fraction of users.
That being said there are of course benefits for being able to consume such a schema in a nice way - like the enhanced visualizer. The majority of XState audience are JS developers and I feel like all of the mentioned drawbacks of the JSON schema approach are actually solved by TypeScript. If we can easily convert TS type to a JSON schema is another problem but actually it might not be that hard - I would expect that a simple VScode plugin could be implemented to add a context menu item like "Copy as JSON schema" if only the given type is not using crazy stuff like mapped typed etc the conversion should be fairly trivial. Sure - it wouldn't work for other IDEs out of the box, but one could always also write this manually (which is actually the only way to do it proposed in the original post). One could paste in such a generated schema into a dedicated input in the visualizer. We could also add a new API that could be used for those that want a runtime validation - something like Alternatively, in the future, the visualizer could be enhanced with TS support and could potentially be able to convert types from the source code into such schema so one would only have to paste-in their machine code (which would be better as would only require a single action from a user). Even though this would not be a lightweight solution I don't think this is a particular problem - we can expand visualizer with complex capabilities and treat it like a full-blown app. With apps like CodeSandbox, Excalidraw, TS & Babel REPLs etc I wouldn't be worried about bundlesize/startup times. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Event schemas are an important feature for event-driven systems, and XState should provide the ability to define event schemas in the machine so that they can be declaratively read and used (within and outside of a TypeScript context).
Some prior art:
Proposed Plan:
events
(name TBD) property to the machine to represent all of the events that the machine accepts as a subset of JSON Schema:xstate/schema
(name TBD):machine.events
so that it returns a mapping of those event schema objects:One of the biggest benefits of this will be in the visualizer (xstate.js.org/viz), since clicking an event can make a popover appear that will present a small form to fill in the required values for the given event type, or even generate sample events automatically!
Any thoughts on this are welcome, feel free to share.
EDIT: Changed
eventSchema
toevents
sincemachine.events
already returns an array of accepted events and can be repurposed to return the event schemas for those events, which is more useful.Beta Was this translation helpful? Give feedback.
All reactions