Skip to content

Instantly share code, notes, and snippets.

@vinhlee95
Created June 7, 2020 12:24
Show Gist options
  • Save vinhlee95/dee676975a7a022f233244a50eb0c75f to your computer and use it in GitHub Desktop.
Save vinhlee95/dee676975a7a022f233244a50eb0c75f to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
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