Hey, creator of XState here! Just want to mention that we're very close to releasing XState v5 beta, which brings even more features to the state machines & statecharts you can create, and greatly improves the developer experience, but also makes it more usable as a general-purpose state management library (or orchestration "framework" if you want to consider it that), whether on the frontend or backend.
Hello! I’ve looked at XState a bunch of times but always felt it seemed very involved for what it does. A simple “vanilla” state machine isn’t particularly complicated. Are you sure you need to bring “even more features”, or rather, what are your thoughts on how feature-rich a state machine library needs to be?
Xstate: State machines and statecharts for the modern web
61–70 of 81 posts
Re: Xstate: State machines and statecharts for the modern web
#62Earlier quoted context omitted.
“ Any sufficiently complicated model class contains an ad-hoc, informally-specified, bug-ridden, slow implementation of half of a state machine. ” –a former colleague
“I don’t need a state machine library” - everyone when starting a new project
{/* the fact that i'm doing this means I probably should have used x-state or something */}
{stages.filter(Boolean).length > 1 && (
WARNING! More than one assumption is true! {JSON.stringify(stages)}
)}
One day I'll learn...Re: Xstate: State machines and statecharts for the modern web
#63Hey, creator of XState here! Just want to mention that we're very close to releasing XState v5 beta, which brings even more features to the state machines & statecharts you can create, and greatly improves the developer experience, but also makes it more usable as a general-purpose state management library (or orchestration "framework" if you want to consider it that), whether on the frontend or backend.
Re: Xstate: State machines and statecharts for the modern web
#64I'm a simple man. I just want to embed a state machine in my objects. I want to let it know when an event occurs. I want it to let me know when an event occurs. Sigh.. Found xstates desire too control everything made a simple embedding use case really awkward. The hoops to jump through for good typescript support while breaking the config down into reusable components... Really wanted to like xstates but it felt like…
I ended up writing my own in about 80 lines of typescript in less time than it would have taken to finish going through the Xstate docs. Doubtless Xstate is exactly what's needed for some projects, but I suspect it's unnecessarily adding to the complexity of many more.
Re: Xstate: State machines and statecharts for the modern web
#65I used it in the past and really disliked how it encouraged things like defining which functions to call by specifying a string with the function name. I would have much rather had it be more opinionated and require passing a reference to the function so it forces you to write code that benefits from typescript
There's many benefits in having a state machine configuration that can be serialized, especially if you can store that config in a database and load it at runtime. And actually xstate does allow you to pass the functions directly if you want to
The serializability of your state itself has nothing to do with how events are bound to callback functions. If you serialize a string name of a function and not the source code of that function this buys you nothing. The source code itself is all text, so its serializable no matter whether the code is suitable for being statically analyzed
Re: Xstate: State machines and statecharts for the modern web
#66Pros:
- The concept of states, events, and actions are an intuitive way to model state and the learning curve for me wasn't too bad.
- I enjoyed using the visual editor inside VSCode and after setting up TS, the codegen was pretty cool.
- The library supports parallel states.
Cons:
- The codegen was sometimes pretty bad for TS. There was some jank between the editor and the codegen, leading to frustrating DX.
Re: Xstate: State machines and statecharts for the modern web
#67The readme gives the example of a traffic light to demo a state machine usecase. Fair enough, but what's a rudimentary usecase for 'the modern web'? What everyday UI thing is better done with this than other methods? Why is it better?
Example: Even typing into a text box on Hacker News is a new UI state: you have a different set of shortcuts, the keyboard works slightly differently, the browser has to keep track of the input if you suddenly press back and then forward again (some browsers maintain input in these cases), undo has to work within the scope of the textbox etc.
The more complex your UI becomes, the more contradictory states become, and need to be tracked. We're posting something over XHR/WebSockets? X amount of elements on the page have to be disabled, some state has to updated with save progress etc. We are logged in/logged out? We need to show a different state. There's an error in some part of the app? We need to block user from doing something and show errors etc.
If you can split those into actual states and allow interface and actions based on the current state, it becomes easier to reason about what is going on ini your app
Re: Xstate: State machines and statecharts for the modern web
#68StateCharts are the "Strucured Programming" version of State Machines.
StateCharts use parental authority instead of inheritance (my words).
A child state machine cannot override the operation of a parent. A parent can yank children out of their current states back to some known state.
OOP and FP don't encourage this kind of thinking.
The Big Deal concerning state machines is the "state explosion problem". Harel's StateChart notation conquers this problem.
My reading of the original paper is here: https://guitarvydas.github.io/2020/12/09/StateCharts.html
Re: Xstate: State machines and statecharts for the modern web
#69Earlier quoted context omitted.
React expects and recommends you to use side-effects.
This is not my understanding of React - can you elaborate? Its original value prop was one way data flow and pure functional UI based on passed in props. It allows for side effects, to get certain things done pragmatically, but in my view does not encourage them in any way.
Re: Xstate: State machines and statecharts for the modern web
#70The readme gives the example of a traffic light to demo a state machine usecase. Fair enough, but what's a rudimentary usecase for 'the modern web'? What everyday UI thing is better done with this than other methods? Why is it better?