Skip to content

Instantly share code, notes, and snippets.

@ShMcK
Last active July 18, 2018 02:12
Show Gist options
  • Save ShMcK/ea1759a06d75dd89e83670eaee906b5a to your computer and use it in GitHub Desktop.
Save ShMcK/ea1759a06d75dd89e83670eaee906b5a to your computer and use it in GitHub Desktop.
Visualizing State: Walkman State Machine Guard
 const events = {
  STOP: 'Stopped',
  PLAY: 'Playing',
  REWIND: 'Rewinding',
  FASTFORWARD: 'FastForwarding',
 }
class Walkman extends React.Component {
 transition = (target) => {
  const preventTapeJam =
['REWIND', 'FASTFORWARD', 'PLAY'].includes(target) &&
  this.state.current === 'stopped'
 
  if (!preventTapeJam) {
  this.setState({ current: events[target] })
  }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment