refactor: add callback usage to all event handlers

This commit is contained in:
James Murdza 2024-10-24 19:15:03 -06:00
parent 33c8ed8b32
commit c644b0054e

View File

@ -20,7 +20,6 @@ import { SecureGitClient } from "./SecureGitClient"
import { socketAuth } from "./socketAuth"; // Import the new socketAuth middleware import { socketAuth } from "./socketAuth"; // Import the new socketAuth middleware
import { handleCloseTerminal, handleCreateFile, handleCreateFolder, handleCreateTerminal, handleDeleteFile, handleDeleteFolder, handleDeploy, handleGenerateCode, handleGetFile, handleGetFolder, handleHeartbeat, handleListApps, handleMoveFile, HandlerContext, handleRenameFile, handleResizeTerminal, handleSaveFile, handleTerminalData } from "./SocketHandlers" import { handleCloseTerminal, handleCreateFile, handleCreateFolder, handleCreateTerminal, handleDeleteFile, handleDeleteFolder, handleDeploy, handleGenerateCode, handleGetFile, handleGetFolder, handleHeartbeat, handleListApps, handleMoveFile, HandlerContext, handleRenameFile, handleResizeTerminal, handleSaveFile, handleTerminalData } from "./SocketHandlers"
import { TerminalManager } from "./TerminalManager" import { TerminalManager } from "./TerminalManager"
import { DokkuResponse } from "./types"
import { LockManager } from "./utils" import { LockManager } from "./utils"
// Handle uncaught exceptions // Handle uncaught exceptions
@ -182,9 +181,9 @@ io.on("connection", async (socket) => {
} }
// Handle various socket events (heartbeat, file operations, terminal operations, etc.) // Handle various socket events (heartbeat, file operations, terminal operations, etc.)
socket.on("heartbeat", async () => { socket.on("heartbeat", async (callback) => {
try { try {
handleHeartbeat(data, handlerContext) callback?.(handleHeartbeat(data, handlerContext))
} catch (e: any) { } catch (e: any) {
console.error("Error setting timeout:", e) console.error("Error setting timeout:", e)
socket.emit("error", `Error: set timeout. ${e.message ?? e}`) socket.emit("error", `Error: set timeout. ${e.message ?? e}`)
@ -193,7 +192,7 @@ io.on("connection", async (socket) => {
socket.on("getFile", async (fileId: string, callback) => { socket.on("getFile", async (fileId: string, callback) => {
try { try {
callback(await handleGetFile(fileId, handlerContext)) callback?.(await handleGetFile(fileId, handlerContext))
} catch (e: any) { } catch (e: any) {
console.error("Error getting file:", e) console.error("Error getting file:", e)
socket.emit("error", `Error: get file. ${e.message ?? e}`) socket.emit("error", `Error: get file. ${e.message ?? e}`)
@ -202,17 +201,17 @@ io.on("connection", async (socket) => {
socket.on("getFolder", async (folderId: string, callback) => { socket.on("getFolder", async (folderId: string, callback) => {
try { try {
callback(await handleGetFolder(folderId, handlerContext)) callback?.(await handleGetFolder(folderId, handlerContext))
} catch (e: any) { } catch (e: any) {
console.error("Error getting folder:", e) console.error("Error getting folder:", e)
socket.emit("error", `Error: get folder. ${e.message ?? e}`) socket.emit("error", `Error: get folder. ${e.message ?? e}`)
} }
}) })
socket.on("saveFile", async (fileId: string, body: string) => { socket.on("saveFile", async (fileId: string, body: string, callback) => {
try { try {
await saveFileRL.consume(data.userId, 1) await saveFileRL.consume(data.userId, 1)
await handleSaveFile(fileId, body, handlerContext) callback?.(await handleSaveFile(fileId, body, handlerContext))
} catch (e: any) { } catch (e: any) {
console.error("Error saving file:", e) console.error("Error saving file:", e)
socket.emit("error", `Error: file saving. ${e.message ?? e}`) socket.emit("error", `Error: file saving. ${e.message ?? e}`)
@ -221,41 +220,37 @@ io.on("connection", async (socket) => {
socket.on("moveFile", async (fileId: string, folderId: string, callback) => { socket.on("moveFile", async (fileId: string, folderId: string, callback) => {
try { try {
callback(await handleMoveFile(fileId, folderId, handlerContext)) callback?.(await handleMoveFile(fileId, folderId, handlerContext))
} catch (e: any) { } catch (e: any) {
console.error("Error moving file:", e) console.error("Error moving file:", e)
socket.emit("error", `Error: file moving. ${e.message ?? e}`) socket.emit("error", `Error: file moving. ${e.message ?? e}`)
} }
}) })
socket.on("list", async (callback: (response: DokkuResponse) => void) => { socket.on("list", async (callback) => {
console.log("Retrieving apps list...") console.log("Retrieving apps list...")
try { try {
callback(await handleListApps(handlerContext)) callback?.(await handleListApps(handlerContext))
} catch (error) { } catch (e: any) {
callback({ console.error("Error retrieving apps list:", e)
success: false, socket.emit("error", `Error: app list retrieval. ${e.message ?? e}`)
message: "Failed to retrieve apps list",
})
} }
}) })
socket.on("deploy", async (callback: (response: DokkuResponse) => void) => { socket.on("deploy", async (callback) => {
try { try {
console.log("Deploying project ${data.sandboxId}...") console.log("Deploying project ${data.sandboxId}...")
callback(await handleDeploy(data.sandboxId, handlerContext)) callback?.(await handleDeploy(data.sandboxId, handlerContext))
} catch (error) { } catch (e: any) {
callback({ console.error("Error deploying project:", e)
success: false, socket.emit("error", `Error: project deployment. ${e.message ?? e}`)
message: "Failed to deploy project: " + error,
})
} }
}) })
socket.on("createFile", async (name: string, callback) => { socket.on("createFile", async (name: string, callback) => {
try { try {
await createFileRL.consume(data.userId, 1) await createFileRL.consume(data.userId, 1)
callback({ success: await handleCreateFile(name, handlerContext) }) callback?.({ success: await handleCreateFile(name, handlerContext) })
} catch (e: any) { } catch (e: any) {
console.error("Error creating file:", e) console.error("Error creating file:", e)
socket.emit("error", `Error: file creation. ${e.message ?? e}`) socket.emit("error", `Error: file creation. ${e.message ?? e}`)
@ -265,18 +260,17 @@ io.on("connection", async (socket) => {
socket.on("createFolder", async (name: string, callback) => { socket.on("createFolder", async (name: string, callback) => {
try { try {
await createFolderRL.consume(data.userId, 1) await createFolderRL.consume(data.userId, 1)
await handleCreateFolder(name, handlerContext) callback?.(await handleCreateFolder(name, handlerContext))
callback()
} catch (e: any) { } catch (e: any) {
console.error("Error creating folder:", e) console.error("Error creating folder:", e)
socket.emit("error", `Error: folder creation. ${e.message ?? e}`) socket.emit("error", `Error: folder creation. ${e.message ?? e}`)
} }
}) })
socket.on("renameFile", async (fileId: string, newName: string) => { socket.on("renameFile", async (fileId: string, newName: string, callback) => {
try { try {
await renameFileRL.consume(data.userId, 1) await renameFileRL.consume(data.userId, 1)
await handleRenameFile(fileId, newName, handlerContext) callback?.(await handleRenameFile(fileId, newName, handlerContext))
} catch (e: any) { } catch (e: any) {
console.error("Error renaming file:", e) console.error("Error renaming file:", e)
socket.emit("error", `Error: file renaming. ${e.message ?? e}`) socket.emit("error", `Error: file renaming. ${e.message ?? e}`)
@ -286,7 +280,7 @@ io.on("connection", async (socket) => {
socket.on("deleteFile", async (fileId: string, callback) => { socket.on("deleteFile", async (fileId: string, callback) => {
try { try {
await deleteFileRL.consume(data.userId, 1) await deleteFileRL.consume(data.userId, 1)
callback(await handleDeleteFile(fileId, handlerContext)) callback?.(await handleDeleteFile(fileId, handlerContext))
} catch (e: any) { } catch (e: any) {
console.error("Error deleting file:", e) console.error("Error deleting file:", e)
socket.emit("error", `Error: file deletion. ${e.message ?? e}`) socket.emit("error", `Error: file deletion. ${e.message ?? e}`)
@ -295,7 +289,7 @@ io.on("connection", async (socket) => {
socket.on("deleteFolder", async (folderId: string, callback) => { socket.on("deleteFolder", async (folderId: string, callback) => {
try { try {
callback(await handleDeleteFolder(folderId, handlerContext)) callback?.(await handleDeleteFolder(folderId, handlerContext))
} catch (e: any) { } catch (e: any) {
console.error("Error deleting folder:", e) console.error("Error deleting folder:", e)
socket.emit("error", `Error: folder deletion. ${e.message ?? e}`) socket.emit("error", `Error: folder deletion. ${e.message ?? e}`)
@ -304,26 +298,25 @@ io.on("connection", async (socket) => {
socket.on("createTerminal", async (id: string, callback) => { socket.on("createTerminal", async (id: string, callback) => {
try { try {
await handleCreateTerminal(id, socket, data, handlerContext) callback?.(await handleCreateTerminal(id, socket, data, handlerContext))
callback()
} catch (e: any) { } catch (e: any) {
console.error(`Error creating terminal ${id}:`, e) console.error(`Error creating terminal ${id}:`, e)
socket.emit("error", `Error: terminal creation. ${e.message ?? e}`) socket.emit("error", `Error: terminal creation. ${e.message ?? e}`)
} }
}) })
socket.on("resizeTerminal", (dimensions: { cols: number; rows: number }) => { socket.on("resizeTerminal", (dimensions: { cols: number; rows: number }, callback) => {
try { try {
handleResizeTerminal(dimensions, handlerContext) callback?.(handleResizeTerminal(dimensions, handlerContext))
} catch (e: any) { } catch (e: any) {
console.error("Error resizing terminal:", e) console.error("Error resizing terminal:", e)
socket.emit("error", `Error: terminal resizing. ${e.message ?? e}`) socket.emit("error", `Error: terminal resizing. ${e.message ?? e}`)
} }
}) })
socket.on("terminalData", async (id: string, data: string) => { socket.on("terminalData", async (id: string, data: string, callback) => {
try { try {
await handleTerminalData(id, data, handlerContext) callback?.(await handleTerminalData(id, data, handlerContext))
} catch (e: any) { } catch (e: any) {
console.error("Error writing to terminal:", e) console.error("Error writing to terminal:", e)
socket.emit("error", `Error: writing to terminal. ${e.message ?? e}`) socket.emit("error", `Error: writing to terminal. ${e.message ?? e}`)
@ -332,8 +325,7 @@ io.on("connection", async (socket) => {
socket.on("closeTerminal", async (id: string, callback) => { socket.on("closeTerminal", async (id: string, callback) => {
try { try {
await handleCloseTerminal(id, handlerContext) callback?.(await handleCloseTerminal(id, handlerContext))
callback()
} catch (e: any) { } catch (e: any) {
console.error("Error closing terminal:", e) console.error("Error closing terminal:", e)
socket.emit("error", `Error: closing terminal. ${e.message ?? e}`) socket.emit("error", `Error: closing terminal. ${e.message ?? e}`)
@ -342,7 +334,7 @@ io.on("connection", async (socket) => {
socket.on("generateCode", async (fileName: string, code: string, line: number, instructions: string, callback) => { socket.on("generateCode", async (fileName: string, code: string, line: number, instructions: string, callback) => {
try { try {
callback(await handleGenerateCode(data.userId, fileName, code, line, instructions, handlerContext)) callback?.(await handleGenerateCode(data.userId, fileName, code, line, instructions, handlerContext))
} catch (e: any) { } catch (e: any) {
console.error("Error generating code:", e) console.error("Error generating code:", e)
socket.emit("error", `Error: code generation. ${e.message ?? e}`) socket.emit("error", `Error: code generation. ${e.message ?? e}`)