Skip to content

Instantly share code, notes, and snippets.

@TheDutchCoder
Last active September 24, 2020 20:45
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const get = () => {
const fail = Math.random() > 0.75
return new Promise((resolve, reject) => {
setTimeout(() => {
if (fail) {
reject({ code: 1 })
}
resolve('cool')
}, 1000)
})
}
const onwedStates = {
on: {
GO_TO_JOINED: 'joined',
GO_TO_INVITES: 'invites',
},
initial: 'waiting',
states: {
idle: {
initial: 'noError',
states: {
noError: {},
error: {},
},
},
waiting: {
invoke: {
src: 'fetchOwned',
onDone: 'idle',
onError: 'idle.error',
},
},
},
}
const joinedStates = {
on: {
GO_TO_OWNED: 'owned',
GO_TO_INVITES: 'invites',
},
initial: 'waiting',
states: {
idle: {
initial: 'noError',
states: {
noError: {},
error: {},
},
},
waiting: {
invoke: {
src: 'fetchJoined',
onDone: 'idle',
onError: 'idle.error',
},
},
},
}
const invitesStates = {
on: {
GO_TO_OWNED: 'owned',
GO_TO_JOINED: 'joined',
},
initial: 'waiting',
states: {
idle: {
initial: 'noError',
states: {
noError: {},
error: {},
},
},
waiting: {
invoke: {
src: 'fetchInvites',
onDone: 'idle',
onError: 'idle.error',
},
},
},
}
const profileMachine = Machine({
id: 'profile',
initial: 'owned',
states: {
owned: { ...onwedStates },
joined: { ...joinedStates },
invites: { ...invitesStates },
},
}, {
services: {
fetchOwned: (c, e) => get(),
fetchJoined: (c, e) => get(),
fetchInvites: (c, e) => get(),
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment