Skip to content

Instantly share code, notes, and snippets.

@UberMouse
Created May 19, 2020 03:15
Show Gist options
  • Save UberMouse/aee4834675e9bec256916a470d5d90d2 to your computer and use it in GitHub Desktop.
Save UberMouse/aee4834675e9bec256916a470d5d90d2 to your computer and use it in GitHub Desktop.
type Events =
| { type: "LOG_IN" }
| { type: "LOG_OUT" }
| { type: "CHECK_TOKEN_VALID" }
| { type: "CHECK_TOKEN_INVALID" }
| { type: "AUTHORIZE_OK" }
| { type: "AUTHORIZE_DENIED" };
const machine = createMachine<{}, Events>({
id: "main-auth-spec",
initial: "checkToken",
states: {
checkToken: {
on: {
CHECK_TOKEN_VALID: "loggedIn",
CHECK_TOKEN_INVALID: "loggedOut",
},
},
loggedOut: {
on: {
LOG_IN: "loggingIn",
},
meta: {
test: () => {},
},
},
loggingIn: {
on: {
AUTHORIZE_OK: "loggedIn",
AUTHORIZE_DENIED: "loggedOut",
},
meta: {
test: () => {},
},
},
loggedIn: {
on: {
LOG_OUT: "loggedOut",
},
meta: {
test: () => {},
},
},
},
});
const testModel = createModel<typeof testMachine>(machine).withEvents({
LOG_IN: {
exec: (service) => {
service.send({ type: "LOG_IN" });
},
},
LOG_OUT: {
exec: (service) => {
service.send({ type: "LOG_OUT" });
},
},
CHECK_TOKEN_VALID: {
exec: () => void 0,
},
CHECK_TOKEN_INVALID: {
exec: () => void 0,
},
AUTHORIZE_OK: {
exec: async () => await timeout(),
},
AUTHORIZE_DENIED: {
exec: () => void 0,
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment