Skip to content

Commit

Permalink
handle cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Jun 22, 2024
1 parent c8caffd commit bf636c7
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/cli/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ export async function startServer(options: { port: string }) {
}
> = {}

const cancelAll = () => {
for (const [runId, run] of Object.entries(runs)) {
console.log(`abort run ${runId}`)
run.canceller.abort("closing")
delete runs[runId]
}
}

// cleanup runs
wss.on("close", () => {
cancelAll()
})
wss.on("connection", function connection(ws) {
console.log(`clients: connected (${wss.clients.size} clients)`)
ws.on("error", console.error)
Expand Down Expand Up @@ -120,6 +132,8 @@ export async function startServer(options: { port: string }) {
break
}
case "script.start": {
cancelAll()

const { script, files, options, id } = data
const runId = id
const canceller =
Expand Down Expand Up @@ -157,6 +171,7 @@ export async function startServer(options: { port: string }) {
)
})
.catch((e) => {
if (canceller.controller.signal.aborted) return
if (!isCancelError(e)) trace.error(e)
ws?.send(
JSON.stringify(<
Expand Down

0 comments on commit bf636c7

Please sign in to comment.