Skip to content

Instantly share code, notes, and snippets.

@njdancer
Last active October 10, 2019 23:35
Show Gist options
  • Save njdancer/79e1430db6f76b0a1c94cd1d53a8097b to your computer and use it in GitHub Desktop.
Save njdancer/79e1430db6f76b0a1c94cd1d53a8097b to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const registerDevice = () => new Promise((resolve,reject) => {
resolve("deviceIdTest")
})
const getPushToken = () => new Promise((resolve,reject) => {
resolve("pushTokenTest")
})
const onboardingMachine = Machine(
{
context: {
desiredInstructions: 1,
desiredTermsAndConditions: 1,
},
id: "onboarding",
initial: "check",
states: {
check: {
on: {
"": [
{
cond: "isTermsAndConditionsNeeded",
target: "termsAndConditions",
},
{ cond: "isInstructionsNeeded", target: "instructions" },
{ target: "locationServices" },
],
},
},
complete: {
data: {
deviceId: (context, event) => context.deviceId,
instructions: (context, event) => context.instructions,
termsAndConditions: (context, event) => context.termsAndConditions,
},
type: "final",
},
instructions: {
entry: "navigateToInstructions",
on: {
AGREE: {
actions: ["assignInstructions"],
target: "check",
},
},
},
locationServices: {
invoke: {
id: "requestLocationServicesPermission",
onDone: "pushNotifications",
onError: {
actions: [
"handleLocationServicesPermissionDenied",
sendParent("AUTH.RESET"),
],
},
src: "requestLocationServicesPermission",
},
},
pushNotifications: {
initial: "checkDeviceId",
onDone: "complete",
states: {
checkDeviceId: {
on: {
"": [
{ cond: "isDeviceIdSet", target: "permission" },
{ target: "registration" },
],
},
},
permission: {
invoke: {
id: "requestPushNotificationsPermission",
onDone: { target: "pushToken" },
onError: {
actions: [
"handlePushNotificationsPermissionDenied",
sendParent("AUTH.RESET"),
],
},
src: "requestPushNotificationsPermission",
},
},
pushToken: {
invoke: {
id: "pushToken",
onDone: {
// actions: ["assignPushToken"],
target: "updateRegistration",
},
onError: "registrationFailed",
src: "getPushToken",
},
},
registered: {
type: "final",
},
registration: {
invoke: {
id: "registerDevice",
onDone: {
actions: ["assignDeviceId"],
target: "permission",
},
onError: {
actions: [
"handleRegisterDeviceError",
sendParent("AUTH.RESET"),
],
},
src: "registerDevice",
},
},
registrationFailed: {
entry: [
actions.log(
(context, event) => ({ context, event }),
"RegistrationFailed"
),
sendParent("AUTH.RESET"),
],
},
updateRegistration: {
exit: actions.log(
(context, event) => event.data.response,
"updateRegistrationExit"
),
invoke: {
id: "updateDeviceRegistration",
onDone: "registered",
onError: "registrationFailed",
src: "updateDeviceRegistration",
},
},
},
},
termsAndConditions: {
entry: "navigateToTermsAndConditions",
on: {
AGREE: {
actions: ["assignTermsAndConditions"],
target: "check",
},
},
},
},
},
{
actions: {
assignDeviceId: assign({
deviceId: (context, event) => event.data.data.deviceId,
}),
assignInstructions: assign({
instructions: (context, event) => context.desiredInstructions,
}),
assignTermsAndConditions: assign({
termsAndConditions: (context, event) =>
context.desiredTermsAndConditions,
}),
},
guards: {
isDeviceIdSet: (context, event) => context.deviceId != null,
isInstructionsNeeded: (context, event) =>
context.instructions == null ||
context.instructions < context.desiredInstructions,
isTermsAndConditionsNeeded: (context, event) =>
context.termsAndConditions == null ||
context.termsAndConditions < context.desiredTermsAndConditions,
},
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment