chantastic.fm

5: Finite State Machines with David Khourshid

Episode 5 · April 3, 2018 · 00:36:36

Hosted by Michael Jackson · With David Khourshid

Download MP3 · Jump to transcript

In this episode Michael Jackson talks with David Khourshid about State Machines. David is a developer on the Visual Studio Live Share team at Microsoft. Recently, he's been exploring methods of using finite state machines together with React to create predictable flows through applications that are easy to follow and test.

Featuring

Links

Transcript

[00:00:06] Michael Jackson: This episode of the React Podcast is brought to you by reacttraining.com. In-person, hands-on training for development teams from React community leaders and experts. Visit reacttraining.com to learn more. Hello, everybody, and welcome back to the React Podcast. I am your host, Michael Jackson, and I have a very special guest with me today, David K. Piano.

[00:00:35] David Khourshid: Hey, glad to be here.

[00:00:36] Michael Jackson: And, uh, you know, I, I, uh, I actually forgot to ask you before we started, how do you- how exactly do you pronounce your last name, David?

[00:00:45] David Khourshid: So my last name is pronounced Khourshid, or at least I think it is. Yeah, got

[00:00:50] Michael Jackson: it. And I take it you're a piano player as well?

[00:00:52] David Khourshid: I do, yeah, I do play piano.

[00:00:54] Michael Jackson: Nice. Uh, yeah, I mean, you would have to, I mean, if to put it in your, in your screen name, right? Have you been playing for a while?

[00:01:02] David Khourshid: Yeah, I've been playing for over 20 years. You'd be surprised how many people think that Piano is my last name just because it's on my eBay.

[00:01:09] Michael Jackson: Yeah, it's- so you're probably pretty good at it. Was that like- was that like- did you study it in school or anything?

[00:01:16] David Khourshid: Uh, yeah, it was actually my, uh, my major in college, and then only, only at the very last year did I decide that I would rather do web development as a career than be a musician? Because I mean, they make a little bit more money, so.

[00:01:31] Michael Jackson: Oh yeah. Well, I mean, unless you're like, you know, Elton John. You could have you could have you could have been Elton John.

[00:01:39] David Khourshid: Yeah.

[00:01:41] Michael Jackson: So uh so that's awesome. I you know, it's funny how many uh musicians I meet uh in in programming and in development in general. I'm I'm actually a musician. I sing and I play the guitar and I like played with a band and stuff in high school and stuff. But but anyway. Um, well, that's cool, man. That's that's that's really cool. Um, so uh so David is the— um, so I first met David, I should probably tell people. I first met David last year at the React Rally conference in Salt Lake City, Utah. And I met him in the, uh I guess you could say, kind of backstage, although it was more like just a room off to the side of the stage. Um, and uh it. David was also giving a talk that year uh about uh finite state machines in React. And it was a topic that for me was super interesting because I've done some work with uh state machines in the past. You've probably uh done some work with state machines if you uh if you have any kind of uh you know formal uh training in programming. If not, uh hopefully today we'll we'll be able to talk about a little bit of that too and and kind of help you understand what they are and what they're useful for. Um but first of all, I just wanted to ask David if he could tell us a little bit bit um about himself and and maybe David, you could tell us a little bit about too, like what you do um day to day at Microsoft.

[00:03:04] David Khourshid: Sure. So like you said, I work at Microsoft. I'm a software developer, about 6 years of experience. I feel like I'm in a job interview.

[00:03:12] Michael Jackson: Can you solve this uh hard logic puzzle for me? No, I'm just-

[00:03:18] David Khourshid: Well, um right now I'm working on the VS Live Share team. Which is a way of real-time collaboration with developers, whether you're using Visual Studio or VS Code. You could pretty much code from the comforts of your own editor from anywhere in the world and even do things like sharing debug sessions and shared live servers and all of that.

[00:03:43] Michael Jackson: Oh, nice. So so you're working so VS Code is uh it's I I didn't realize, is VS Code online? Like is it like does it run in a browser? I mean, I know it runs on Electron, but-

[00:03:55] David Khourshid: So no, and-

[00:03:57] Michael Jackson: I probably misinterpreted what you just said. It's it's you're you're working on like this live mode for VS Code, right, that allows you to share, but it's not in a browser. It's just maybe right there in the actual VS Code editor, right?

[00:04:09] David Khourshid: Right, exactly. So I let's say I have a problem on a project. I would start a share session with you and I would send you a uh a share session link and you would click that. And if we both have, you know, the the Live Share extension installed, you could see my project from your own editor. You could edit files, rename files, um start a debug session, all of that.

[00:04:33] Michael Jackson: Nice, nice. So you send me a link and then it opens in my editor.

[00:04:36] David Khourshid: Exactly.

[00:04:37] Michael Jackson: Sweet, sweet. Um and so so VS Code is uh is actually built, uh as far as I understand, it's an Electron app, right? And so it's built using a lot of web technologies. Um, so how long have you been uh working with the the VS Code team?

[00:04:54] David Khourshid: So we don't work directly with the VS Code team. Uh, we work alongside them. Uh, okay. The the Live Share team is really its own team. And actually, I've only started working with this team uh since the last time we talked. So about 6 months.

[00:05:10] Michael Jackson: Okay, cool. Cool. Well, that sounds like a that sounds like a pretty awesome uh gig. Also, what you just described sounds incredibly difficult. It's one thing for me to just pop open my editor and start typing. Uh, it's another thing entirely once, you know, we're trying to synchronize changes or or share some kind of, uh, you know, some kind of like you were talking about a debugging session. I mean, um, I I I I remember writing reading a white paper once from Google about um Google Wave. Do you remember Google Wave? That was like kind of that thing where they they had like shared document editing in Google Wave. But anyway, they they described this like operational transform over the document, right? So if one person is typing some characters and then you start to like delete those same characters, like how would you resolve these those kinds of conflicts? Are these the kinds of problems that you're that you're kind of grappling with when you're working on Live Share?

[00:06:09] David Khourshid: Right, so in Live Share, we do use the operational transform algorithm. Thankfully, all that legwork was already pretty much established and done by the time that I got there. However, with that said, there's still a lot of moving parts in uh VS Code and Visual Studio for Live Share, because it's not just syncing uh collaborative edits, it's also syncing IntelliSense, like if you're using C# sharp or TypeScript, and we plan on supporting more languages in the future. And also, as you said, debug sessions, local servers, things like that.

[00:06:45] Michael Jackson: Got it, got it. And so you got up last year at React Rally and you gave a talk about, uh, let's see, it was entitled uh Deterministic Finite Automata, in uh in in and you and you talked about how to apply this specifically to React. I imagine that Um, maybe some of that work is uh is useful in the in the work that you're doing today, right? Uh, you know, when when there's lots of sort of, you know, uh actions and things coming in, things happening concurrently, lots of moving pieces, it probably uh helps a lot to have a machine where you can, you know, reliably predict, you know, given the state that we're in, there's some new input, uh where are we going from here? Is that does it does it relate to kind of the the work that you do day to day?

[00:07:40] David Khourshid: Oh, yeah, absolutely. So, since I started the uh started joining the team right after React Rally, basically. Well, when I started working on the project, I noticed that there were a lot of issues based around state where let's say that you know you were trying to log in and then you were trying to share a session and at the same time you were like, actually, I want to log out. And then you you get this sort of weird state where eventually the session will be shared, but you're not logged in anymore. So, you know, how do you resolve that? And there were a whole bunch of both UI and, you know, just internal business logic issues around that. And so I took a step back and I I um I drew out a state machine just to model all of the complex, like, sign-in, login, sharing, joining, debugging behavior. And I I decided to apply what I practiced, basically dogfooding, and make it an explicit state machine. And we saw an entire class of UI problems just disappear because we explicitly modeled the state machine and we knew that there were certain impossible states that we could never reach. And previously, those impossible states were bugs. And on top of that, uh, I was able to add, you know, 2 lines of code to add telemetry for those transitions from state to state. And we we got some pretty nice looking graphs on just um like what paths are most commonly took with users using the actual app. And so from there, we get really valuable insights such as Oh, a lot of users are seeming to hit this error and they're hitting this error when they travel this path, like when they log in externally and they do this and they do that.

[00:09:37] Michael Jackson: Interesting.

[00:09:38] David Khourshid: Yeah. And I I talked about this at uh JSConf a couple weeks ago too.

[00:09:44] Michael Jackson: Uh, was that the one in, um, sorry, in Australia?

[00:09:48] David Khourshid: Iceland, actually.

[00:09:49] Michael Jackson: Oh, oh, Iceland. That's of course, yeah, of course. Right. I forgot. Yeah, I forgot about that one. How could I forget? Actually, one of the most recent guests that I interviewed was Dan, just that week that he was actually going to Iceland. Awesome. So, um, so, let's, let's, I wanted to back up just just a little bit for the sake of maybe some of the listeners who haven't ever actually worked with um state machines before. Maybe, you know, in a in a Kind of like a very sort of succinct way, could you sort of sum up what what you mean when you're talking about you know a state machine?

[00:10:30] David Khourshid: Sure. So the first thing to realize is that you are already writing state machines. Every single developer listening to this podcast, every single developer doing any sort of development, they're already creating state machines. It's It's just not very well-defined. So, uh, the, I guess the the Hello World example of a state machine is a stoplight, but I don't want to give that example. It's a little bit too contrived. So instead, uh, let's talk about a promise. When you make a promise, at first, no promises made. So, you know, you're just idle. You're waiting for a promise to be made. And then when you make that promise, like let's say you're fetching something from an API, uh you go into this pending state. You're waiting for the promise to resolve or reject. So you did an action, which is starting that promise, and now you're pending. And now you could either have it resolve or reject. And if it resolves, you get into this fulfilled state. If it rejects, you get into this uh failure states or error state. I forget which one it is. So basically, what I described was a series of states, which is a finite number of states, which is you're either idle, pending, fulfilled, or rejected, and a series of actions, which is executing the promise, having the promise succeed, or having the promise fail.

[00:12:03] Michael Jackson: I like what you had to say about uh you know people are already doing this, right? I mean, if you think about it. You know, in your uh in your Node.js callback, when you say, if there was an error, uh you know, take one branch, you know, maybe log it or throw it or do whatever you want, call the callback with the error, otherwise do something else, right? So we are, you know, we are embedding these these state machines or these pieces of uh these pieces of logic, these decisions basically on how we go from one state to the next. Um, all over the place, right? It's all over the place in our code. Uh, and we're deciding, okay, if, you know, like you say, if there was a, if, you know, if there was a failure, we are now going to run some sort of failure or error handling code. Otherwise, if there was success, um, you know, we're gonna handle the success case. It, it kind of reminds me as well of, um, you know, you use the example of of a promise. Um. In in uh the Flux pattern, for those people who might be uh familiar with Flux, it was kind of a thing for a while in in the React uh ecosystem. But you know, if you if you had some sort of asynchronous behavior in in Flux, whether it was modeled with uh a promise or a callback, you know, some you're making like some network request or whatever, um you would usually have, you know, one action that's like, okay, here's now we're getting started with this, we're going to fetch some data. And then at least 2 actions to represent uh what happens when you get back. So either here's the data and we succeeded in the request, or the request failed and and we didn't succeed and here's the error, you know. And so we're, you know, we have- this is very, very common. This is a very, very common thing that we've got already uh scattered all throughout our code, and yet most of the time it's not sort of written out and very sort of structured, right? It's just sort of hidden and tucked away in these, you know, like I said, in these if statements or or maybe, you know, switches or something. It's but it's stuff that we're doing all the time already, right?

[00:14:11] David Khourshid: Right, exactly. The the way I like to think about it is that all of the business logic exists in the developer's head. And the problem with that, especially now that I'm working on collaborative software, is when you're working with another developer, They might not know exactly what's in your head. They can't read your mind. And just reading the code, they're trying to decipher what your intentions are, what the intended business logic is. And that's less than ideal. However, that's the way that many, many developers, the majority of developers are developing nowadays. Why? Because it's easy. It's the most straightforward path. And as soon as you talk to a developer about how about taking a little bit more uh legwork to actually model this properly, they say, "Why? It's easier to just use an if statement or a switch statement." And so that's when I say, "Here, let's visualize your app. Try doing that with the code that you wrote."

