Last active
March 8, 2020 11:15
-
-
Save arekmaz/17dd1cf51479aeee821ccfe89a138f3c 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 fetchMachine = Machine({ | |
id: 'fetch', | |
initial: 'gettingFromCache', | |
context: { | |
fetchTimes: 0, | |
fetchRetryCount: 4 | |
}, | |
states: { | |
gettingFromCache: { | |
on: { | |
cacheReadError: [ | |
{ | |
target: 'error', | |
cond: context => context.fetchTimes > 0 | |
}, | |
{ | |
target: 'fetching' | |
} | |
], | |
cacheReadSuccess: 'success' | |
} | |
}, | |
fetching: { | |
on: { | |
fetchSuccess: { | |
target: 'writingToCache', | |
actions: assign({ | |
fetchTimes: (context, event) => context.fetchTimes + 1 | |
}) | |
}, | |
fetchError: [ | |
{ | |
target: 'error', | |
cond: context => context.fetchTimes >= context.fetchRetryCount, | |
actions: assign({ | |
fetchTimes: (context, event) => context.fetchTimes + 1 | |
}) | |
}, | |
{ | |
target: 'fetching', | |
actions: assign({ | |
fetchTimes: (context, event) => context.fetchTimes + 1 | |
}) | |
} | |
] | |
} | |
}, | |
writingToCache: { | |
on: { | |
cacheWritingSuccess: 'gettingFromCache', | |
cacheWritingError: 'error' | |
} | |
}, | |
success: { | |
type: 'final' | |
}, | |
error: { | |
type: 'final' | |
} | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment