MobX Patterns: State Machines & Flags

Originally Posted May 14th 2018

A few weeks ago, I was listening to the React Podcast, specifically their episode on State Machines with David Khourshid. About 20 minutes into the podcast, there was a discussion on the benefits of using enums over flags to model the state of objects that can change across multiple dimensions. It reminded me of one of the nice patterns that MobX makes possible, and I thought I’d share it here.

Background

To set the stage here, the podcast discussion centered around modeling an async process like a promise. One possible way given was to use a mix of flags and if statements like this:

if(loader.hasError){// show error state}elseif(loader.isLoading){// show loading state}else{// show data State}

In the podcast David Khourshid criticized this as a haphazard way to manage state, that doesn’t show the whole picture. Because we can’t easily see how the various flags combine, it’s tough to keep track of all possible states. For instance, in the above example, did we really want to show the error state if we had an error, but have called load() again and both hasError and isLoading are now true? Maybe that was the intention, but maybe that was just a state that was missed during the original construction of the program. It’s hard to tell.

As an alternative to full blown state machine modeling, David advocated for an easy first step of swapping out flags for enums. In that case the code above might look like:

We can see just from using an enum, we’ve cleared some ambiguity: now it is clear that we should be showing a loading state when we’re loading data, even if a previous load resulted in an error. And we’ve also been forced to remember another state distinction that was missed originally, the difference between a successful load and the initial state before load is called. This type of Enum-based approach is really helpful for modeling complex systems and especially async process state. But it’s not perfect. While ENUMs are great for listing out every possible state, sometimes we just need to know if an object is in a particular state. For instance, we might have a special help-prompt component that should only show if our data is in an error state, but is otherwise divorced from the details of the data. In that case loader.hasError is simpler and cleaner than loader.state === STATES.ERROR. It’s a straight forward named expression that doesn’t require importing a constant from another file or matching up to a string value. Enums add some verbosity and ceremony to code that can otherwise be very simple. Fortunately MobX allows us to have our cake and eat it too.

MobX

MobX is a state management solution created by Michael Weststrate. It grew out of the React community but can be used with other frameworks or on it’s own. One of its nicest features are its intelligently updated computed properties. With MobX we can write our loader object like this:

And because MobX takes care of the details of efficiently updating our flags based on state, we know everything will stay in sync and remain performant, without a bunch of ugly code to make sure of that.