Skip to content

Commit

Permalink
update state machines
Browse files Browse the repository at this point in the history
  • Loading branch information
nikgraf committed Apr 23, 2024
1 parent 9fb9e63 commit 0c0cb0e
Show file tree
Hide file tree
Showing 50 changed files with 2,114 additions and 2,639 deletions.
14 changes: 6 additions & 8 deletions apps/app/components/accountMenu/AccountMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ export default function AccountMenu({
const navigation = useNavigation();
const { activeDevice } = useAppContext();
const [state, send] = useMachine(accountMenuMachine, {
context: {
params: { workspaceId, activeDevice },
},
input: { workspaceId, activeDevice },
});
const [isPasswordModalVisible, setIsPasswordModalVisible] = useState(false);
const { workspaces } = workspaceStore.useLocalWorkspaces();
Expand All @@ -70,7 +68,7 @@ export default function AccountMenu({
};

const logoutPreflight = () => {
send("CLOSE");
send({ type: "CLOSE" });
const mainDevice = getMainDevice();
if (mainDevice) {
logout();
Expand Down Expand Up @@ -177,7 +175,7 @@ export default function AccountMenu({
if (activeDevice && isOpen) {
workspaceStore.loadRemoteWorkspaces({ activeDevice });
}
send(isOpen ? "OPEN" : "CLOSE");
send(isOpen ? { type: "OPEN" } : { type: "CLOSE" });
}}
trigger={
<Pressable
Expand Down Expand Up @@ -221,7 +219,7 @@ export default function AccountMenu({
<MenuLink
to={{ screen: "AccountSettings" }}
onPress={(event) => {
send("CLOSE");
send({ type: "CLOSE" });
if (OS === "ios") {
event.preventDefault();
navigation.navigate("AccountSettings");
Expand Down Expand Up @@ -278,7 +276,7 @@ export default function AccountMenu({
<View style={tw`pl-1.5 pr-3 py-1.5`}>
<IconButton
onPress={() => {
send("CLOSE");
send({ type: "CLOSE" });
openCreateWorkspace();
}}
name="plus"
Expand All @@ -289,7 +287,7 @@ export default function AccountMenu({
) : (
<MenuButton
onPress={() => {
send("CLOSE");
send({ type: "CLOSE" });
openCreateWorkspace();
}}
iconName="plus"
Expand Down
265 changes: 133 additions & 132 deletions apps/app/components/accountMenu/accountMenuMachine.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
import { LocalDevice } from "@serenity-tools/common";
import { AnyActorRef, assign, createMachine, spawn } from "xstate";
import { AnyActorRef, assign, setup } from "xstate";
import {
MeQueryResult,
meQueryService,
MeQueryServiceEvent,
MeQueryUpdateResultEvent,
WorkspaceQueryResult,
workspaceQueryService,
WorkspaceQueryServiceEvent,
WorkspaceQueryUpdateResultEvent,
WorkspacesQueryResult,
workspacesQueryService,
WorkspacesQueryServiceEvent,
WorkspacesQueryUpdateResultEvent,
meQueryService,
workspaceQueryService,
workspacesQueryService,
} from "../../generated/graphql";
import { showToast } from "../../utils/toast/showToast";

type Params = {
type Input = {
workspaceId?: string;
activeDevice: LocalDevice | null;
};

interface Context {
params: Params;
params: Input;
meQueryResult?: MeQueryResult;
meQueryError: boolean;
meQueryActor?: AnyActorRef;
Expand All @@ -38,132 +35,136 @@ export const accountMenuMachine =
/** @xstate-layout N4IgpgJg5mDOIC5QEMDGqD2BXAdgFwFkwcsC0ALASxzAGIiBFLMAJwE8A6AVQAUARAIIAVAKIB9AEoiAylwAyQgNoAGALqJQABwyxKeShhwaQbRAFoAHAHYOAFgsBWAGxWHFgIzurLtwBoQAB6IDu5OHMoAzCHOAEwOAL7x-miYuITEpBTUdIzM7BwiEhIA8hIq6kgg2rr6hsamCJY29s6uHl4+Fv5BCCFhkdFOcYnJ6Nj4RCRkqFQ0tADqGCwA1rCaaHBMrJy8gqKSMvJKasbVegZGlQ1Ndo6dnt5t3cGh4VHusQlJICnj6VNZOaLFZrDawLb5QolMonSpnWqXUDXay3VpuB6dZ69V4DD5DL6jVITDLTWZ0YGrdaoMAQnb8YTiKSyBTlU46c51K7mFEte4dJ6BF79d6fEY-MZpSaZGbZBZLSkbWkFIqlVlw9kI+rc5p3NoYgU9PpvQbDb6-SUkwFgDgYTTEWgAYTkxWkIjVWg1F2MPXctjCcSsgacAE4LMpgzFA-4GjEIsoOMGIk53MGPm4LBErMGxebiQCZTQOKgADY6SC0Yo8EQAOXdVU9nNA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rShgS4pEn+pLZEuK5gIBHKIoKjRJjEHBWBEwZDChFiPhGl6RjeTi2JO06xrYMSOBYTizlhUo4YWtrEIRmpbuYMROHunGQXG0GxpethWPGibJqmHa7pm2ZmnO2FWkWpawJAonASRZiSdJB7niek4wSY-ZWNJI5jh2bEzm+QA */
createMachine(
{
schema: {
events: {} as
| MeQueryServiceEvent
| WorkspacesQueryServiceEvent
| WorkspaceQueryServiceEvent
| { type: "OPEN" | "CLOSE" },
context: {} as Context,
},
tsTypes: {} as import("./accountMenuMachine.typegen").Typegen0,
predictableActionArguments: true,
context: {
params: {
activeDevice: null,
},
meQueryError: false,
workspaceQueryError: false,
workspacesQueryError: false,
},
initial: "closed",
on: {
"MeQuery.UPDATE_RESULT": {
actions: [
assign((_, event: MeQueryUpdateResultEvent) => {
return {
meQueryError: false,
meQueryResult: event.result,
};
}),
],
},
"MeQuery.ERROR": {
actions: ["showErrorToast", assign({ meQueryError: true })],
},
"WorkspacesQuery.UPDATE_RESULT": {
actions: [
assign((_, event: WorkspacesQueryUpdateResultEvent) => {
return {
workspacesQueryError: false,
workspacesQueryResult: event.result,
};
}),
],
},
"WorkspacesQuery.ERROR": {
actions: ["showErrorToast", assign({ workspacesQueryError: true })],
},
"WorkspaceQuery.UPDATE_RESULT": {
actions: [
assign((_, event: WorkspaceQueryUpdateResultEvent) => {
return {
workspaceQueryError: false,
workspaceQueryResult: event.result,
};
}),
],
},
"WorkspaceQuery.ERROR": {
actions: ["showErrorToast", assign({ workspaceQueryError: true })],
},
},
states: {
open: {
entry: ["stopActors", "spawnActors"], // respawn to trigger a request
on: { CLOSE: "closed" },
},
closed: {
entry: ["stopActors", "spawnActors"], // respawn to trigger a request
on: { OPEN: "open" },
},
},
id: "accountMenuMachine",
setup({
types: {} as {
context: Context;
input: Input;
events:
| MeQueryServiceEvent
| WorkspacesQueryServiceEvent
| WorkspaceQueryServiceEvent
| { type: "OPEN" | "CLOSE" };
},
actors: {
meQueryService: meQueryService,
workspacesQueryService: workspacesQueryService,
workspaceQueryService: workspaceQueryService,
},
{
actions: {
showErrorToast: (context) => {
// makes sure the error toast is only shown once
if (
!context.meQueryError &&
!context.workspaceQueryError &&
!context.workspacesQueryError
) {
showToast("Failed to load account menu data.", "error");
}
},
spawnActors: assign((context) => {
return {
meQueryActor: spawn(meQueryService({}, 120000)), // poll only every 2 minutes
workspaceQueryActor:
context.params.activeDevice && context.params.workspaceId
? spawn(
workspaceQueryService(
{
id: context.params.workspaceId,
deviceSigningPublicKey:
context.params.activeDevice.signingPublicKey,
},
120000 // poll only every 2 minutes
)
)
: undefined,
workspacesQueryActor: context.params.activeDevice
? spawn(
workspacesQueryService(
{
actions: {
showErrorToast: ({ context }) => {
// makes sure the error toast is only shown once
if (
!context.meQueryError &&
!context.workspaceQueryError &&
!context.workspacesQueryError
) {
showToast("Failed to load account menu data.", "error");
}
},
spawnActors: assign(({ context, spawn }) => {
return {
meQueryActor: spawn("meQueryService", {
input: { variables: {}, intervalInMs: 120000 },
}), // poll only every 2 minutes
workspaceQueryActor:
context.params.activeDevice && context.params.workspaceId
? spawn("workspaceQueryService", {
input: {
variables: {
id: context.params.workspaceId,
deviceSigningPublicKey:
context.params.activeDevice.signingPublicKey,
},
120000 // poll only every 2 minutes
)
)
intervalInMs: 120000, // poll only every 2 minutes
},
})
: undefined,
};
}),
stopActors: (context) => {
if (context.meQueryActor?.stop) {
context.meQueryActor.stop();
}
if (context.workspaceQueryActor?.stop) {
context.workspaceQueryActor.stop();
}
if (context.workspacesQueryActor?.stop) {
context.workspacesQueryActor.stop();
}
},
workspacesQueryActor: context.params.activeDevice
? spawn("workspacesQueryService", {
input: {
variables: {
deviceSigningPublicKey:
context.params.activeDevice.signingPublicKey,
},
intervalInMs: 120000, // poll only every 2 minutes
},
})
: undefined,
};
}),
stopActors: ({ context }) => {
if (context.meQueryActor?.stop) {
context.meQueryActor.stop();
}
if (context.workspaceQueryActor?.stop) {
context.workspaceQueryActor.stop();
}
if (context.workspacesQueryActor?.stop) {
context.workspacesQueryActor.stop();
}
},
}
);
},
}).createMachine({
context: ({ input }) => ({
params: {
activeDevice: input.activeDevice,
workspaceId: input.workspaceId,
},
meQueryError: false,
workspaceQueryError: false,
workspacesQueryError: false,
}),
initial: "closed",
on: {
"MeQuery.UPDATE_RESULT": {
actions: [
assign(({ event }) => {
return {
meQueryError: false,
meQueryResult: event.result,
};
}),
],
},
"MeQuery.ERROR": {
actions: ["showErrorToast", assign({ meQueryError: true })],
},
"WorkspacesQuery.UPDATE_RESULT": {
actions: [
assign(({ event }) => {
return {
workspacesQueryError: false,
workspacesQueryResult: event.result,
};
}),
],
},
"WorkspacesQuery.ERROR": {
actions: ["showErrorToast", assign({ workspacesQueryError: true })],
},
"WorkspaceQuery.UPDATE_RESULT": {
actions: [
assign(({ event }) => {
return {
workspaceQueryError: false,
workspaceQueryResult: event.result,
};
}),
],
},
"WorkspaceQuery.ERROR": {
actions: ["showErrorToast", assign({ workspaceQueryError: true })],
},
},
states: {
open: {
entry: ["stopActors", "spawnActors"], // respawn to trigger a request
on: { CLOSE: "closed" },
},
closed: {
entry: ["stopActors", "spawnActors"], // respawn to trigger a request
on: { OPEN: "open" },
},
},
id: "accountMenuMachine",
});
28 changes: 0 additions & 28 deletions apps/app/components/accountMenu/accountMenuMachine.typegen.ts

This file was deleted.

Loading

0 comments on commit 0c0cb0e

Please sign in to comment.