Skip to content

Instantly share code, notes, and snippets.

View davidkpiano's full-sized avatar
🎹
Working on XState Dev Tools

David Khourshid davidkpiano

🎹
Working on XState Dev Tools
View GitHub Profile
Machine({
initial: 'playing',
context: {
plays: 0
},
states: {
playing: {
on: {
'': {
cond: (context) => context.plays >= 3,
const donutMachine = Machine({
id: 'donut',
initial: 'ingredients',
states: {
ingredients: {
on: {
NEXT: 'directions'
}
},
directions: {
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
Machine({
id: 'screen',
initial: 'inactive',
states: {
inactive: {
on: {
TURN_ON: 'active'
}
},
active: {
const buttonFactory = id => {
const toggleName = `TOGGLE${id}`
return {
id: `button${id}`,
initial: 'off',
states: {
off: {
on: {
[toggleName]: 'on'
}
@davidkpiano
davidkpiano / machine.js
Created April 30, 2020 12:09
Generated by XState Viz: https://xstate.js.org/viz
const initial = {};
const fetchMachine = Machine({
id: 'panel',
initial: 'waiting',
context: initial,
states: {
waiting: {
// always reset to initial context
entry: assign(() => initial),
on: {
@davidkpiano
davidkpiano / ts-0-60.ts
Last active January 22, 2021 04:55
TypeScript from 0 to 60
// No TypeScript
function add(a, b) {
return a + b;
}
// Type function arguments
// vvvvvv vvvvvv
function add(a: number, b: number) {
return a + b;
}
const ticker = ctx => cb => {
const interval = setInterval(() => {
cb("TICK");
}, ctx.interval * 1000);
return () => clearInterval(interval);
};
const timerExpired = (ctx) => ctx.elapsed >= ctx.duration;
@davidkpiano
davidkpiano / machine.js
Last active March 31, 2020 18:55
Generated by XState Viz: https://xstate.js.org/viz
Machine({
id: 'questionnaire',
initial: 'symptoms',
states: {
symptoms: {
on: {
NEXT: 'fluTest'
}
},
fluTest: {
@davidkpiano
davidkpiano / machine.js
Last active November 1, 2020 21:16
Generated by XState Viz: https://xstate.js.org/viz
Machine({
id: 'Dog API',
initial: 'idle',
context: {
dog: null
},
states: {
idle: {
on: {
FETCH: 'loading'