[00:15:13] Michael Jackson: Yeah, yeah, yeah. Let's I I want to talk about that that visualization next. Um I just wanted to say real quickly is that um this actually feels a lot like what React itself does for me and for my apps, um as far As far as state goes, right, because um before I started using React, you know, maybe I was building an app with uh you know jQuery or whatever I was using before, I would have little bits of state in my app, but they were just variables, right. And but if I ever asked myself like, okay, what is all of the state in the app, uh it wasn't ever you know very obvious to me until I collected them all into these objects where I said, okay, this is the state. This is the state of this component right now, right? And and these setState calls uh model you know changes to those states. So in in in some ways, uh what you're describing to me is like the logical next step. You know, now that we know sort of what our state is, um and and again, talking specifically about React, I think it makes sense to- You know, uh, have a have a structured way to say, okay, what is the business logic that dictates changes to that state? You know, let's get it out of your head just so that it doesn't— it you're not the only person that knows about this, and and let's write it down so that uh so that we can all discuss it and so we can all see it and we can and as you were as you were saying, you know, we can we can actually visualize it. Now, you've actually built a a visualization tool. Uh for for uh for state machines.

[00:16:58] David Khourshid: Yeah, I did. And I'm actually coming out with a new version soon. Um and and the way it works is that we have a declarative representation of the state machine. So I'm just using a JSON object. Um and so this is my project XState. That's what's uh that that's what's being used to model this. And uh funny enough, there's a specification called SCXML. Which is, of course, extremely declarative because it's XML, which describes these uh state machines and state charts. And this is a very old specification. I think it's more than 10 years old. However, it's it's essentially done and it's being used in other fields of technology.

[00:17:44] Michael Jackson: Interesting. So SC, I assume, stands for state charts. So uh so SCXML would be a way to describe a state chart or or a state machine in uh in XML. I I you know I bet we could just build a parser for that uh and boil it down to uh an uh an XState JSON object.

[00:18:04] David Khourshid: Yeah, that's actually— I'm working on that right now. And so that's gonna be coming out soon.

[00:18:08] Michael Jackson: Nice, nice. I look forward to seeing that. Um and so right now, how do you right now, how do you do it? If you want to get started with uh with kind of uh you know With XState or with with these visualizations. Um I by the way, I have to say, uh I was a huge fan of the approach where you're just like, this is just uh this is just a JSON object, you know, and uh and and I I thought it was hilarious actually when you when you said in your React Rally talk, you're like, hey, you know, if you want a magical finite state machine library thing, take this function right here. And it was uh it was literally just uh you know a function that selects um based on the— there were let's see, it was nested two levels deep, your object, right? The top level is uh something about like the current state, and then the next level is all of the actions, the possible actions in that current state. Is that right?

[00:19:03] David Khourshid: Yeah, that's right. It's just a sort of a dictionary, a mapping between um a state, an action, and the next state based on, you know, the current state and the action.

[00:19:13] Michael Jackson: Nice. And and then you you had this function and you said, look, here's this function. If I minify it, it's uh super small. Don't, you know, I think it was like 22 bytes or something. And you said, don't gzip it or else it'll be 44 bytes. It'll just be bigger. I I love that.

[00:19:29] David Khourshid: XState's considerably bigger than that, of course. And that's because it's dealing with state charts where there's Uh, there it's nested state machine, so there's a little bit more to the algorithm, um, but that's sort of besides the point. Uh, as far as getting started with XState and state machines in general, um, I always tell people that, you know, don't don't worry about like researching about state machines. Like just take baby steps. And I I posted some of these on Twitter. One of them is for example, using enum statements or enum expressions instead of having something like isLoading equals false, isError equals false, is, you know, those boolean flags. Because uh those boolean flags um are really not representative of my app could only be in one of these states at any given time.

[00:20:27] Michael Jackson: What if two of them are true? Now which state are you in? It's not entirely clear. It depends on which if statement comes first.

[00:20:35] David Khourshid: Exactly. And and the thing is that developers who love using if statements will tell me, oh, I I'm gonna keep using boolean flags because if I have isLoading true and then I have isSuccess true, I could just make sure that isLoading is set to false. And I tell them that that's a perfect example of having all of the business logic inside your head. The if statement doesn't directly tell me that it could only be in isLoading or isSuccess, so you have to do it manually.

[00:21:06] Michael Jackson: Now, in your talk, you you talked about two different kinds of states, um, and I believe you were talking about XState when you said this. You said there are hierarchical states and concurrent states. Um, could you maybe describe a little bit more what you meant by those, by or by that distinction?

[00:21:25] David Khourshid: Sure. So. Actually, let's use routing as an example. I know you worked on React Router.

[00:21:30] Michael Jackson: Yeah, for sure.

[00:21:31] David Khourshid: And routing is also a great way to think about state machines because the way you configure your app, you could only be in one route at a time. But of course, with React Router, you could have nested routes. So let's say you're on the users page, but you're going to a specific user, like users/michael. Then You're both in the user's state, which is the parent state, but you're also in the Michael state, which is the child state. And so that's where the idea behind hierarchical finite state machines or state charts. Now, um, let's say you have two panels. Let's say you have a users panel and on the right side you have a, uh, I don't know, a a workshops panel, you know, where you're managing all of the workshops. Then you're in both of those states at the same time. You're in both the user state and the workshop state. And so that's what's meant by parallel states.

[00:22:31] Michael Jackson: Got it. So that's that's just another that's just another state machine over there, right? That's just two state machines running on the same page, right?

[00:22:37] David Khourshid: Right, exactly. Another example is, let's say you have a little toolbar where you could make something bold or italic or underline or maybe a combination of those. And trying to model that as a state machine, of course you could do, but you're going to have arrows going all over the place in an explosion of different transitions. And so that's what state charts try to solve.

[00:23:01] Michael Jackson: Got it.

[00:23:02] David Khourshid: Just modeling that so that you could have all of those at the same time.

[00:23:06] Michael Jackson: Got it, got it. Um, so is so let's talk a little bit about kind of some of the benefits uh as you've seen them that kind of Um, I mean, I I suppose we've touched on on them already, but maybe some of some more of the kind of um, you know, concrete benefits when it comes to like, you know, testing your apps and and being able to like sleep easy at night because you're because you know that you know you've modeled everything uh with the state machine. Is it possible, for example, to uh to build, you know, a function where you could essentially prove It seems like it it seems like it should be, right? I I mean, uh people who have— I I've never actually done any any um Haskell, but I know that uh for people who have, that's kind of one of the things that they really get excited about is like, hey, look, I can prove my Haskell code. You know, this is this is mathematically sound code. And it seems like we could kind of get there if we had a a state machine where we could sort of map out all the states, like we could write a pretty good test suite and say, look, if these are all the states and these are all the actions for all the different states, uh we could kind of kind of automate some of that, right? Couldn't we uh automate uh you know transitions between states and ensure that we actually do end up in the states that we that we think we should be going to when we get these certain inputs?

[00:24:39] David Khourshid: Yeah, absolutely. So uh what you were talking about at first gets more into the area of formal methods and formal verification, which is something that number one has existed for decades and number two, I really hope one day finds its way into um the frontend JavaScript community just because it's an amazingly powerful thing and it's the next step after all of our quote unquote testing innovations like snapshot testing, end-to-end testing, all of that stuff is going beyond, "Oh, hey, I wrote 1,000 tests," and saying like, "Here is a model of my app and I could 100% prove to you that it has no bugs," which is pretty awesome.

[00:25:27] Michael Jackson: That sounds pretty awesome, yeah. I don't know if I uh I I I I'm not a sort of classically formally trained developer myself. I just kind of I'm I'm kind of a self-taught programmer, but it uh It sounds pretty awesome. Have you have you done any sort of uh have you done any of that kind of thing before, that kind of um uh verif formal verification?

