Fix ghci death throes

This commit is contained in:
Radon Rosborough 2020-06-07 16:51:46 -06:00
parent 6784a2bdae
commit 869816c6ab
1 changed files with 21 additions and 12 deletions

View File

@ -10,13 +10,13 @@ import { LangConfig, langs } from "./langs";
export class Session { export class Session {
code: string; code: string;
config: LangConfig; config: LangConfig;
term: IPty; term: { pty: IPty; live: boolean };
ws: WebSocket; ws: WebSocket;
constructor(ws: WebSocket, lang: string) { constructor(ws: WebSocket, lang: string) {
this.ws = ws; this.ws = ws;
this.config = langs[lang]; this.config = langs[lang];
this.term = null; this.term = { pty: null, live: false };
this.code = ""; this.code = "";
this.ws.send( this.ws.send(
JSON.stringify({ JSON.stringify({
@ -42,7 +42,7 @@ export class Session {
} else if (typeof msg.input !== "string") { } else if (typeof msg.input !== "string") {
console.error(`terminalInput: missing or malformed input field`); console.error(`terminalInput: missing or malformed input field`);
} else { } else {
this.term.write(msg.input); this.term.pty.write(msg.input);
} }
break; break;
case "runCode": case "runCode":
@ -67,8 +67,9 @@ export class Session {
}; };
run = async () => { run = async () => {
const { repl, file, suffix, run } = this.config; const { repl, file, suffix, run } = this.config;
if (this.term) { if (this.term.pty) {
this.term.kill(); this.term.pty.kill();
this.term.live = false;
} }
this.ws.send(JSON.stringify({ event: "terminalClear" })); this.ws.send(JSON.stringify({ event: "terminalClear" }));
const tmpdir: string = await new Promise((resolve, reject) => const tmpdir: string = await new Promise((resolve, reject) =>
@ -99,13 +100,21 @@ export class Session {
} else { } else {
cmdline = this.parseCmdline(repl); cmdline = this.parseCmdline(repl);
} }
this.term = pty.spawn(cmdline[0], cmdline.slice(1), { const term = {
name: "xterm-color", pty: pty.spawn(cmdline[0], cmdline.slice(1), {
cwd: tmpdir, name: "xterm-color",
env: process.env, cwd: tmpdir,
env: process.env,
}),
live: true,
};
this.term = term;
term.pty.on("data", (data) => {
// Capture term in closure so that we don't keep sending output
// from the old pty even after it's been killed (see ghci).
if (term.live) {
this.ws.send(JSON.stringify({ event: "terminalOutput", output: data }));
}
}); });
this.term.on("data", (data) =>
this.ws.send(JSON.stringify({ event: "terminalOutput", output: data }))
);
}; };
} }