Skip to content

Instantly share code, notes, and snippets.

@cogell
Created October 3, 2019 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cogell/24e324463c03f9f92dcd494d6fb3f366 to your computer and use it in GitHub Desktop.
Save cogell/24e324463c03f9f92dcd494d6fb3f366 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const s = {
idle: "idle",
loading: "loading",
loggedIn: "loggedIn",
error: "error"
};
// event types
const e = {
login: "login",
logout: "logout",
success: "success",
error: "error"
};
// private context
const initContext = {
name: null,
dogPerson: null,
catPerson: null
};
const machine = Machine({
id: "userMachine",
initial: [s.idle],
context: initContext,
states: {
[s.idle]: {
on: {
login: s.loading
}
},
[s.loading]: {
invoke: {
id: "authUser",
src: "authUser",
onDone: {
target: s.loggedIn,
actions: assign({
name: (_, e) => e.data.name,
dogPerson: (_, e) => e.data.dogPerson,
catPerson: (_, e) => e.data.catPerson
})
},
onError: {
target: e.error,
actions: (_, e) => {
// log
console.log("error", e);
}
}
},
on: {
[e.success]: s.loggedIn,
[e.error]: s.error
}
},
[s.loggedIn]: {
on: {
[e.logout]: s.idle
}
},
[s.error]: {}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment