Skip to content

Instantly share code, notes, and snippets.

@thiagomata
Last active August 12, 2021 13:07
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 thiagomata/26789b19aaf08fd6272c44fecb0ddac3 to your computer and use it in GitHub Desktop.
Save thiagomata/26789b19aaf08fd6272c44fecb0ddac3 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Invoked child machine
const counterMachine = Machine({
id: 'counter',
initial: 'active',
context:{
current: 0,
times: 0,
timesLimit: 5,
},
states: {
active: {
on: {
NEXT: {
target: 'check',
actions: assign({
current: (context, event) => context.current + 1
})
}
},
after: {
1000: {
target: 'check',
actions: assign({
current: function (context, event) {
console.log("incrementing current");
console.log(context.current + 1);
return context.current + 1;
}
})
}
}
},
check: {
on: {
'': [
{
target: 'end',
cond: 'endLoop'
},
{
target: 'active',
actions: assign({
times: function (context, event) {
console.log("incrementing times");
console.log(context.times + 1);
return context.times + 1
}
})
}
]
}
},
end: {
type: 'final',
data: {
current: (context,event) => context.current
}
}
}
},{
guards: {
endLoop: function (context) {
let result = context.times >= context.timesLimit
return result
}
},
});
const parentMachine = Machine({
id: 'parent',
initial: 'active',
context: {
current: 0
},
states: {
active: {
invoke: {
id: 'counter',
src: counterMachine,
onDone: {
target: 'success',
actions: assign({ current: function (context, event) {
return event.data.current;
}
})
}
},
entry: send(
{
type: 'NEXT'
},
{
to: 'counter',
delay: 1000
}
),
},
success: {
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment