Skip to content

Instantly share code, notes, and snippets.

@ascorbic
Last active March 18, 2020 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ascorbic/149c3d21a5f589c238b163cda9bab21b to your computer and use it in GitHub Desktop.
Save ascorbic/149c3d21a5f589c238b163cda9bab21b 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 MAX_RECURSION = 2
const rageAgainstTheStateMachine = ctx =>
console.error(`Fuck you I won't build what you tell me`)
const assignMutatedNodes = assign((ctx, event) => {
return {
nodesMutatedDuringQueryRun:
ctx.nodesMutatedDuringQueryRun || event.nodesMutated,
}
})
const buildMachine = Machine({
id: `build`,
initial: `bootstrapping`,
context: {
recursionCount: 0,
nodesMutatedDuringQueryRun: false,
firstRun: true,
},
states: {
bootstrapping: {
on: {
DONE: {
target: `customizingSchema`,
},
ERROR: {
target: `failed`,
},
},
},
customizingSchema: {
on: {
COMPLETE: {
target: `sourcingNodes`,
},
ERROR: {
target: `idle`,
},
},
},
sourcingNodes: {
on: {
COMPLETE: {
target: `inferringSchema`,
},
ERROR: {
target: `idle`,
},
},
},
inferringSchema: {
on: {
COMPLETE: {
target: `runningCreatePages`,
actions: assignMutatedNodes,
},
ERROR: {
target: `idle`,
},
},
},
runningCreatePages: {
on: {
COMPLETE: [
{
target: `runningCreatePagesStatefully`,
cond: ctx => ctx.firstRun,
actions: assign((ctx, event) => {
return {
nodesMutatedDuringQueryRun:
ctx.nodesMutatedDuringQueryRun || event.nodesMutated,
firstRun: false,
}
}),
},
{
target: `runningStaticQueries`,
actions: assignMutatedNodes,
},
],
ERROR: {
target: `idle`,
},
},
},
runningCreatePagesStatefully: {
on: {
COMPLETE: {
target: `runningStaticQueries`,
actions: assignMutatedNodes,
},
ERROR: {
target: `idle`,
},
},
},
runningStaticQueries: {
on: {
COMPLETE: {
target: `runningPageQueries`,
actions: assignMutatedNodes,
},
ERROR: {
target: `idle`,
},
},
},
runningPageQueries: {
on: {
COMPLETE: [
{
target: `writingArtifacts`,
cond: (ctx, event) =>
!(ctx.nodesMutatedDuringQueryRun || event.nodesMutated),
},
{
actions: assign(ctx => {
return {
recursionCount: ctx.recursionCount + 1,
nodesMutatedDuringQueryRun: false, // Resetting
}
}),
target: `customizingSchema`,
cond: (ctx, event) => ctx.recursionCount < MAX_RECURSION,
},
{
invoke: {
src: `rageAgainstTheStateMachine`,
},
actions: assign(ctx => {
return {
recursionCount: 0,
nodesMutatedDuringQueryRun: false, // Resetting
}
}),
target: `idle`,
},
],
ERROR: {
target: `idle`,
},
},
},
writingArtifacts: {
on: {
SUCCESS: {
target: `idle`,
},
ERROR: {
target: `idle`,
},
},
},
transactionRunning: {
on: {
COMMIT_TRANSACTION: {
target: `customizingSchema`,
},
ERROR: {
target: `idle`,
},
},
},
batchingPageMutations: {
on: {
COMMIT_BATCH: {
target: `runningStaticQueries`,
},
ERROR: {
target: `idle`,
},
},
},
idle: {
on: {
WEBHOOK_RECEIVED: {
target: `transactionRunning`,
},
ENQUEUE_NODE_MUTATION: {
target: `transactionRunning`,
},
ENQUEUE_PAGE_MUTATION: {
target: `batchingPageMutations`,
},
CREATE_TRANSACTION: {
target: `transactionRunning`,
},
},
},
failed: {
final: true,
},
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment