Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State machines #38

Merged
merged 7 commits into from
Jan 27, 2018
Merged

State machines #38

merged 7 commits into from
Jan 27, 2018

Conversation

yoshuawuyts
Copy link
Member

Adds a section about state machines: what to use them for, and how to implement one. Thanks!

@yoshuawuyts yoshuawuyts requested a review from a user January 10, 2018 01:14
Or in a more compact notation:

```js
var stateMachine => (t, c, n) => t[c][n]
Copy link

@bendrucker bendrucker Jan 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first => should be =

If you're looking for a solid state machine implementation, check out
[choojs/nanostate](https://github.com/choojs/nanostate). It's similar to the
state machine we implemented in the section above, but adds event hooks, clean
error messages and more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oxford comma? (, and more)

@bendrucker
Copy link

Super well done, caught some typos for ya!

@yoshuawuyts
Copy link
Member Author

Pushed fixes! Thanks @bendrucker

# state machines
If you work in software long enough, you'll have to deal with legacy codebases.
but you'll also have had the chance to start projects from scratch. And
eventually see your project become a legacy codebase itself.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eventually you'll see [...]

```

And that's the basics. From here we could go on to add more states and
transitions, expanding the graph.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

graph here introduces a concept that previously hasnt been talked about, might be good to add a representation of the state machine as a graph, visually

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but don't have any graph tools around right now. If someone has a graph that'd be neat!

## State machines in JavaScript
Let's implement the traffic light example in JavaScript. In order for this to
work, we'll need to implement:
- save the states & transitions in an Object
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saving

work, we'll need to implement:
- save the states & transitions in an Object
- the core state machine algorithm
- create a small, stateful class to hold the state
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

creating

- save the states & transitions in an Object
- the core state machine algorithm
- create a small, stateful class to hold the state
- combine all of these to form the complete state machine.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

combining


### Class
While the state machine algorithm is quite simple, it requires us to keep track
of what the current state is. This means that statemachines themselves are
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

state machines

- Nested state machines.

We'll leave this as an exercise up to the reader.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo this is missing usage examples in user interface design, as hinted at during the first section

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aye, yeah fair

@yoshuawuyts
Copy link
Member Author

Made an issue for the last unsolved comment. Merging now, will fix the remaining comments later! ✨

V. excited to have some of these sections up! 😁

@yoshuawuyts yoshuawuyts merged commit 4f76a81 into master Jan 27, 2018
@yoshuawuyts yoshuawuyts deleted the state-machines branch January 27, 2018 02:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants