Last active
March 25, 2020 20:18
-
-
Save saadshahd/75635776e30655dbdacb586042227878 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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