Created
June 5, 2020 15:31
-
-
Save pipex/2e87e3f8fb468c9eae87c8e6bd1a11e7 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
// Function that returns a promise | |
// This promise might resolve with, e.g., | |
// { name: 'David', location: 'Florida' } | |
const doApiAction = deviceId => | |
fetch(`url/to/device/${deviceId}`).then(response => response.json()); | |
const machine = Machine({ | |
id: 'device', | |
initial: 'current', | |
context: { | |
deviceId: 42, | |
result: false, | |
error: false | |
}, | |
states: { | |
current: { | |
on: { | |
ACTION: 'waiting' | |
} | |
}, | |
waiting: { | |
invoke: { | |
id: 'doAction', | |
src: (context, event) => doApiAction(context.deviceId), | |
onDone: { | |
target: 'target', | |
actions: assign({ result: (context, event) => event.data }) | |
}, | |
onError: { | |
target: 'current', | |
actions: assign({ error: (context, event) => event.data }) | |
} | |
} | |
}, | |
target: {}, | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment