refactor: pass event handler arguments as a single object

This commit is contained in:
James Murdza
2024-10-24 20:00:50 -06:00
parent c644b0054e
commit 1de980cdd6
10 changed files with 148 additions and 158 deletions

View File

@ -25,38 +25,38 @@ function extractPortNumber(inputString: string): number | null {
}
// Handle heartbeat from a socket connection
export function handleHeartbeat(data: any, context: HandlerContext) {
export function handleHeartbeat({ data }: { data: any }, context: HandlerContext) {
context.sandboxManager.setTimeout(CONTAINER_TIMEOUT)
}
// Handle getting a file
export function handleGetFile(fileId: string, context: HandlerContext) {
export function handleGetFile({ fileId }: { fileId: string }, context: HandlerContext) {
return context.fileManager.getFile(fileId)
}
// Handle getting a folder
export function handleGetFolder(folderId: string, context: HandlerContext) {
export function handleGetFolder({ folderId }: { folderId: string }, context: HandlerContext) {
return context.fileManager.getFolder(folderId)
}
// Handle saving a file
export function handleSaveFile(fileId: string, body: string, context: HandlerContext) {
export function handleSaveFile({ fileId, body }: { fileId: string, body: string }, context: HandlerContext) {
return context.fileManager.saveFile(fileId, body)
}
// Handle moving a file
export function handleMoveFile(fileId: string, folderId: string, context: HandlerContext) {
export function handleMoveFile({ fileId, folderId }: { fileId: string, folderId: string }, context: HandlerContext) {
return context.fileManager.moveFile(fileId, folderId)
}
// Handle listing apps
export async function handleListApps(context: HandlerContext) {
export async function handleListApps({ }, context: HandlerContext) {
if (!context.dokkuClient) throw Error("Failed to retrieve apps list: No Dokku client")
return { success: true, apps: await context.dokkuClient.listApps() }
}
// Handle deploying code
export async function handleDeploy(sandboxId: string, context: HandlerContext) {
export async function handleDeploy({ sandboxId }: { sandboxId: string }, context: HandlerContext) {
if (!context.gitClient) throw Error("Failed to retrieve apps list: No git client")
const fixedFilePaths = context.fileManager.sandboxFiles.fileData.map((file) => ({
...file,
@ -67,32 +67,32 @@ export async function handleDeploy(sandboxId: string, context: HandlerContext) {
}
// Handle creating a file
export function handleCreateFile(name: string, context: HandlerContext) {
export function handleCreateFile({ name }: { name: string }, context: HandlerContext) {
return context.fileManager.createFile(name)
}
// Handle creating a folder
export function handleCreateFolder(name: string, context: HandlerContext) {
export function handleCreateFolder({ name }: { name: string }, context: HandlerContext) {
return context.fileManager.createFolder(name)
}
// Handle renaming a file
export function handleRenameFile(fileId: string, newName: string, context: HandlerContext) {
export function handleRenameFile({ fileId, newName }: { fileId: string, newName: string }, context: HandlerContext) {
return context.fileManager.renameFile(fileId, newName)
}
// Handle deleting a file
export function handleDeleteFile(fileId: string, context: HandlerContext) {
export function handleDeleteFile({ fileId }: { fileId: string }, context: HandlerContext) {
return context.fileManager.deleteFile(fileId)
}
// Handle deleting a folder
export function handleDeleteFolder(folderId: string, context: HandlerContext) {
export function handleDeleteFolder({ folderId }: { folderId: string }, context: HandlerContext) {
return context.fileManager.deleteFolder(folderId)
}
// Handle creating a terminal session
export async function handleCreateTerminal(id: string, socket: any, data: any, context: HandlerContext) {
export async function handleCreateTerminal({ id, socket, data }: { id: string, socket: any, data: any }, context: HandlerContext) {
await context.lockManager.acquireLock(data.sandboxId, async () => {
await context.terminalManager.createTerminal(id, (responseString: string) => {
socket.emit("terminalResponse", { id, data: responseString })
@ -108,21 +108,21 @@ export async function handleCreateTerminal(id: string, socket: any, data: any, c
}
// Handle resizing a terminal
export function handleResizeTerminal(dimensions: { cols: number; rows: number }, context: HandlerContext) {
export function handleResizeTerminal({ dimensions }: { dimensions: { cols: number; rows: number } }, context: HandlerContext) {
context.terminalManager.resizeTerminal(dimensions)
}
// Handle sending data to a terminal
export function handleTerminalData(id: string, data: string, context: HandlerContext) {
export function handleTerminalData({ id, data }: { id: string, data: string }, context: HandlerContext) {
return context.terminalManager.sendTerminalData(id, data)
}
// Handle closing a terminal
export function handleCloseTerminal(id: string, context: HandlerContext) {
export function handleCloseTerminal({ id }: { id: string }, context: HandlerContext) {
return context.terminalManager.closeTerminal(id)
}
// Handle generating code
export function handleGenerateCode(userId: string, fileName: string, code: string, line: number, instructions: string, context: HandlerContext) {
export function handleGenerateCode({ userId, fileName, code, line, instructions }: { userId: string, fileName: string, code: string, line: number, instructions: string }, context: HandlerContext) {
return context.aiWorker.generateCode(userId, fileName, code, line, instructions)
}

View File

@ -181,160 +181,159 @@ io.on("connection", async (socket) => {
}
// Handle various socket events (heartbeat, file operations, terminal operations, etc.)
socket.on("heartbeat", async (callback) => {
socket.on("heartbeat", async (options, callback) => {
try {
callback?.(handleHeartbeat(data, handlerContext))
callback?.(handleHeartbeat(options, handlerContext))
} catch (e: any) {
console.error("Error setting timeout:", e)
socket.emit("error", `Error: set timeout. ${e.message ?? e}`)
}
})
socket.on("getFile", async (fileId: string, callback) => {
socket.on("getFile", async (options, callback) => {
try {
callback?.(await handleGetFile(fileId, handlerContext))
callback?.(await handleGetFile(options, handlerContext))
} catch (e: any) {
console.error("Error getting file:", e)
socket.emit("error", `Error: get file. ${e.message ?? e}`)
}
})
socket.on("getFolder", async (folderId: string, callback) => {
socket.on("getFolder", async (options, callback) => {
try {
callback?.(await handleGetFolder(folderId, handlerContext))
callback?.(await handleGetFolder(options, handlerContext))
} catch (e: any) {
console.error("Error getting folder:", e)
socket.emit("error", `Error: get folder. ${e.message ?? e}`)
}
})
socket.on("saveFile", async (fileId: string, body: string, callback) => {
socket.on("saveFile", async (options, callback) => {
try {
await saveFileRL.consume(data.userId, 1)
callback?.(await handleSaveFile(fileId, body, handlerContext))
callback?.(await handleSaveFile(options, handlerContext))
} catch (e: any) {
console.error("Error saving file:", e)
socket.emit("error", `Error: file saving. ${e.message ?? e}`)
}
})
socket.on("moveFile", async (fileId: string, folderId: string, callback) => {
socket.on("moveFile", async (options, callback) => {
try {
callback?.(await handleMoveFile(fileId, folderId, handlerContext))
callback?.(await handleMoveFile(options, handlerContext))
} catch (e: any) {
console.error("Error moving file:", e)
socket.emit("error", `Error: file moving. ${e.message ?? e}`)
}
})
socket.on("list", async (callback) => {
socket.on("list", async (options, callback) => {
console.log("Retrieving apps list...")
try {
callback?.(await handleListApps(handlerContext))
callback?.(await handleListApps(options, handlerContext))
} catch (e: any) {
console.error("Error retrieving apps list:", e)
socket.emit("error", `Error: app list retrieval. ${e.message ?? e}`)
}
})
socket.on("deploy", async (callback) => {
socket.on("deploy", async (options, callback) => {
try {
console.log("Deploying project ${data.sandboxId}...")
callback?.(await handleDeploy(data.sandboxId, handlerContext))
callback?.(await handleDeploy(options, handlerContext))
} catch (e: any) {
console.error("Error deploying project:", e)
socket.emit("error", `Error: project deployment. ${e.message ?? e}`)
}
})
socket.on("createFile", async (name: string, callback) => {
socket.on("createFile", async (options, callback) => {
try {
await createFileRL.consume(data.userId, 1)
callback?.({ success: await handleCreateFile(name, handlerContext) })
callback?.({ success: await handleCreateFile(options, handlerContext) })
} catch (e: any) {
console.error("Error creating file:", e)
socket.emit("error", `Error: file creation. ${e.message ?? e}`)
}
})
socket.on("createFolder", async (name: string, callback) => {
socket.on("createFolder", async (options, callback) => {
try {
await createFolderRL.consume(data.userId, 1)
callback?.(await handleCreateFolder(name, handlerContext))
callback?.(await handleCreateFolder(options, handlerContext))
} catch (e: any) {
console.error("Error creating folder:", e)
socket.emit("error", `Error: folder creation. ${e.message ?? e}`)
}
})
socket.on("renameFile", async (fileId: string, newName: string, callback) => {
socket.on("renameFile", async (options, callback) => {
try {
await renameFileRL.consume(data.userId, 1)
callback?.(await handleRenameFile(fileId, newName, handlerContext))
callback?.(await handleRenameFile(options, handlerContext))
} catch (e: any) {
console.error("Error renaming file:", e)
socket.emit("error", `Error: file renaming. ${e.message ?? e}`)
}
})
socket.on("deleteFile", async (fileId: string, callback) => {
socket.on("deleteFile", async (options, callback) => {
try {
await deleteFileRL.consume(data.userId, 1)
callback?.(await handleDeleteFile(fileId, handlerContext))
callback?.(await handleDeleteFile(options, handlerContext))
} catch (e: any) {
console.error("Error deleting file:", e)
socket.emit("error", `Error: file deletion. ${e.message ?? e}`)
}
})
socket.on("deleteFolder", async (folderId: string, callback) => {
socket.on("deleteFolder", async (options, callback) => {
try {
callback?.(await handleDeleteFolder(folderId, handlerContext))
callback?.(await handleDeleteFolder(options, handlerContext))
} catch (e: any) {
console.error("Error deleting folder:", e)
socket.emit("error", `Error: folder deletion. ${e.message ?? e}`)
}
})
socket.on("createTerminal", async (id: string, callback) => {
socket.on("createTerminal", async (options, callback) => {
try {
callback?.(await handleCreateTerminal(id, socket, data, handlerContext))
callback?.(await handleCreateTerminal(options, handlerContext))
} catch (e: any) {
console.error(`Error creating terminal ${id}:`, e)
console.error(`Error creating terminal ${options.id}:`, e)
socket.emit("error", `Error: terminal creation. ${e.message ?? e}`)
}
})
socket.on("resizeTerminal", (dimensions: { cols: number; rows: number }, callback) => {
socket.on("resizeTerminal", (options, callback) => {
try {
callback?.(handleResizeTerminal(dimensions, handlerContext))
callback?.(handleResizeTerminal(options, handlerContext))
} catch (e: any) {
console.error("Error resizing terminal:", e)
socket.emit("error", `Error: terminal resizing. ${e.message ?? e}`)
}
})
socket.on("terminalData", async (id: string, data: string, callback) => {
socket.on("terminalData", async (options, callback) => {
try {
callback?.(await handleTerminalData(id, data, handlerContext))
callback?.(await handleTerminalData(options, handlerContext))
} catch (e: any) {
console.error("Error writing to terminal:", e)
socket.emit("error", `Error: writing to terminal. ${e.message ?? e}`)
}
})
socket.on("closeTerminal", async (id: string, callback) => {
socket.on("closeTerminal", async (options, callback) => {
try {
callback?.(await handleCloseTerminal(id, handlerContext))
callback?.(await handleCloseTerminal(options, handlerContext))
} catch (e: any) {
console.error("Error closing terminal:", e)
socket.emit("error", `Error: closing terminal. ${e.message ?? e}`)
}
})
socket.on("generateCode", async (fileName: string, code: string, line: number, instructions: string, callback) => {
socket.on("generateCode", async (options, callback) => {
try {
callback?.(await handleGenerateCode(data.userId, fileName, code, line, instructions, handlerContext))
callback?.(await handleGenerateCode(options, handlerContext))
} catch (e: any) {
console.error("Error generating code:", e)
socket.emit("error", `Error: code generation. ${e.message ?? e}`)