almost ready

This commit is contained in:
Chris Wanstrath 2025-09-20 19:04:53 -07:00
parent d66b3f9f5f
commit 4c65c6f4ed
4 changed files with 12 additions and 13 deletions

View File

@ -5,7 +5,6 @@ import { addInput, setStatus, addOutput } from "./scrollback.js"
import { send } from "./websocket.js"
import { randomID } from "../shared/utils.js"
import type { Message, CommandResult } from "../shared/types.js"
import type { CompoundAssignmentOperator } from "typescript"
export function runCommand(input: string) {
const id = randomID()
@ -15,9 +14,7 @@ export function runCommand(input: string) {
}
// message received from server
export function handleMessage(e: MessageEvent) {
const data = JSON.parse(e.data)
console.log("-> receive", data)
export function handleMessage(data: Message) {
if (data.type === "output") {
handleOutput(data)
} else {

View File

@ -14,7 +14,7 @@ export function startConnection() {
ws = new WebSocket(url)
ws.onopen = () => console.log('WS connected')
ws.onmessage = handleMessage
ws.onmessage = receive
ws.onclose = () => setTimeout(startConnection, 1000) // simple retry
ws.onerror = () => ws?.close()
}
@ -26,13 +26,14 @@ export function send(msg: Message) {
console.log("-> send", msg)
}
function receive(e: MessageEvent) {
const data = JSON.parse(e.data) as Message
console.log("<- receive", data)
handleMessage(data)
}
// close it... plz don't do this, though
export function close() {
ws?.close(1000, 'bye')
}
// register a local listener
export function onMessage(callback: (e: MessageEvent) => void) {
if (!ws) return
ws.onmessage = callback
}

View File

@ -52,12 +52,12 @@ app.on("GET", ["/js/:path{.+}", "/shared/:path{.+}"], async c => {
// websocket
//
const connections: any[] = []
const wsConnections: any[] = []
app.get("/ws", upgradeWebSocket((c) => {
return {
onOpen(_e, ws) {
connections.push(ws)
wsConnections.push(ws)
},
onMessage(event, ws) {
let data: Message | undefined
@ -125,7 +125,7 @@ if (process.env.BUN_HOT) {
// @ts-ignore
globalThis.__hot_reload_cleanup = () => {
connections.forEach(conn => conn?.close())
wsConnections.forEach(conn => conn?.close())
}
for (const sig of ["SIGINT", "SIGTERM"] as const) {

View File

@ -2,6 +2,7 @@
// runs commands and such.
import type { CommandResult } from "./shared/types"
import { NOSE_BIN } from "./config"
export function runCommand(input: string): CommandResult {
return { status: "ok", output: "command: " + input }