Last active
December 20, 2020 08:11
-
-
Save massimoselvi/a3768c794e1d159d1740225d0899c43c 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
/* | |
* FETCH MACHINE bis (work in progress) | |
* preview: https://xstate.js.org/viz/?gist=a3768c794e1d159d1740225d0899c43c | |
* previous: https://xstate.js.org/viz/?gist=0291cafd5f583c165a3ef1dd35bf02af | |
*/ | |
// Available variables: | |
// Machine (machine factory function) | |
// assign (action) | |
// XState (all XState exports) | |
const fetchMachine = Machine({ | |
id: 'fetch', | |
context: { attempts: 0 }, | |
initial: 'idle', | |
states: { | |
idle: { | |
on: { | |
FETCH: { | |
target: 'pending', | |
cond: function canFetch() { | |
return true | |
}, | |
} | |
}, | |
}, | |
pending: { | |
entry: assign({ | |
attempts: ctx => ctx.attempts + 1 | |
}), | |
after: { | |
TIMEOUT: 'rejected' | |
}, | |
on: { | |
RESOLVE: 'fulfilled', | |
REJECT: 'rejected' | |
} | |
}, | |
fulfilled: { | |
initial: 'first', | |
states: { | |
first: { | |
on: { | |
NEXT: 'second' | |
} | |
}, | |
second: { | |
on: { | |
NEXT: 'third' | |
} | |
}, | |
third: { | |
type: 'final' | |
} | |
} | |
}, | |
rejected: { | |
entry: assign({ | |
ref: () => spawn(Machine({ initial: 'foo', states: {foo: {}}})) | |
}), | |
initial: 'can retry', | |
states: { | |
'can retry': { | |
on: { | |
'': { | |
target: 'failure', | |
cond: 'maxAttempts' | |
} | |
} | |
}, | |
failure: { | |
on: { | |
RETRY: undefined, | |
}, | |
type: 'final' | |
} | |
}, | |
on: { | |
RETRY: 'pending' | |
} | |
} | |
} | |
}, { | |
guards: { | |
maxAttempts: ctx => ctx.attempts >= 5 | |
}, | |
delays: { | |
TIMEOUT: 2000 | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment