Last active
November 5, 2019 20:17
-
-
Save isaacplmann/bad797c96559946faf1ebbcb200c0ae7 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or 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 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