Skip to content

Instantly share code, notes, and snippets.

@aethant
Last active September 3, 2020 18:12
Show Gist options
  • Save aethant/899e09b1eb2e80755649f8e177b0be32 to your computer and use it in GitHub Desktop.
Save aethant/899e09b1eb2e80755649f8e177b0be32 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const postingStates = {
initial: 'posting',
states: {
posting: { // posting processing starts
entry: ['handlePost'], // handle POST
on: {
SUCCESS: "success", // POST succeeded
ERROR: "error", // POST failed
}
},
success: {
on: {
// NOTE: v4.11+ uses 'always' instead of ''
'': '#FeedEditorCTA.completed'
}, // reach outside nested state to core machine
},
error: {
on: {
// NOTE: v4.11+ uses 'always' instead of ''
'': '#FeedEditorCTA.failed'
},
}, // handle error state
},
actions: {
handlePost: (context, event) => { // action to handle posting data to server
console.log("POST xhr")
}
}
}
const FeedEditorMachine = Machine({
id: 'FeedEditorCTA',
initial: 'initial',
states: {
initial: { // starting point
on: {
FOCUS: 'focus' // user clicks on comp
}
},
focus: {
entry: ["sendMetrics"], // send metric ping
on: {
ACTIVE: 'active', // user enters content
// an UNFOCUS event is always sent when
// a user blurs away. However, since we
// only have a listener on "focus", if
// we're "active" and "unfocus",
// nothing will occur, as desired.
UNFOCUS: 'initial' // user blurs away
}
},
active: {
on: {
POST: 'post', // user clicks post button
INACTIVE: 'focus', // when no content present
}
},
post: {
...postingStates, // nested state for posting
},
completed: { // interaction is completed ok
entry: ['sendMetrics']
},
failed: { // interaction did not complete ok
entry: ['sendMetrics']
},
},
actions: {
sendMetrics: (context, event) => { // our telemetry ping
console.log("metrics sent here")
},
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment