Last active
August 20, 2020 09:35
-
-
Save alizbazar/90c96fa3dbdb7418a10ab01b41a0e879 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
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
// 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