ifandelse / machina.js

js ex machina - finite state machines in JavaScript
http://machina-js.org/
Other
1.93k stars 147 forks source link

How would I handle this? #162

Open stephankaag opened 5 years ago

stephankaag commented 5 years ago

Let's say for example my machine represents a light bulb. The two obvious states would be 'on' and 'off'. But, if the bulb is dimmable and the dimmer value is some value between 1 and 100 how would I handle this 'second state' that a bulb has?

I think defining the states ['on_1', 'on_2', 'on_3'.., 'on_100'] would be suboptimal. Is there a better way?

ctmackay commented 5 years ago

Why does the dimness of the light bulb have to be a state at all? You should use the variable “brightness” in the internal logic of the on state.

The value of the brightness doesn’t matter to be able to transition to the off state. All 100 brightness levels transition to the off state, is that right ? Or do they transition to one another ?

I would design the architecture such that you do not have a need for any other state other than on / off

andreabadesso commented 5 years ago

I have a finite state machine (in erlang) describing an light bulb on my home automation startup, the way we designed it was to have two states:

idle() The light bulb is available to be messed with.

We have methods here to turn on and off, dim the light, etc...

wait_reply() Everytime a message is sent, we transition to this state until the response is received (our products communicate via RF).

We process the message here and transition to idle back again, storing the new state.