[00:25:50] David Khourshid: Not yet, but it's something that I've definitely wanted to play with. I've been looking at uh something called TLA+, which is one of the more popular uh methods of formal verification. And of course, there's numerous others. Um and in fact, um people have written research papers on using these statecharts as models for formal verification.

[00:26:10] Michael Jackson: Got it.

[00:26:10] David Khourshid: But what what I have been doing is, um, if I have an app modeled as a uh statechart, and I I've actually been doing this in Live Share and a couple of other projects, uh I I would use that exact same statechart that I used to code uh some part of my app, and I would use that to generate all of the possible states and transitions uh that could be taken for that app.

[00:26:38] Michael Jackson: Ah, okay.

[00:26:38] David Khourshid: And so, yeah, so so I add a few heuristics onto that. Like, for example, if I'm in the home state, then the title says home. Just really simple tests like that. And based on that, I could automate the generation of integration tests, you know, where you're testing from uh from starting the app to some end state. And um I I found that it it works really well. And so my heuristics are my assumptions of like how the app uh should look at any given state and um you know all the actions I take. And um I I I would generate like 60 or 70 tests like for some you know sizable state chart. And I would just ask myself, like, do I really want to be hand coding all these tests?

[00:27:30] Michael Jackson: No, no, no. I mean, it seems like a logical next step, right? I mean, now that we've now that we've sort of gotten the business logic out of the developer's head and into a into a JSON object that where he's we're you know declaring it essentially right there in the code, seems like a logical next step where we could you know iterate over that object and and yeah, as you said, generate generate tests for all the different inputs. It's fascinating.

[00:27:55] David Khourshid: Yeah. And there's actually a project, not even written by me, but by uh Michael Bertoli. Uh he he works on um React at Facebook in London, I believe.

[00:28:06] Michael Jackson: Oh yeah, yeah.

[00:28:06] David Khourshid: Right. And so he he wrote this uh library called React Automata, where you take um you take state charts, which are written using um my library XState. And you apply them to a component. So these are just normal components. They could be whatever you want them to be. And then um you could just iterate through all the states just by- there's this one method called test state chart. And you pass in the state chart and your components and it just writes all the tests for you. That's awesome. It's not doing like full testing quite yet. Like you know of executing every single possible action and all of that. But it's generating just a ton of snapshot tests where you don't have to manually generate them. And so it's a really cool project and I encourage everyone to check it out.

[00:28:59] Michael Jackson: Nice. So you said React Automata.

[00:29:03] David Khourshid: Cool.

[00:29:03] Michael Jackson: So um uh yeah, I was I was actually gonna ask you about that next about you know it this sounds really, really cool, right? XState sounds really cool. Is there like a React XState sort of library, or is that it? Is is is is React Automata just for testing, or is it actually for something you know something that I can use to say, okay, here's my here's my XState JSON object, um here are the components that I want to render in the different states, um build my app? You know, is is that does that something that that exists, or is that something that that you're interested in, or is there some sort of reason why that's not a great idea?

[00:29:47] David Khourshid: So uh React Automata lets you build these components using these statecharts. Uh as far as like a React-specific XState, um I I I like to think of using XState as sort of a frameworkless thing. It doesn't matter if you're using Angular, Vue, React, whatever. It's it's similar to RxJS. I love using RxJS in React, and I I don't really like just pulling libraries that automatically do certain bindings, just because I like to think of RxJS as its own thing that supplements React and lets me use it a lot better. Uh, with that said, I definitely want to be working with Michael on React Automata and seeing how we could integrate XState and uh make it, you know, do more robust testing and even automatic visualizations. There was someone who actually did a uh or started a project where they integrated XState with Storybook, and that way you could take your React components, give it a state chart, and have it automatically render the components in every single possible state, which I'm sure developers who use Storybook who are maybe listening right now, they know that they have to do every single one of those manually right now, which sucks.

