Skip to content

Instantly share code, notes, and snippets.

@riccardo-forina
Created April 15, 2021 08:56
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 riccardo-forina/ce7db081ed350655c1107d9ba39a4d36 to your computer and use it in GitHub Desktop.
Save riccardo-forina/ce7db081ed350655c1107d9ba39a4d36 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 kafkaMachine = Machine({
id: 'kafkas',
initial: 'loading',
context: {},
states: {
loading: {
invoke: {
id: 'fetchKafkaInstances',
src: 'something',
onDone: {
target: 'verify',
actions: assign({
instances: (_context, event) => event.data.data,
}),
},
onError: {
target: 'failure',
actions: assign({
error: (_context, event) => event.data,
}),
},
},
},
failure: {
},
verify: {
on: {
'': [
{ target: 'selecting', cond: 'noInstanceSelected' },
{ target: 'valid', cond: 'instanceSelected' },
]
}
},
selecting: {
entry: sendParent('isInvalid'),
on: {
selectInstance: {
target: 'valid',
actions: 'selectInstance',
},
},
},
valid: {
entry: sendParent('isValid'),
on: {
selectInstance: {
target: 'verify',
actions: 'selectInstance',
},
deselectInstance: {
target: 'verify',
actions: 'reset'
},
confirm: {
target: 'done'
}
}
},
done: {
type: "final",
data: {
selectedInstance: (context) => context.selectedInstance
}
}
},
}, {
services: {
'something': Promise.resolve({
data: {
data: {
instances: []
}
}
})
},
actions: {
selectInstance: assign({
selectedInstance: (context, event) => {
if (event.type === "selectInstance") {
return context.instances?.items.find(i => i.id == event.selectedInstance);
}
return context.selectedInstance;
}
}),
reset: assign({
selectedInstance: (context, event) => event.type === "deselectInstance" ? undefined : context.selectedInstance
})
},
guards: {
instanceSelected: (context) => context.selectedInstance !== undefined,
noInstanceSelected: (context) => context.selectedInstance === undefined,
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment