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

-13
View File
@@ -95,10 +95,6 @@ function newResponse(response) {
store.session.status = response.status
}
function updateStatus(status) {
store.session.status = status
}
export function useTerminal() {
return term
}
@@ -137,18 +133,9 @@ function setup() {
socket.on('graph', (event) => {
updateGraph(event)
})
// socket.on('hints', (event) => {
// updateHints(event)
// })
// socket.on('tasks', (tasks) => {
// setTasks(tasks)
// })
socket.on('response', (response) => {
newResponse(response)
})
socket.on('status', (event) => {
updateStatus(event)
})
}
setup()
+1 -1
View File
@@ -48,7 +48,7 @@ export interface ISession {
user: Types.ObjectId
quest: string
containerId: string | null
status: string
status: 'active' | 'finished' | 'inactive'
createdAt: Date
updatedAt: Date
finishedAt?: Date
@@ -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' })
}
)
-1
View File
@@ -5,6 +5,5 @@ import { newCommandValidator } from '../validators/commandValidator.js'
const router = Router()
router.post('/', newCommandValidator, commandController.newCommand)
router.post('/completed', commandController.completedCommand)
export default router
+6
View File
@@ -79,6 +79,12 @@ export default class SessionHandler {
this.addNewTasks()
this.session.responses.push(stage.responses)
if (stage.id === 'END') {
this.session.status = 'finished'
}
return {
responses: stage.responses,
hints: stage.hints,