This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { State, withStatechart } from 'react-automata' | |
import alarmMachine from './alarmMachine' | |
const AlarmClock = () => ( | |
<> | |
<State value='Ringing'> | |
<Clock ringing /> | |
</State> | |
<State value=['Idle', 'Snoozing']> | |
<Clock /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { withStatechart } from 'react-automata' | |
import alarmMachine from './alarmMachine' | |
const AlarmClock = ({ machineState }) => ( | |
<Clock ringing={machineState.value === 'Ringing'}/> | |
) | |
export default withStatechart(alarmMachine)(AlarmClock) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { withStatechart } from 'react-automata' | |
import alarmMachine from './alarmMachine' | |
import AlarmClock from './components/AlarmClock' | |
export default withStatechart(alarmMachine)(AlarmClock) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let currentState = 'Idle' | |
const nextState = alarmMachine | |
.transition(currentState, 'ALARM_TRIGGERED') | |
nextState.value // 'Ringing' | |
nextState.actions // ['ring'] | |
nextState.events // ['SNOOZE', 'CANCEL'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Machine } from 'xstate' | |
const Alarm = { | |
Alarm: { | |
initial: 'Ringing', | |
on: { CANCEL: 'Idle' }, | |
states: { | |
Ringing: { | |
on: { SNOOZE: 'Snoozing' }, | |
onEntry: ['startRing'], | |
onExit: ['stopRing'], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Props = { | |
load(): void, | |
} | |
type States = { | |
mode: 'loading' | 'loaded' | 'errors', | |
data: any, | |
} | |
const actions = { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const events = { | |
STOP: 'Stopped', | |
PLAY: 'Playing' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Walkman extends React.Component { | |
transition = (event) => { | |
const preventTapeJam = /* ... */ | |
const shouldEject = event === 'STOP' && this.state.current === 'Stopped' | |
if (!preventTapeJam) { | |
/* … */ | |
} else if (shouldEject) { | |
this.setState({ current: events.EJECT }) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const events = { | |
STOP: 'Stopped', | |
PLAY: 'Playing', | |
REWIND: 'Rewinding', | |
FASTFORWARD: 'FastForwarding', | |
} | |
class Walkman extends React.Component { | |
transition = (target) => { | |
const preventTapeJam = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const events = { | |
STOP: 'Stopped', | |
PLAY: 'Playing', | |
REWIND: 'Rewinding', | |
FASTFORWARD: 'FastForwarding', | |
} | |
class Walkman extends React.Component { | |
state = { | |
current: events.STOP, |