Last active
September 21, 2021 10:13
-
-
Save skkim7821/d2a1b4de4d5010c8c982577f1b96f400 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const fetching = () => fetch('https://hacker-news.firebaseio.com/v0/item/160705.json?print=pretty') | |
.then(res => res.json()); | |
const fetchMachine = Machine( | |
{ | |
id: 'fetch', | |
initial: 'idle', | |
context: { | |
data: {}, | |
error: {}, | |
isFetching: false | |
}, | |
states: { | |
idle: { | |
entry: 'isNotFetching', | |
on: { | |
FETCH: 'fetching' | |
} | |
}, | |
fetching: { | |
entry: 'isFetching', | |
invoke: { | |
id: 'repo', | |
src: fetching, | |
onDone: { | |
target: 'success', | |
actions: ['getData'] | |
}, | |
onError: { | |
target: 'failure', | |
actions: ['getError'] | |
} | |
}, | |
on: { | |
CANCEL: 'idle' | |
} | |
}, | |
intervalFetching: { | |
after: { | |
INTERVAL: 'fetching' | |
}, | |
on: { | |
CANCEL: 'idle' | |
} | |
}, | |
success: { | |
entry: 'isNotFetching', | |
after: { | |
0: 'intervalFetching' | |
} | |
}, | |
failure: { | |
entry: 'isNotFetching', | |
on: { | |
RETRY: 'idle' | |
} | |
} | |
} | |
}, | |
{ | |
delays: { | |
INTERVAL: 10000, | |
}, | |
actions: { | |
getData: assign({ | |
data: (context, event) => { | |
console.log('event', event); | |
return event.data | |
} | |
}), | |
getError: assign({ | |
error: (context, event) => event.data | |
}), | |
isNotFetching: assign({ isFetching: false }), | |
isFetching: assign({ isFetching: true }), | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment