Skip to content

Instantly share code, notes, and snippets.

@bmartel
Last active July 28, 2022 18:32
Show Gist options
  • Save bmartel/8447dfcb8e0a9b3a7d712a881630bafd to your computer and use it in GitHub Desktop.
Save bmartel/8447dfcb8e0a9b3a7d712a881630bafd 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 fetchMachine = Machine({
id: "taskDiscussions",
initial: "idle",
context: {
discussionId: null,
task: { id: -1, openDiscussionId: null },
annotation: { id: null },
comments: [
// { id: 1, text: "Hello" },
],
},
states: {
idle: {
on: {
INIT: {
target: "fetchingRelatedDiscussion",
actions: ["updateTask", "updateAnnotation"],
},
},
},
fetchingRelatedDiscussion: {
on: {
FETCH_OPEN_DISCUSSION: {
target: "loading",
actions: ["markTaskDiscussionOpen"]
},
FETCH_ANNOTATION_DISCUSSION: {
target: "loading",
actions: ["markTaskDiscussionClosed", "updateAnnotation"]
}
}
},
loading: {
invoke: {
id: "fetch-discussion",
src: "loadDiscussion",
onDone: {
target: "loaded",
actions: ["setDiscussionComments"],
},
onError: {
target: "failure",
},
},
},
loaded: {
on: {
SUBMIT_ANNOTATION: "submitting",
UPDATE_ANNOTATION: "updating",
REJECT_ANNOTATION: "rejecting",
ACCEPT_ANNOTATION: "accepting",
SKIP_TASK: "skipping",
NEXT_TASK: {
target: "idle",
actions: ["updateTask", "updateAnnotation"],
},
PREVIOUS_TASK: {
target: "idle",
actions: ["updateTask", "updateAnnotation"],
},
},
},
submitting: {
invoke: {
id: "submitAnnotation",
src: "submitAnnotation",
onDone: {
target: "idle",
actions: [
"markTaskDiscussionAsClosed",
"transferDiscussionCommentsToAnnotation",
"updateTask",
"updateAnnotation",
],
},
onError: {
target: "submitFailure",
},
},
},
updating: {
invoke: {
id: "updateAnnotation",
src: "updateAnnotation",
onDone: [
{
target: "idle",
actions: [
"markTaskDiscussionAsClosed",
"updateTask",
"updateAnnotation",
],
},
],
onError: {
target: "updateFailure",
},
},
},
accepting: {
invoke: {
id: "acceptAnnotation",
src: "rejectAnnotation",
onDone: {
target: "idle",
actions: ["updateTask", "updateAnnotation"],
},
onError: {
target: "acceptFailure",
},
},
},
rejecting: {
invoke: {
id: "rejectAnnotation",
src: "rejectAnnotation",
onDone: {
target: "idle",
actions: ["updateTask", "updateAnnotation"],
},
onError: {
target: "rejectFailure",
},
},
},
skipping: {
invoke: {
id: "skipTask",
src: "skipTask",
onDone: {
target: "idle",
actions: [
"markTaskDiscussionAsOpen",
"updateTask",
"updateAnnotation",
],
},
onError: {
target: "skipFailure",
},
},
},
failure: {
on: {
RETRY: "loading",
},
},
submitFailure: {
on: {
RETRY: "submitting",
},
},
updateFailure: {
on: {
RETRY: "updating",
}
},
skipFailure: {
on: {
RETRY: "skipping",
},
},
acceptFailure: {
on: {
RETRY: "accepting",
},
},
rejectFailure: {
on: {
RETRY: "rejecting",
},
},
},
guards: {
annotationExists: (context) =>
!!(context.annotation && context.annotation.id),
annotationMissing: (context) =>
!context.annotation || !context.annotation.id,
},
actions: {
updateTask: assign({
task: (context, event) => ({ ...context.task, ...event.task }),
}),
markTaskDiscussionAsOpen: assign({
task: (context, event) => ({
...context.task,
openDiscussionId: event.discussion.id,
}),
}),
markTaskDiscussionAsClosed: assign({
task: (context, event) => ({ ...context.task, openDiscussionId: null }),
}),
transferDiscussionCommentsToAnnotation: assign({
comments: (context, event) => event.comments,
}),
rejectAnnotation: assign({
annotation: (context, event) => ({
...context.annotation,
id: event.annotation.id,
rejected: Date.now(),
}),
}),
acceptAnnotation: assign({
annotation: (context, event) => ({
...context.annotation,
id: event.annotation.id,
rejected: null,
}),
}),
submitAnnotation: assign({
annotation: (context, event) => event.annotation,
}),
updateAnnotation: assign({
annotation: (context, event) => event.annotation,
}),
nextTask: assign({
task: (context, event) => event.task,
}),
previousTask: assign({
task: (context, event) => event.task,
}),
setDiscussionComments: assign({
discussionId: (context, event) => event.discussion.id,
comments: (context, event) => event.discussion.comments,
}),
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment