Files
linux-odyssey/app/src/utils/session.js
T
wancat ed14491cfc feat: socket error handling
* create a new session when failed to connect to socket
* reset termnal on connect
2023-11-12 21:23:12 +08:00

108 lines
2.4 KiB
JavaScript

import { ref } from 'vue'
import { FileGraph } from '@linux-odyssey/file-graph'
import api from './api'
import Socket from './socket'
import SocketTerminal from './terminal'
class SessionManager {
constructor(questId) {
this.questId = questId
this.session = ref(null)
this.hints = ref([])
this.graph = ref(
new FileGraph({
path: '/',
type: 'folder',
discovered: false,
})
)
this.pwd = ref('')
this.status = ref('inactive')
this.socket = new Socket()
this.term = new SocketTerminal(40, 80)
}
init() {
this.socket.on('terminal', (data) => {
this.term.write(data)
})
this.term.onData((data) => {
this.socket.emit('terminal', data)
})
this.socket.on('graph', (event) => {
this.handleGraphUpdate(event)
})
this.socket.on('hints', (event) => {
this.handleHintUpdate(event)
})
this.socket.on('tasks', (tasks) => {
this.setTasks(tasks)
})
this.socket.on('status', (event) => {
this.handleStatusUpdate(event)
})
return this.getActiveSession()
}
getSession() {
return this.session.value
}
async setSession(session) {
this.session.value = session
this.graph.value = new FileGraph(session.graph)
this.hints.value = session.hints
this.status.value = session.status
this.term.reset()
await this.socket.connect(session)
this.term.focus()
}
handleGraphUpdate(event) {
if (event.discover) {
this.graph.value.discover(event.discover)
}
if (event.pwd) {
this.pwd.value = event.pwd
}
}
handleHintUpdate(hints) {
this.hints.value = [...this.hints.value, ...hints]
}
handleStatusUpdate(status) {
this.status.value = status
}
setTasks(tasks) {
this.session.value.tasks = tasks
}
async createSession() {
console.log('Creating a new session...')
const res = await api.post('/sessions', { quest_id: this.questId })
const { data } = res
console.log('Created Session ID:', data._id)
this.setSession(data)
}
async getActiveSession() {
const res = await api.post('/sessions/active', {
quest_id: this.questId,
})
const session = res.data
console.log(session)
try {
await this.setSession(session)
} catch (err) {
console.log(err)
this.createSession().catch(console.error)
}
}
}
const sessionManager = new SessionManager('helloworld')
export default sessionManager