Skip to content

Instantly share code, notes, and snippets.

@hosmelq
Last active December 14, 2019 01:05
Show Gist options
  • Save hosmelq/1a3e0f78d5a7291262978adb66f43a08 to your computer and use it in GitHub Desktop.
Save hosmelq/1a3e0f78d5a7291262978adb66f43a08 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
function buildVibrationPattern(seconds) {
return new Array(seconds / 1500)
.fill(null)
.reduce(prev => [...prev, 0, 1000, 500], [])
}
const ACTIVE = `active`
const BACKGROUND = `background`
const WAIT_TIME = 30000
const PATTERN = buildVibrationPattern(WAIT_TIME)
new Machine(
{
id: `courier-state`,
initial: `initializing`,
context: {
appState: ACTIVE,
currentOrderId: null,
online: false,
order: null,
orders: [],
time: null
},
on: {
SET_APP_STATE: {
actions: `setAppState`
}
},
states: {
initializing: {
on: {
'': [
{
cond: `isOffline`,
target: `offline.success`
},
{
cond: `isOnline`,
target: `online.offJob`
}
]
}
},
offline: {
initial: `loading`,
on: {
ONLINE: `online`
},
states: {
loading: {
invoke: {
id: `go-offline`,
src: `goOffline`,
onDone: `success`,
onError: `failure`
}
},
success: {
entry: `setOffline`
},
failure: {
entry: `setOffline`
}
}
},
online: {
initial: `loading`,
on: {
OFFLINE: `offline`
},
states: {
loading: {
invoke: {
id: `go-online`,
src: `goOnline`,
onDone: `offJob`,
onError: `failure`
}
},
failure: {
entry: `setOffline`
},
offJob: {
entry: `setOnline`,
on: {
OFFER: {
actions: [`setCurrentOrderId`, `setTime`],
target: `offer`
}
}
},
offer: {
initial: `checkAppState`,
states: {
checkAppState: {
entry: `vibrate`,
on: {
'': [
{
cond: `isBackground`,
target: `background`
},
{
cond: `isActive`,
target: `waiting`
}
]
}
},
background: {
on: {
ACTIVE: `waiting`
}
},
waiting: {
on: {
ACCEPT_JOB: `accepted`,
REJECT_JOB: `rejected`
},
after: {
ORDER_DELAY: `rejected`
}
},
accepted: {
entry: `cancelVibration`,
invoke: {
id: `accept-order`,
src: `acceptOrder`,
onDone: `#courier-state.online.onJob`
}
},
rejected: {
entry: `cancelVibration`,
invoke: {
id: `reject-order`,
src: `rejectOrder`,
onDone: `#courier-state.online.offJob`
}
}
}
},
onJob: {
entry: `setOrder`
}
}
}
}
},
{
actions: {
vibrate: () => {},
cancelVibration: () => {},
setAppState: assign({
appState: ctx => ctx.appState === ACTIVE ? BACKGROUND : ACTIVE
}),
setOffline: assign({
online: false
}),
setOnline: assign({
online: true
}),
setOrder: assign({
order: {
id: 1
}
}),
setTime: assign({
time: Date.now()
})
},
delays: {
ORDER_DELAY: ctx => {
if (!ctx.time) {
return WAIT_TIME
}
return Math.floor(ctx.time + WAIT_TIME - Date.now())
}
},
guards: {
isActive: ctx => ctx.appState === ACTIVE,
isBackground: ctx => ctx.appState === BACKGROUND,
isOffline: ctx => !ctx.online,
isOnline: ctx => ctx.online
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment