Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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: 'pending',
- },
- type: 'final'
- }
- },
- on: {
- RETRY: 'pending'
- }
- }
- }
- }, {
- guards: {
- maxAttempts: ctx => ctx.attempts >= 5
- },
- delays: {
- TIMEOUT: 2000
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement