Created
June 7, 2020 12:24
-
-
Save vinhlee95/dee676975a7a022f233244a50eb0c75f 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
const state = { | |
idle: "idle", | |
fetching: "fetching", | |
success: "success", | |
error: "error" | |
}; | |
const todoActions = { | |
fetchTodos: "fetchTodos", | |
cancelFetching: "cancelFetching", | |
disableFetching: "disableFetching", | |
enableFetching: "enableFetching" | |
}; | |
Machine({ | |
id: "fetchTodos", | |
initial: state.idle, | |
context: { | |
canFetch: true, | |
data: [], | |
error: null | |
}, | |
states: { | |
idle: { | |
on: { | |
[todoActions.fetchTodos]: { | |
target: state.fetching, // next target | |
cond: "canFetch" // condition to move to the next target state | |
}, | |
[todoActions.disableFetching]: { | |
actions: "disableFetching" | |
}, | |
[todoActions.enableFetching]: { | |
actions: "enableFetching" | |
} | |
} | |
}, | |
fetching: { | |
on: { | |
[todoActions.cancelFetching]: state.idle | |
}, | |
// Invoke service | |
invoke: { | |
src: "fetchTodos", // source event to invoke | |
onDone: { | |
target: state.success, | |
actions: "saveTodos" | |
}, | |
onError: { | |
target: state.error, | |
actions: "handleError" | |
} | |
} | |
}, | |
success: { | |
on: { | |
[todoActions.fetchTodos]: state.fetching | |
} | |
}, | |
error: { | |
on: { | |
[todoActions.fetchTodos]: state.fetching | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment