fix server emit bug

This commit is contained in:
Ishaan Dey 2024-05-07 23:52:14 -07:00
parent 12e8051673
commit d0fd95bf13
3 changed files with 63 additions and 55 deletions

View File

@ -151,7 +151,7 @@ io.on("connection", async (socket) => {
})
await saveFile(fileId, body)
} catch (e) {
socket.emit("rateLimit", "Rate limited: file saving. Please slow down.")
io.emit("rateLimit", "Rate limited: file saving. Please slow down.")
}
})
@ -178,7 +178,7 @@ io.on("connection", async (socket) => {
await createFile(id)
} catch (e) {
socket.emit("rateLimit", "Rate limited: file creation. Please slow down.")
io.emit("rateLimit", "Rate limited: file creation. Please slow down.")
}
})
@ -203,7 +203,7 @@ io.on("connection", async (socket) => {
)
await renameFile(fileId, newFileId, file.data)
} catch (e) {
socket.emit("rateLimit", "Rate limited: file renaming. Please slow down.")
io.emit("rateLimit", "Rate limited: file renaming. Please slow down.")
return
}
})
@ -226,7 +226,7 @@ io.on("connection", async (socket) => {
const newFiles = await getSandboxFiles(data.sandboxId)
callback(newFiles.files)
} catch (e) {
socket.emit("rateLimit", "Rate limited: file deletion. Please slow down.")
io.emit("rateLimit", "Rate limited: file deletion. Please slow down.")
}
})
@ -243,7 +243,8 @@ io.on("connection", async (socket) => {
})
const onData = pty.onData((data) => {
socket.emit("terminalResponse", {
console.log("terminalResponse", id, data)
io.emit("terminalResponse", {
id,
data,
})
@ -259,11 +260,13 @@ io.on("connection", async (socket) => {
onExit,
}
callback(true)
callback()
})
socket.on("terminalData", (id: string, data: string) => {
console.log("terminalData", id, data)
if (!terminals[id]) {
console.log("terminal not found", id)
return
}
@ -279,11 +282,14 @@ io.on("connection", async (socket) => {
return
}
console.log("closing terminal", id)
terminals[id].onData.dispose()
terminals[id].onExit.dispose()
delete terminals[id]
callback(true)
console.log("terminals:", Object.keys(terminals))
callback()
})
socket.on(
@ -324,6 +330,7 @@ io.on("connection", async (socket) => {
socket.on("disconnect", async () => {
if (data.isOwner) {
console.log("deleting all terminals")
Object.entries(terminals).forEach((t) => {
const { terminal, onData, onExit } = t[1]
onData.dispose()

View File

@ -359,14 +359,11 @@ export default function CodeEditor({
useEffect(() => {
const onConnect = () => {
console.log("connected");
createTerminal();
};
const onDisconnect = () => {
console.log("disconnected");
closeAllTerminals();
setTerminals([]);
};
const onLoadedEvent = (files: (TFolder | TFile)[]) => {
@ -378,11 +375,14 @@ export default function CodeEditor({
};
const onTerminalResponse = (response: { id: string; data: string }) => {
const res = response.data;
console.log("terminal response:", res);
console.log("terminal response:", response);
// console.log("activeId:", activeTerminalId);
const term = terminals.find((t) => t.id === response.id);
if (term && term.terminal) term.terminal.write(res);
// const term = terminals.find((t) => t.id === response.id);
// if (term && term.terminal) {
// console.log("writing to terminal, id:", response.id);
// term.terminal.write(response.data);
// }
};
const onDisableAccess = (message: string) => {
@ -414,15 +414,14 @@ export default function CodeEditor({
const createTerminal = () => {
setCreatingTerminal(true);
const id = createId();
setActiveTerminalId(id);
console.log("creating terminal, id:", id);
setTimeout(() => {
socket.emit("createTerminal", id, (res: boolean) => {
if (res) {
socket.emit("createTerminal", id, () => {
setTerminals((prev) => [...prev, { id, terminal: null }]);
}
setActiveTerminalId(id);
setCreatingTerminal(false);
});
}, 1000);
setCreatingTerminal(false);
};
const selectFile = (tab: TTab) => {
@ -476,8 +475,7 @@ export default function CodeEditor({
const index = terminals.findIndex((t) => t.id === term.id);
if (index === -1) return;
socket.emit("closeTerminal", term.id, (res: boolean) => {
if (res) {
socket.emit("closeTerminal", term.id, () => {
const nextId =
activeTerminalId === term.id
? numTerminals === 1
@ -497,14 +495,6 @@ export default function CodeEditor({
setActiveTerminalId(nextTerminal.id);
}
}
}
});
};
const closeAllTerminals = () => {
terminals.forEach((term) => {
socket.emit("closeTerminal", term.id, () => {}); // no need to wait for response here
setTerminals((prev) => prev.filter((t) => t.id !== term.id));
});
};
@ -781,24 +771,34 @@ export default function CodeEditor({
)}
</Button>
</div>
<div className="w-full relative grow h-full overflow-hidden rounded-md bg-secondary">
{socket && activeTerminal ? (
<div className="w-full relative grow h-full overflow-hidden rounded-md bg-secondary">
<EditorTerminal
socket={socket}
id={activeTerminal.id}
term={activeTerminal.terminal}
setTerm={(t: Terminal) => {
console.log(
"setting terminal",
activeTerminalId,
t.options
);
setTerminals((prev) =>
prev.map((term) =>
term.id === activeTerminal.id
term.id === activeTerminalId
? { ...term, terminal: t }
: term
)
);
}}
/>
) : null}
</div>
) : (
<div className="w-full h-full flex items-center justify-center text-lg font-medium text-muted-foreground/50 select-none">
<TerminalSquare className="w-4 h-4 mr-2" />
No terminals open.
</div>
)}
</>
) : (
<div className="w-full h-full flex items-center justify-center text-lg font-medium text-muted-foreground/50 select-none">

View File

@ -51,6 +51,7 @@ export default function EditorTerminal({
setTerm(term);
}
const disposable = term.onData((data) => {
console.log("terminalData", id, data);
socket.emit("terminalData", id, data);
});