Skip to content

Commit

Permalink
Exercise 01 - Creating a State Machine - object
Browse files Browse the repository at this point in the history
  • Loading branch information
chesterheng committed May 14, 2020
1 parent dab6027 commit cb7a464
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions 01/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
const elBox = document.querySelector('#box');

// FSM: ->inactve <-CLICK-> active
const machine = {
initial: "inactive",
states: {
inactive: {
on: {
CLICK: "active"
}
},
active: {
on: {
CLICK: "inactive"
}
}
}
}

// Pure function that returns the next state,
// given the current state and sent event
const transition = (state, event) => {
// FSM: ->inactve <-CLICK-> active
switch (state) {
case "inactive":
switch (event) {
case "CLICK":
return "active";
default:
return state;
}
case "active":
switch (event) {
case "CLICK":
return "inactive";
default:
return state;
}
default:
return state;
}
return machine.states[state].on?.[event] || state ;
}

// Keep track of your current state
// start with inactive initial state
let currentState = "inactive";
let currentState = machine.initial;

const send = event => {
// Determine the next value of `currentState`
Expand Down

0 comments on commit cb7a464

Please sign in to comment.