Skip to content

Instantly share code, notes, and snippets.

View ShMcK's full-sized avatar

Shawn McKay ShMcK

View GitHub Profile
@ShMcK
ShMcK / states.js
Created July 17, 2018 02:50
State Machine with external actions
type Props = {
load(): void,
}
type States = {
mode: 'loading' | 'loaded' | 'errors',
data: any,
}
const actions = {
@ShMcK
ShMcK / xstate-example.js
Last active July 22, 2018 15:27
xstate example
import { Machine } from 'xstate'
const Alarm = {
Alarm: {
initial: 'Ringing',
on: { CANCEL: 'Idle' },
states: {
Ringing: {
on: { SNOOZE: 'Snoozing' },
onEntry: ['startRing'],
onExit: ['stopRing'],
@ShMcK
ShMcK / xstate-stateless.js
Created July 20, 2018 03:06
stateless xstate
let currentState = 'Idle'
const nextState = alarmMachine
.transition(currentState, 'ALARM_TRIGGERED')
nextState.value // 'Ringing'
nextState.actions // ['ring']
nextState.events // ['SNOOZE', 'CANCEL']
@ShMcK
ShMcK / react-automata-withStatechart.js
Created July 20, 2018 03:14
react-automata-withStatechart
import { withStatechart } from 'react-automata'
import alarmMachine from './alarmMachine'
import AlarmClock from './components/AlarmClock'
export default withStatechart(alarmMachine)(AlarmClock)
@ShMcK
ShMcK / state-as-prop.js
Created July 20, 2018 03:17
stateAsProp
import { withStatechart } from 'react-automata'
import alarmMachine from './alarmMachine'
const AlarmClock = ({ machineState }) => (
<Clock ringing={machineState.value === 'Ringing'}/>
)
export default withStatechart(alarmMachine)(AlarmClock)
@ShMcK
ShMcK / state-as-context.js
Last active July 23, 2018 14:45
State as Context
import { State, withStatechart } from 'react-automata'
import alarmMachine from './alarmMachine'
const AlarmClock = () => (
<>
<State value='Ringing'>
<Clock ringing />
</State>
<State value=['Idle', 'Snoozing']>
<Clock />
@ShMcK
ShMcK / state-as-context-render-prop.js
Last active July 23, 2018 14:43
state-as-context-render-prop
import { State, withStatechart } from 'react-automata'
import alarmMachine from './alarmMachine'
const AlarmClock = () => (
<State
value='Ringing'
render={(ringing) => (
<Clock ringing={ringing} />
)}
/>
@ShMcK
ShMcK / action-as-context.js
Last active July 23, 2018 14:40
action-as-context
import { Action, withStatechart } from 'react-automata'
import alarmMachine from './alarmMachine'
const AlarmClock = () => (
<>
<Action show='ring'>
<Ringing />
</Action>
<Clock />
</>
@ShMcK
ShMcK / state-from-actions.js
Created July 20, 2018 03:52
state-from-actions
import { withStatechart } from 'react-automata'
import alarmMachine from './alarmMachine'
class AlarmClock extends React.Component {
state = {
ringing: false
}
startRing = () => {
this.setState({ ringing: true })
}
@ShMcK
ShMcK / xstateful-setup.js
Created July 20, 2018 03:55
xstateful setup
import { Machine } from 'xstate'
import { createStatefulMachine } from '@avaragado/xstateful'
import alarmMachine from './alarmMachine'
const alarm = createStatefulMachine({
machine: alarmMachine
})
alarm.init()