Last active
September 8, 2019 16:44
-
-
Save erickeno/76a22bf50997dd82c9494a683982a879 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 (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 | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment