Skip to content

Instantly share code, notes, and snippets.

@sonntam
Last active April 3, 2020 21:43
Show Gist options
  • Save sonntam/ddafcb27e6b8f726549eded0b26e7041 to your computer and use it in GitHub Desktop.
Save sonntam/ddafcb27e6b8f726549eded0b26e7041 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const mach1 = Machine({
context: {
counter1: 0,
counter2: 0
},
type: 'parallel',
states: {
machine1: {
initial: 'idle1',
states: {
idle1: {
on: {
TRIGGER: 'count1'
}
},
count1: {
invoke: { src: 'increaseCounter1'},
after: {
5000: 'idle1',
},
on: {
'BLA': {
actions: assign(
{
counter1: (ctx,ev) => {
console.warn(ev.value);
return ctx.counter1 + 1;
}
}
)
}
}
}
}
},
machine2: {
initial: 'idle2',
states: {
idle2: {
on: {
TRIGGER: 'count2'
}
},
count2: {
after: {
5000: 'idle2',
500: {
actions: assign({
counter2: (ctx) => ctx.counter2 + 1
}),
target: 'count2'
}
}
}
}
}
}
},
{
services: {
increaseCounter1: (ctx,ev) => (cb) => {
const { counter1 } = ctx;
const id = setInterval(() => {
cb({
type: 'BLA',
value: counter1 + 1
});
console.warn(counter1);
}, 500);
return () => clearInterval(id);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment