Skip to content

Instantly share code, notes, and snippets.

@filipemonteiroth
Last active February 14, 2020 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filipemonteiroth/3e49ee23d190bc04fe6b045542ae20fe to your computer and use it in GitHub Desktop.
Save filipemonteiroth/3e49ee23d190bc04fe6b045542ae20fe to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const fetchMachine = Machine({
id: 'userCacheMachine',
type: 'parallel',
context: {
hasProcessedCache: false,
cacheData: null,
userId: null,
onCompleteUpload: null,
onFailCacheProcessing: null,
onFinishCacheProcessing: null,
},
states: {
upload: {
initial: 'inactive',
states: {
inactive: {
id: '#inactive',
on: {
REQUESTED_TO_START: 'active',
},
},
active: {
initial: 'idle',
on: {
REQUESTED_TO_STOP: '#inactive',
REQUESTED_UPLOAD: [
{
cond: 'canUploadCache',
target: '#uploadCache',
actions: ['assignOnComplete'],
},
{
target: '#idle',
},
],
},
states: {
idle: {
id: 'idle',
after: {
60000: [
{
cond: 'canUploadCache',
target: 'uploadCache',
},
{target: 'idle'},
],
},
},
uploadCache: {
id: 'uploadCache',
invoke: {
id: 'uploadUserCache',
src: 'uploadUserCache',
onDone: {
target: 'idle',
actions: ['notifyUploadComplete'],
},
onError: 'idle',
},
},
},
},
},
},
manager: {
initial: 'inactive',
on: {
REQUESTED_TO_STOP: {
target: '#inactive',
actions: [
'assignCacheAsNotProcessed',
'clearUser',
'clearLocalCache',
],
},
},
states: {
inactive: {
id: 'inactive',
on: {
REQUESTED_TO_START: {target: 'active', actions: ['assignUserId']},
},
},
active: {
initial: 'boot',
on: {
REQUESTED_TO_LOAD_CACHE: {
target: '#fetchingCache',
actions: ['assignFetchCacheCallbacks'],
},
},
states: {
boot: {
after: {
// Give sometime to machine to know if user will request to load cache or not.
// When user is already logged in, we won't receive that event.
500: [
{
cond: 'shouldMigrateLocalData',
target: 'migrateLocalData',
},
{target: 'processCacheData'},
],
},
},
fetchingCache: {
id: 'fetchingCache',
invoke: {
id: 'fetchCache',
src: 'fetchCache',
onDone: {
target: 'transientFetchingCache',
actions: ['assignData', 'notifyFetchCacheSuccess'],
},
onError: {
target: '#inactive',
actions: ['notifyFetchCacheFailure'],
},
},
},
//That is a transient state to help defining if we should migrate the context
//Or go straight to process cache
transientFetchingCache: {
on: {
'': [
{
cond: 'shouldMigrateContextData',
target: 'migrateContextData',
},
{
actions: ['persistData'],
target: 'processCacheData',
},
],
},
},
// When user is already logged in and received a new version update
// we should read data from the local storage instead of going to user context
migrateLocalData: {
invoke: {
id: 'migrateLocalData',
src: 'migrateLocalData',
onDone: {
target: 'processCacheData',
},
onError: {
target: 'processCacheData',
},
},
},
migrateContextData: {
invoke: {
id: 'migrateContextData',
src: 'migrateContextData',
onDone: {
target: 'processCacheData',
},
onError: {target: 'processCacheData'},
},
},
processCacheData: {
invoke: {
id: 'processCacheData',
src: 'processCacheData',
onDone: {target: 'ready'},
onError: {target: 'ready'},
},
},
ready: {
onEntry: ['setMainCacheAsMigrated', 'assignCacheAsProcessed'],
on: {
REQUESTED_CACHED_KEY_UPDATE: {
actions: ['updateCachedKey'],
},
CLEAR_CACHED_KEY: {
actions: ['clearCachedKey'],
},
},
},
},
},
},
},
},
},
{
guards: {
shouldMigrateLocalData: true
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment