Skip to content

Instantly share code, notes, and snippets.

@farskid
Last active June 29, 2021 20:26
Show Gist options
  • Save farskid/8c44f3bda8e809bb5c4e76d5cbad8393 to your computer and use it in GitHub Desktop.
Save farskid/8c44f3bda8e809bb5c4e76d5cbad8393 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
Machine({
id: 'a',
initial: 'checking_url',
context: {gistID: null, gistRawContent: null},
states: {
checking_url: {
entry: 'parseQueries',
always: [
{ target: 'with_gist', cond: 'isGistIDAvailable' },
{ target: 'no_gist' }
]
},
with_gist: {
initial: 'loading_content',
states: {
loading_content: {
invoke: {
src: 'loadGistContent',
onDone: {
target: 'gist_loaded',
actions: [(_, e) => e.data]
},
onError: {
target: 'gist_error',
actions: ['showError']
}
}
},
gist_loaded: {
type: 'final'
},
gist_error: {
type: 'final'
}
}
},
no_gist: {
type: 'final'
}
}
}, {
actions: {
parseQueries: assign({
gistID: new URLSearchParams(window.location.search).get('gist')
})
},
guards: {
isGistIDAvailable: ctx => !!ctx.gistID
},
services: {
loadGistContent: ctx => {
return fetch('https://api.github.com/gist/' + ctx.gistID).then(resp => {
if (resp.status === 404) {
return Promise.reject(Error('Gist not found'))
}
})
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment