Created
July 15, 2019 22:30
-
-
Save Andarist/5b83bfc720bb12a53fd8910ec5b81313 to your computer and use it in GitHub Desktop.
Generated by [XState Viz](https://xstate.js.org/viz)
This file contains 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: 'rejected', | |
cond: function canFetch() { | |
return false | |
}, | |
}, { | |
target: 'pending', | |
cond: function canFetch() { | |
return true | |
}, | |
}, { | |
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