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

View File

@ -359,14 +359,11 @@ export default function CodeEditor({
useEffect(() => { useEffect(() => {
const onConnect = () => { const onConnect = () => {
console.log("connected"); console.log("connected");
createTerminal();
}; };
const onDisconnect = () => { const onDisconnect = () => {
console.log("disconnected"); console.log("disconnected");
setTerminals([]);
closeAllTerminals();
}; };
const onLoadedEvent = (files: (TFolder | TFile)[]) => { const onLoadedEvent = (files: (TFolder | TFile)[]) => {
@ -378,11 +375,14 @@ export default function CodeEditor({
}; };
const onTerminalResponse = (response: { id: string; data: string }) => { const onTerminalResponse = (response: { id: string; data: string }) => {
const res = response.data; console.log("terminal response:", response);
console.log("terminal response:", res); // console.log("activeId:", activeTerminalId);
const term = terminals.find((t) => t.id === response.id); // const term = terminals.find((t) => t.id === response.id);
if (term && term.terminal) term.terminal.write(res); // if (term && term.terminal) {
// console.log("writing to terminal, id:", response.id);
// term.terminal.write(response.data);
// }
}; };
const onDisableAccess = (message: string) => { const onDisableAccess = (message: string) => {
@ -414,15 +414,14 @@ export default function CodeEditor({
const createTerminal = () => { const createTerminal = () => {
setCreatingTerminal(true); setCreatingTerminal(true);
const id = createId(); const id = createId();
setActiveTerminalId(id); console.log("creating terminal, id:", id);
setTimeout(() => { setTimeout(() => {
socket.emit("createTerminal", id, (res: boolean) => { socket.emit("createTerminal", id, () => {
if (res) {
setTerminals((prev) => [...prev, { id, terminal: null }]); setTerminals((prev) => [...prev, { id, terminal: null }]);
} setActiveTerminalId(id);
setCreatingTerminal(false);
}); });
}, 1000); }, 1000);
setCreatingTerminal(false);
}; };
const selectFile = (tab: TTab) => { const selectFile = (tab: TTab) => {
@ -476,8 +475,7 @@ export default function CodeEditor({
const index = terminals.findIndex((t) => t.id === term.id); const index = terminals.findIndex((t) => t.id === term.id);
if (index === -1) return; if (index === -1) return;
socket.emit("closeTerminal", term.id, (res: boolean) => { socket.emit("closeTerminal", term.id, () => {
if (res) {
const nextId = const nextId =
activeTerminalId === term.id activeTerminalId === term.id
? numTerminals === 1 ? numTerminals === 1
@ -497,14 +495,6 @@ export default function CodeEditor({
setActiveTerminalId(nextTerminal.id); 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> </Button>
</div> </div>
<div className="w-full relative grow h-full overflow-hidden rounded-md bg-secondary">
{socket && activeTerminal ? ( {socket && activeTerminal ? (
<div className="w-full relative grow h-full overflow-hidden rounded-md bg-secondary">
<EditorTerminal <EditorTerminal
socket={socket} socket={socket}
id={activeTerminal.id} id={activeTerminal.id}
term={activeTerminal.terminal} term={activeTerminal.terminal}
setTerm={(t: Terminal) => { setTerm={(t: Terminal) => {
console.log(
"setting terminal",
activeTerminalId,
t.options
);
setTerminals((prev) => setTerminals((prev) =>
prev.map((term) => prev.map((term) =>
term.id === activeTerminal.id term.id === activeTerminalId
? { ...term, terminal: t } ? { ...term, terminal: t }
: term : term
) )
); );
}} }}
/> />
) : null}
</div> </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"> <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); setTerm(term);
} }
const disposable = term.onData((data) => { const disposable = term.onData((data) => {
console.log("terminalData", id, data);
socket.emit("terminalData", id, data); socket.emit("terminalData", id, data);
}); });