Skip to content

Instantly share code, notes, and snippets.

@isaacplmann
Last active November 5, 2019 20:17
Show Gist options
  • Save isaacplmann/bad797c96559946faf1ebbcb200c0ae7 to your computer and use it in GitHub Desktop.
Save isaacplmann/bad797c96559946faf1ebbcb200c0ae7 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const stepMachine = Machine({
id: 'step',
initial: 'step1',
states: {
step1: {},
step2: {}
}
});
const parentMachine = Machine(
{
id: 'parent',
initial: 'noStates',
context: {
statesArray: []
},
states: {
noStates: {
on: {
PUSH: {
actions: 'push',
target: 'someStates'
}
}
},
someStates: {
on: {
PUSH: {
actions: 'push',
target: 'someStates'
},
POP: {
actions: 'pop',
target: 'someStates'
},
POP: {
cond: 'oneStateLeft',
actions: 'pop',
target: 'noStates'
}
}
}
},
on: {
NEXT: send('NEXT', { to: ctx => ctx.statesArray[0] }),
PREV: send('PREV', { to: ctx => ctx.statesArray[0] })
}
},
{
actions: {
push: assign({
statesArray: ctx => {
// console.log(ctx);
return [spawn(stepMachine), ...ctx.statesArray];
}
}),
pop: assign({
statesArray: ctx => ctx.statesArray.slice(1)
})
},
guards: {
oneStateLeft: ctx => {
// console.log(ctx.statesArray.length);
return ctx.statesArray.length === 1;
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment