feat: send status to the frontend

This commit is contained in:
wancat committed 2024-02-29 10:54:53 +08:00
1 parent 622d64dd45
commit 039da53bbe
5 files changed
+8 -46

No files matched your search

@@ -6,8 +6,6 @@ import { pushToSession } from '../socket.js'
import { finishSession } from '../../models/sessionManager.js'
import { asyncHandler } from '../../middleware/error.js'
const commandCompleteCallbacks = new Map()
export const newCommand = asyncHandler(async (req: Request, res: Response) => {
const { command, pwd, output, error, params } = matchedData(req)
@@ -53,40 +51,12 @@ export const newCommand = asyncHandler(async (req: Request, res: Response) => {
session.responses.push(response.responses)
if (response.stage === 'END') {
if ((session.status as string) === 'finished') {
await finishSession(session)
}
// commandCompleteCallbacks.set(session.id, response.callback)
pushToSession(session.id, 'response', response)
}
res.status(200).end()
await session.save()
})
export const completedCommand = asyncHandler(
async (req: Request, res: Response) => {
const { sessionId } = req.user as any
const session = await Session.findById(sessionId)
if (!session) {
res.status(404).json({ message: 'session not found' })
return
}
if (session.status === 'inactive') {
res.status(400).json({ message: 'session is not active' })
return
}
const callback = commandCompleteCallbacks.get(session.id)
if (!callback) {
res.status(200).json({ message: 'no callback found' })
return
}
callback()
commandCompleteCallbacks.delete(session.id)
res.json({ message: 'ok' })
}
)