Skip to content

Commit

Permalink
✨ (poc): Add userActionNeeded in context and add more logs on machine
Browse files Browse the repository at this point in the history
  • Loading branch information
ofreyssinet-ledger committed Jun 4, 2024
1 parent 568933b commit c761362
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
16 changes: 15 additions & 1 deletion pocs/xstate-study/app/Machine.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";
import { memo } from "react";
import makeOpenAppMachine from "./openAppStateMachine";
import makeOpenAppMachine, { Context } from "./openAppStateMachine";
import { createActor } from "xstate";
import { createBrowserInspector } from "@statelyai/inspect";

Expand All @@ -22,8 +22,22 @@ function Machine() {
},
});

let previousUserActionNeeded: Context["userActionNeeded"] = null;
let previousStateValue: string | null = null;

openAppMachineActor.start();
openAppMachineActor.subscribe((state) => {
if (state.value !== previousStateValue) {
console.log("machine: new state", state.value);
previousStateValue = state.value;
}
if (state.context.userActionNeeded !== previousUserActionNeeded) {
console.log(
"machine: user action needed",
state.context.userActionNeeded
);
previousUserActionNeeded = state.context.userActionNeeded;
}
if (state.status === "done") {
console.log("machine: done", state);
console.log("machine output:", state.output);
Expand Down
40 changes: 23 additions & 17 deletions pocs/xstate-study/app/openAppStateMachine.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import { setup, fromPromise, assign, log } from "xstate";


type DeviceSessionState = {
status: "Ready" | "Busy" | "Locked" | "NotConnected",
deviceOnboarded: boolean,
};

type Input = {
requestedAppName: string,
deviceSessionState: DeviceSessionState,
}

type Context = Input & {
currentlyRunningApp: string | null;
error: Error | null;
}

export const LOCKED_DEVICE_ERROR = "Locked device";
export const APP_NOT_FOUND_ERROR = "App not installed";
export const OPEN_REJECTED_ERROR = "Open app rejected";
Expand Down Expand Up @@ -47,6 +32,22 @@ async function openAppStub(appName: string) {
return;
}

type DeviceSessionState = {
status: "Ready" | "Busy" | "Locked" | "NotConnected",
deviceOnboarded: boolean,
};

type Input = {
requestedAppName: string,
deviceSessionState: DeviceSessionState,
}

export type Context = Input & {
currentlyRunningApp: string | null;
error: Error | null;
userActionNeeded: "OpenApp" | null;
}

function makeOpenAppMachine(params: {
getAppAndVersion: () => Promise<{ app: string, version: string }>,
closeApp: () => Promise<void>,
Expand Down Expand Up @@ -81,17 +82,20 @@ function makeOpenAppMachine(params: {
},
actions: {
assignErrorDeviceNotOnboarded: assign({error: new Error(DEVICE_NOT_ONBOARDED_ERROR)}),
assignErrorDeviceLocked: assign({error: new Error(LOCKED_DEVICE_ERROR)})
assignErrorDeviceLocked: assign({error: new Error(LOCKED_DEVICE_ERROR)}),
assignUserActionNeededOpenApp: assign({userActionNeeded: "OpenApp"}),
assignNoUserActionNeeded: assign({userActionNeeded: null}),
},
}).createMachine({
/** @xstate-layout N4IgpgJg5mDOIC5QHkAOYB2BBVqAiYAbgJYDGYWpALsQPYYB0BJ5ASmAIYQCeAxANoAGALqJQqWrGI16YkAA9EAdgCMDAGyCVAVm3qAzAA4AnNq3bDAGhDdEKgCyGGpw4OMAmffbP37xgL7+1miYOPhEZBTUdIzIGABGtBwAThDEGFAAwgAWYKQA1gIichJSMhhyiggqggzugvbq2qr6Su7aKvr6Kta2CO5tGu7GhvrDKq4jxvaBwejYuMyRlOUMcYkpaRk5eYX8KqJIIKXSMZWI7uruDIYqKu53FtrGbYb2vRd+zg1vDuo67kasxAIQW4RYUVWABlaAV0llcgUiodxJJTrIjlUdAx7IJ9II2vj2ipjHjtB8EPpSTdLqY3B0Bn4lMDQWEluQVjEGDC4dtEXsDiU0eVzv0rjdtK1cQZLrjTBT9OonDUSZozAN7u0WfM2REOdF6AwwgAbMgccpYQgcYjGjjxY1gXgQehgBjpQi0fKu1mLPWQrkms0Wq02u0OhDu2HmmJCYSxoVlM6Y5QMQQPQzqdTGToWLOCck2RBNJQMZ4EwQNfNKSX6bWhX0QzmGwOkaP0S3W232x1gZLJWjJBioW1UABmA4AtmsdQ3lgbGC229gQ13w5HW+VY-GjicRcmxdpU117F5DO4TJLdBTLwx7lnGh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8NG4oY-GzqZXKZMakgUF+QYxE6LpuhgHpetO9YmR+jBRTFBExBGKVRpuIgAaRQGyRRCCOE4VIPEqtGKh5BZ9HcJgMKoHQVi0GYnmFmVNtl0WwLFw7xfQvC9v2g7Dua47JFOxnvgNDA5cNeX0AVHo-vQW7FPZ5Wiq8pZKpxSoTHiEw+f87U6F0hi6Xo1a1kZOFLUJxnfkuiUYK6kZpYtfrLR9Y0YJtRUxiVe2ATJooDNczzQaoEH3G06nePYClqnodxGAYL4vRlb2rMD20YBNfYDkOI5zQtr2A+9OGfeUYOk7tJG7uRTkIP8GOcYxiEElmJhWCxnT3HUwwNAZShXDMBNvvT+FxaT7BcHwpUc45VSqBo5j2IShiIbpzEtZoJbtEoNE5sYxjqL1wIYLQEBwHIAOCXu0Me1zAC02gY0oeL1BmdtaPm7jqRo6iqNWfjngMzzqH1RNcu+qs8NJXtVIxCFODL9LaO0HgPC8SeK1y6xWVsCK7BnnNVJ4OJvF4ZucW80wUg81znjoNEFxpzSl+7XI8vk8JEbXWuIK0paefougXhMSgKvidS6QFJ4ZpvoXywJeEBsrS4dqG3YTxVPtaQHYynSHNQdCLfSStcbyG5pniW-iz1zITZefqNpPj-tGGckMYeEmOeVoBhdL5gpEoVwNw0z-FaAHCYAVB570NOZUS4kIAAM9nXC44orhZlUICDyZ4KTTDUFRP2VEDBz1MJ-bC38h6GlWiNJmnNNZnyzqMdqrRba228ASdwAwfKDAzCIh4x43Bah3uFLK6UwQcMclw2GFhSw6D0FHUkNstDqWGOxe4FYCSuC6phL+CsWELgPuUNOfQ8GT36IeAuXg76MRPH7TS6ldC1FMFRTilsDCyzQRFQ0ABlAArqQcgsB4CAMzogXEkdpT82Ds8d4osGJ1DutHeo9RtAhIUQAUQpskU+opSQljTOMLM4CtDGHUlkzw-COg4xJNvQIQA */
id: "OpenAppDeviceAction",
initial: "DeviceReady",
context: ({input}): Context => {
return {
...input,
currentlyRunningApp: null,
error: null,
userActionNeeded: null,
}
},
states: {
Expand Down Expand Up @@ -176,6 +180,8 @@ function makeOpenAppMachine(params: {
},

OpenApplication: { // execute openApp command,
entry: "assignUserActionNeededOpenApp",
exit: "assignNoUserActionNeeded",
invoke: {
src: "openApp",
input: ({context}) => ({appName: context.requestedAppName}),
Expand Down

0 comments on commit c761362

Please sign in to comment.