Skip to content

Instantly share code, notes, and snippets.

@njdancer
Last active October 12, 2019 07:38
Show Gist options
  • Save njdancer/34eee0e464561c464ff201bc1a68dbeb to your computer and use it in GitHub Desktop.
Save njdancer/34eee0e464561c464ff201bc1a68dbeb to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const systemMachine = Machine({
id: "system",
context: {},
initial: "loading",
states: {
loading: {
id: "loadSystemData",
src: "loadSystemData",
onDone: {
target: "configure",
actions: "assignSystemData"
}
},
configure: {
id: "configureSystem",
src: "configureSystem",
onDone: "checkPermissions"
},
idle: {
entry: "persistSystemData",
on: {
CHECK: "checkPermissions"
}
},
checkPermissions: {
initial: "locationServices",
states: {
failed: {
entry: "sendParentFail",
on: {
"": "#system.idle"
}
},
locationServices: {
invoke: {
id: "requestLocationServicesPermission",
onDone: "pushNotifications",
onError: {
actions: [
"handleLocationServicesPermissionDenied",
"sendParentError",
],
target: "#system.checkPermissions.failed"
},
src: "requestLocationServicesPermission",
},
},
pushNotifications: {
initial: "checkDeviceId",
onDone: "#system.idle",
states: {
checkDeviceId: {
on: {
"": [
{ cond: "isDeviceIdSet", target: "permission" },
{ target: "registration" },
],
},
},
notRegistered: {
type: "final",
},
permission: {
invoke: {
id: "requestPushNotificationsPermission",
onDone: { target: "pushToken" },
onError: {
target: "registrationFailed",
actions: ["handlePushNotificationsPermissionDenied"],
},
src: "requestPushNotificationsPermission",
},
},
pushToken: {
invoke: {
id: "pushToken",
onDone: {
target: "updateRegistration",
},
onError: {
actions: ["handlePushTokenError"],
target: "notRegistered",
},
src: "getPushToken",
},
},
registered: {
type: "final",
},
registration: {
invoke: {
id: "registerDevice",
onDone: {
actions: ["assignDeviceId"],
target: "permission",
},
onError: {
actions: ["handleRegisterDeviceError"],
target: "notRegistered",
},
src: "registerDevice",
},
},
registrationFailed: {
on: {
"": "#system.checkPermissions.failed"
}
},
updateRegistration: {
invoke: {
id: "updateDeviceRegistration",
onDone: "registered",
onError: {
actions: ["handleUpdateDeviceRegistrationError"],
target: "notRegistered",
},
src: "updateDeviceRegistration",
},
},
},
},
},
},
}
}, {
guards: {
isDeviceIdSet: (context, event) => context.deviceId != null,
},
actions: {
sendParentError: (context, event) => sendParent({...event, type:"ERROR"}),
sendParentFail: sendParent("FAIL")
}
})
// const termsAndConditionsVersion = 1
// const instructionsVersion = 1
// const onboardingMachine = Machine({
// id: "onboarding",
// initial: "loading",
// context: {},
// states: {
// loading: {
// invoke: {
// id: "loadOnboardingData",
// src: "loadOnboardingData",
// onDone: {
// target:"check",
// actions: "assignOnboardingData"
// },
// onError: "check"
// }
// },
// check: {
// on: {
// "": [
// {
// cond: "isTermsAndConditionsNeeded",
// target: "termsAndConditions",
// },
// { cond: "isInstructionsNeeded", target: "instructions" },
// { target: "complete" }
// ],
// },
// },
// instructions: {
// entry: "navigateToInstructions",
// on: {
// AGREE: {
// actions: ["markInstructionsComplete"],
// target: "check",
// },
// },
// },
// termsAndConditions: {
// entry: "navigateToTermsAndConditions",
// on: {
// AGREE: {
// actions: ["markTermsAndConditionsComplete"],
// target: "check",
// },
// },
// },
// complete: {
// entry: "persistOnboardingData",
// type: "final"
// }
// }
// }, {
// guards: {
// isInstructionsNeeded: (context, event) =>
// context.instructions == null ||
// context.instructions < instructionsVersion,
// isTermsAndConditionsNeeded: (context, event) =>
// context.termsAndConditions == null ||
// context.termsAndConditions < termsAndConditionsVersion,
// },
// actions: {
// markInstructionsComplete: assign({ instructions: instructionsVersion }),
// markTermsAndConditionsComplete: assign({ termsAndConditions: termsAndConditionsVersion }),
// }
// })
// const sessionMachine = Machine({
// context: {
// organisationId: "sos2us",
// autoCheck: false
// },
// id: "session",
// initial: "loading",
// on: {
// RESET: {
// target: "null",
// actions: "resetSession"
// }
// },
// states: {
// loading: {
// invoke: {
// id: "loadSession",
// src: "loadSession",
// onDone: [{
// cond: "shouldAutoCheck",
// target: "autoCheck"
// },
// {
// target: "loaded",
// actions: "assignSession"
// }],
// },
// on: {
// CHECK: {
// actions: "setAutoCheck"
// }
// }
// },
// autoCheck: {
// on: {
// "": [
// {
// cond: "sessionExists",
// target: "loaded",
// actions: "sendParentSession"
// },
// {
// target: "loaded",
// actions: "sendParentNull"
// }
// ]
// }
// },
// loaded: {
// on: {
// "": [
// {
// cond: "sessionExists",
// target: "session",
// },
// {
// target: "null"
// }
// ]
// }
// },
// session: {
// initial: "idle",
// on: {
// CHECK: {
// actions: "sendParentSession"
// }
// },
// states: {
// idle: {
// entry: ["sendParentAccessToken", "persistSession"],
// on: {
// REFRESH: "refreshToken"
// }
// },
// refreshToken: {
// invoke: {
// id: "refreshToken",
// src: "refreshToken",
// onDone: {
// target: "idle",
// actions: "assignAuthData"
// },
// onError: "#session.null"
// }
// }
// }
// },
// "null": {
// on: {
// AUTH: {
// target: "auth",
// cond: "organisationIdRequired"
// },
// CHECK: {
// actions: "sendParentNull"
// }
// }
// },
// auth: {
// on: {
// CHECK: {
// actions: "sendParentNull"
// }
// },
// invoke: {
// id: "auth",
// src: "auth",
// onDone: "session",
// onError: "null"
// }
// }
// }
// }, {
// guards: {
// sessionExists: (context, event) => context.authData != null,
// organisationIdRequired: (context, event) => event.organisationId != null,
// shouldAutoCheck: (context, event) => context.autoCheck
// },
// actions: {
// sendParentAccessToken:
// (context, event) => sendParent({
// type: "TOKEN",
// accessToken: context.authData.accessToken
// }),
// sendParentSession:
// ({authData, organisationId}, event) => sendParent({
// type: "SESSION",
// authData, organisationId
// }),
// sendParentNull:
// (context, event) => sendParent("NULL"),
// sendParentLoaded:
// (context, event) => sendParent("LOADED.SESSION"),
// setAutoCheck: assign({ autoCheck: true })
// }
// })
// const rootMachine = Machine({
// context: {
// defaults: {
// organisationId: "sos2us",
// },
// },
// id: "root",
// initial: "splashScreen",
// invoke: [
// {
// id: "session",
// src: "sessionMachine",
// },
// {
// id: "system",
// src: "systemMachine",
// },
// ],
// states: {
// auth: {
// entry: "navigateToCredentials",
// initial: "credentials",
// states: {
// credentials: {
// on: {
// ORG_USER: {
// actions: "assignOrganisationId",
// target: "submitted",
// },
// SINGLE_USER: {
// actions: "defaultOrganisationId",
// target: "submitted",
// },
// },
// },
// submitted: {
// entry: "sendSessionAuth",
// on: {
// FAIL: {
// target: "credentials",
// },
// SUCCESS: {
// target: "#root.main",
// },
// },
// },
// },
// },
// checkSession: {
// entry: "sendSessionCheck",
// on: {
// NULL: "auth",
// SESSION: "main",
// },
// },
// loading: {
// invoke: {
// id: "loading",
// onDone: "checkSession",
// src: "loadApplication",
// },
// },
// main: {
// initial: "onboarding",
// invoke: [
// {
// id: "main",
// src: "mainMachine",
// },
// ],
// on: {
// RESET: {
// target: ".reset",
// },
// },
// states: {
// background: {
// on: {
// RESUME: {
// actions: "sendDataRefresh",
// target: "checkSystem",
// },
// },
// },
// checkSystem: {
// entry: "sendSystemCheck",
// on: {
// FAIL: "reset",
// SUCCESS: "running",
// },
// },
// onboarding: {
// invoke: {
// id: "onboarding",
// onDone: {
// actions: "sendMainNavigate",
// target: "checkSystem",
// },
// onError: "reset",
// src: "onboarding",
// },
// },
// reset: {
// on: {
// "": {
// actions: "sendSessionReset",
// target: "#root.auth",
// },
// },
// },
// running: {
// on: {
// BACKGROUND: "background",
// },
// },
// },
// },
// splashScreen: {
// invoke: {
// id: "splashScreen",
// onDone: "loading",
// src: "presentSplashScreen",
// },
// },
// },
// },{
// guards: {
// assignOrganisationId: assign({
// organisationId: (context, event) => event.organisationId,
// }),
// defaultOrganisationId: assign({
// organisationId: (context, event) =>
// context.defaults.OrganisationId,
// })
// },
// actions: {
// sendSessionAuth: (context, event) =>
// send(
// { organisationId: context.organisationId, type: "AUTH" },
// { to: "session" }
// ),
// sendSessionCheck: send("CHECK", { to: "session" }),
// sendDataRefresh: send("REFRESH", { to: "data" }),
// sendSystemCheck: send("CHECK", { to: "system" }),
// sendMainNavigate: send("NAVIGATE", { to: "main" }),
// sendSessionReset: send("RESET", { to: "session" })
// }
// })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment