Skip to content

Instantly share code, notes, and snippets.

@aurerua
Last active December 10, 2019 17:13
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 aurerua/b189460068a493faec6e46da74e48d38 to your computer and use it in GitHub Desktop.
Save aurerua/b189460068a493faec6e46da74e48d38 to your computer and use it in GitHub Desktop.
XState history + activities
const myMachine = Machine({
initial: 'slide',
states: {
slide: {
id: 'slide',
initial: 'ground',
states: {
ground: {
on: {
CLIMB_UP: 'ladder'
}
},
ladder: {
initial: 'step1',
states: {
step1: {
on: {
CLIMB_UP: 'step2',
CLIMB_DOWN: '#slide.ground'
}
},
step2: {
on: {
CLIMB_UP: '#slide.topOfSlide',
CLIMB_DOWN: 'step1'
}
}
}
},
topOfSlide: {
on: {
CLIMB_DOWN: 'ladder.step2',
SLIDE: 'ground'
}
},
hist: {
type: 'history',
history: 'deep'
}
},
on: {
FLY: '#flying'
}
},
flying: {
activities: 'yell',
id: 'flying',
on: {
LAND: 'slide.hist'
},
}
},
}, {
activities: {
yell: (ctx) => {
// Start the activity
const interval = setInterval(
() => console.log('LOOK AT ME!'),
1000
);
// Return a function that stops the activity
return () => {
clearInterval(interval);
};
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment