Rise of the state machines

Mat Warger

@mwarger

state Management

and more

state Management

state Management

state Management

state Management

state Management

state Management

problems

  • Software
  • Time
  • Users

software

software

is hard

"THIS SHOULD NEVER HAPPEN"

Title Text

Title Text

Title Text

Time

Time

is relative

situation

Reece went back in time too late, Sarah was killed,  and John Connor was never born.

result

SKYNET results in JUDGEMENT DAY.  DEATH to all humans.

Reece went back just in time, Sarah was saved,  and John Connor was born.

SKYNET results in JUDGEMENT DAY.  John Connor led the resistance and saved HUMANITY.

situation

result

A user types in their username and password, taps the sign-in button, and then clicks a link to a page.

User is signed in and is routed to the selected page.

A user types in their username and password, clicks a link to a page, and then taps the sign-in button.

User begins navigating and then executes a login action - may the fastest request win!

users

users

Are

U

N

p

R

e

d

i

c

t

a

b

l

e

users

Are

U

N

p

R

e

d

i

c

t

a

b

l

e

your users

your users

USER INPUT

STATE MACHINES

STATE MACHINES

Finite Automata

What are state machines?

  • An INITIAL STATE
  • A finite number of STATES
  • A number of EVENTS
  • A TRANSITION function between states
  • A FINAL STATE (optional)

What are state machines?

example

example

example

goals of xstate

  • Event-based architecture
  • Platform and framework-agnostic
  • Adherence to the SCXML Specification (including serialization)
  • Functional, type-safe*, with zero dependencies

example

INITIAL STATE

Fetch Machine

STATES

EVENTS

example

INITIAL STATE

Fetch Machine

STATES

EVENTS

and TRANSITIONS

Final State

state

state

action

state

event

state

event

+

=

state

event

+

=

state

state

event

+

=

transition

state

event

+

=

something happens

sound familiar?

you're already using them

you're already using them

one
key
Difference

one

state

at a time

what can I do with this?

  • State Visualization
  • Animations
    • ​Transitions
    • CSS
  • Model-based Testing

visualize large workflows

animations

model-based testing

demo

trade-offs and gotchas

everything is a state

 (async, "loading", etc)

NO EXCEPTIONS!

Resources

Thank you

@mwarger - Twitter, Github

mw.codes