Files
modeling-app/src/machines/systemIO/systemIOMachine.ts

121 lines
4.0 KiB
TypeScript
Raw Normal View History

2025-04-09 09:46:30 -05:00
import { DEFAULT_PROJECT_NAME } from '@src/lib/constants'
import type { Project } from '@src/lib/project'
2025-04-14 11:56:28 -06:00
import type { SystemIOContext } from '@src/machines/systemIO/utils'
2025-04-09 09:46:30 -05:00
import {
NO_PROJECT_DIRECTORY,
SystemIOMachineActions,
SystemIOMachineActors,
SystemIOMachineEvents,
SystemIOMachineStates,
} from '@src/machines/systemIO/utils'
import { assertEvent, assign, fromPromise, setup } from 'xstate'
/**
* Handles any system level I/O for folders and files
* This machine will be initializes once within the applications runtime
* and exist for the entire life cycle of the application and able to be access
* at a global level.
*/
export const systemIOMachine = setup({
types: {
context: {} as SystemIOContext,
events: {} as
2025-04-09 09:46:30 -05:00
| {
type: SystemIOMachineEvents.readFoldersFromProjectDirectory
data: {}
}
| {
type: SystemIOMachineEvents.done_readFoldersFromProjectDirectory
data: {}
output: Project[]
}
| {
type: SystemIOMachineEvents.setProjectDirectoryPath
data: { requestedProjectDirectoryPath: string }
}
| {
type: SystemIOMachineEvents.openProject
data: { requestedProjectName: string }
2025-04-14 11:56:28 -06:00
},
},
actions: {
[SystemIOMachineActions.setFolders]: assign({
2025-04-09 09:46:30 -05:00
folders: ({ event }) => {
assertEvent(
event,
SystemIOMachineEvents.done_readFoldersFromProjectDirectory
)
return event.output
2025-04-09 09:46:30 -05:00
},
}),
[SystemIOMachineActions.setProjectDirectoryPath]: assign({
2025-04-09 09:46:30 -05:00
projectDirectoryPath: ({ event }) => {
assertEvent(event, SystemIOMachineEvents.setProjectDirectoryPath)
return event.data.requestedProjectDirectoryPath
2025-04-09 09:46:30 -05:00
},
}),
[SystemIOMachineActions.setRequestedProjectName]: assign({
requestedProjectName: ({ event }) => {
assertEvent(event, SystemIOMachineEvents.openProject)
2025-04-14 11:56:28 -06:00
return { name: event.data.requestedProjectName }
},
}),
},
actors: {
2025-04-09 09:46:30 -05:00
[SystemIOMachineActors.readFoldersFromProjectDirectory]: fromPromise(
async ({ input: context }: { input: SystemIOContext }) => {
return []
}
),
},
}).createMachine({
2025-04-09 09:46:30 -05:00
initial: SystemIOMachineStates.idle,
// Remember, this machine and change its projectDirectory at any point
// '' will be no project directory, aka clear this machine out!
// To be the aboslute root of someones computer we should take the string of path.resolve() in node.js which is different for each OS
context: () => ({
folders: [],
defaultProjectFolderName: DEFAULT_PROJECT_NAME,
projectDirectoryPath: NO_PROJECT_DIRECTORY,
2025-04-09 09:46:30 -05:00
hasListedProjects: false,
2025-04-14 11:56:28 -06:00
requestedProjectName: { name: NO_PROJECT_DIRECTORY },
}),
states: {
[SystemIOMachineStates.idle]: {
on: {
// on can be an action
2025-04-09 09:46:30 -05:00
[SystemIOMachineEvents.readFoldersFromProjectDirectory]:
SystemIOMachineStates.readingFolders,
[SystemIOMachineEvents.setProjectDirectoryPath]: {
target: SystemIOMachineStates.readingFolders,
2025-04-09 09:46:30 -05:00
actions: [SystemIOMachineActions.setProjectDirectoryPath],
},
[SystemIOMachineEvents.openProject]: {
actions: [SystemIOMachineActions.setRequestedProjectName],
},
},
},
[SystemIOMachineStates.readingFolders]: {
invoke: {
id: SystemIOMachineActors.readFoldersFromProjectDirectory,
src: SystemIOMachineActors.readFoldersFromProjectDirectory,
2025-04-09 09:46:30 -05:00
input: ({ context }) => {
return context
},
onDone: {
target: SystemIOMachineStates.idle,
2025-04-09 09:46:30 -05:00
actions: [SystemIOMachineActions.setFolders],
},
onError: {
target: SystemIOMachineStates.idle,
2025-04-09 09:46:30 -05:00
},
},
},
2025-04-14 11:56:28 -06:00
[SystemIOMachineStates.openingProject]: {},
2025-04-09 09:46:30 -05:00
},
})
// Watcher handler
// look at projectDirectory useEffect then send this event if it changes or if we need to do this?
// The handler needs to live somewhere... aka the provider?