[00:31:05] Michael Jackson: You know, we we've talked a lot about state. We've talked a lot about transitioning between states. I know that some people listening are probably going to think, oh, um, maybe maybe this is like the new Redux or the new MobX or something like that. Um, could you talk maybe a little bit about- so if I if I have an app that I've built with, you know, Redux already, um, doesn't mean I can't throw a couple of couple of state machines in there and kind of firm up uh you know some of the flows in the app, right? We can use both of these things together, right?

[00:31:37] David Khourshid: Right, right. And actually, I I encourage it. XState is not really a replacement for Redux or MobX since it doesn't try to manage your entire state. It it's just a pure function that you say, here's my current state and here's the action that just happened. What's the next state? That's the only thing it answers. And uh so I actually really appreciate you know what Redux has done for the JavaScript community because Redux, you know, whether people realize it or not, is sort of a just a grand intro to state machines because it essentially is a state machine. It's sort of an infinite state machine where you don't know what state might come out, but you know it's at least going to be deterministic. Um but all of those switch statements that you make in your Redux reducers, or you know maybe you're doing that in MobX state tree. Um, all of those switch statements could be modeled as finite state machines, because obviously with a switch statement, there's only a finite number of states and you're responding to a finite number of actions. And so that's exactly what a state machine is. The problem is you can't state- you you can't take a switch statement and extract all of those case conditions and have it output into a nice JSON object.

[00:32:54] Michael Jackson: Yeah. I mean, it seems like you could just have a You could have a reducer that uh that just looks at those actions and determines what the next- I mean, this sounds silly to say this because all reducers do this, but it it determines what the next state of the app is. And I'm not talking about, you know, your Redux state, like a counter or something. I'm talking about like, you know, the the the XState state, right, the name of the next sort of high-level state that this app is in. Right? Um, and you could drive some other things based on that. Um, so yeah, it seems like seems like the two would actually integrate really, really well. Well, cool. Um, right on. So David, uh, we have been going now for just over half an hour. Um, usually I kind of wrap things up around now. Is there anything else that you'd like to kind of tell people before we wrap things up?

[00:33:47] David Khourshid: Uh, sure. So um, just real quick, you were mentioning how like React It sort of like changes the way that you think about apps and it sort of moves you towards the state machine model, which is why I love React so much. And what's interesting is, and I actually talked to Dan about this at JSConf Iceland, uh with the new Suspense and the async rendering stuff, um it's moving even closer to the state machine and state chart model. And it's because uh with a state machine and state chart, Obviously, it will tell you the next states, but you also might have things done uh with those states. For example, if you're going from idle to loading something, you're going to need to fetch the search results, right? And so uh with React, their model is throwing a promise, which might seem crazy, but it's actually what's done in like in this conceptual way in many uh functional languages. As well. And so that models what statecharts do too, where you would give it a current state, give it an action- or sorry, an event, and you would get the next- well, yeah, the event is whatever the user does. Yeah. So given the current state and the event, you would get not just the next state, but you would get the next state and the actions to be performed. And so in React, that's modeled as-

[00:35:10] Michael Jackson: Okay, got it. So the next state is We throw the promise, which is now that we're in this pending state, basically. Um, and then the, I guess the action to be performed in that case would be resolve the promise, right?

[00:35:25] David Khourshid: Right, exactly. And so that that's why I encourage everyone, like as this async rendering and uh Suspense and time slicing stuff comes out more and more this year, as will be announced by the React team. Look at the similarities between that and state machines and see how it could help you model your app as a state machine.

[00:35:46] Michael Jackson: Yeah, I think I think they're just uh there are a lot, a lot of of avenues I think to explore with this. So uh thank you so much for coming on the podcast today, David Khourshid. Again, um where can people find you and follow you? You're you're @DavidKPiano pretty much everywhere, right? Uh yeah, so @DavidKPiano on Twitter, on on GitHub. Um, again, the author of XState and has done a ton of work with state machines. So thank you so much for taking the time to be with us today. And uh hopefully it won't be too long before uh we get to catch up in person. Thanks, David.

[00:36:20] David Khourshid: Absolutely. Thank you for having me.