Skip to content

Instantly share code, notes, and snippets.

@alizbazar
Last active August 20, 2020 09:35
Show Gist options
  • Save alizbazar/90c96fa3dbdb7418a10ab01b41a0e879 to your computer and use it in GitHub Desktop.
Save alizbazar/90c96fa3dbdb7418a10ab01b41a0e879 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const MIN_VIEW_TIME = 5000
const SHOW_AFTER_DISMISS = 5000
const SHOW_AFTER_ANSWER = 10000
// Change this to any async function that either resolves (if on Today) or rejects
const isOnTodayScreen = () => Promise.reject()
const feedbackMachine = Machine({
id: 'feedback',
initial: 'init',
context: {
currentState: 'treatment',
delayedUntil: null,
},
states: {
init: {
on: {
'': [{
cond: 'isDelayed',
target: 'delayForNextTime',
}, {
cond: 'isApplicable',
target: 'waitingToTrigger'
}, {
target: 'notApplicable',
}]
},
},
notApplicable: {},
delayForNextTime: {
on: {
TICK: 'init',
},
},
waitingToTrigger: {
on: {
VISIT_CHAT: 'rendered',
}
},
rendered: {
on: {
DISMISS: {
actions: 'setDismissed',
target: 'delayForNextTime',
},
ANSWER: {
actions: 'setAnswered',
target: 'delayForNextTime',
},
},
initial: 'init',
states: {
init: {
invoke: {
src: 'isOnTodayScreen',
onDone: 'viewing',
onError: 'hidden',
},
},
hidden: {
on: {
NAVIGATE_TODAY: 'viewing',
},
},
viewing: {
after: {
[MIN_VIEW_TIME]: 'didntReact',
},
on: {
NAVIGATE_AWAY: 'hidden',
},
},
didntReact: {
on: {
NAVIGATE_AWAY: {
actions: raise('DISMISS'),
},
},
},
},
},
}
}, {
guards: {
isApplicable: (ctx) => ctx.currentState === 'treatment',
isDelayed: ctx => ctx.delayedUntil && Date.now() < ctx.delayedUntil
},
actions: {
setAnswered: assign({ delayedUntil: () => Date.now() + SHOW_AFTER_DISMISS }),
setDismissed: assign({ delayedUntil: () => Date.now() + SHOW_AFTER_DISMISS }),
},
services: {
isOnTodayScreen,
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment