Skip to content

Instantly share code, notes, and snippets.

@saadshahd
Last active March 25, 2020 20:18
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 saadshahd/75635776e30655dbdacb586042227878 to your computer and use it in GitHub Desktop.
Save saadshahd/75635776e30655dbdacb586042227878 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const test = (regex, base) => base && base.name && regex.test(base.name);
const isStart = ({ base }) => test(/^start-/, base);
const isFull = ({ base }) => !test(/^start-/, base);
const baseMachine = Machine({
id: 'base',
initial: 'init',
context: {
startBase: null,
base: {}
},
states: {
init: {
on: {
next: {
target: 'addFields',
actions: [
assign({
base: (context, base) => base
})
]
}
}
},
addEnd: {
on: {
next: {
target: 'addFields',
actions: [
assign({
startBase: ({base: startBase}) => startBase,
base: (_, endBase) => endBase
})
]
}
}
},
addFields: {
on: {
next: {
target: 'determineBaseType',
actions: [
assign({
base: ({base}, {fields}) => ({
...base,
fields
})
})
]
}
}
},
determineBaseType: {
on: {
'': [
{ target: 'startCreated', cond: isStart },
{ target: 'fullCreated', cond: isFull }
]
}
},
fullCreated: {
type: 'final'
},
startCreated: {
on: {
'': 'addEnd'
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment