mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 11:30:13 +08:00
Fix re-login error (#60)
* refactor: put all session ref into one reactive store * fix relogin duplicate signal * fix: put quest data into store, fix hint reactivity * fix: duplicate after relogin * only register listener once
This commit is contained in:
1 parent
10e9d89349
commit
7d7ad154ab
10 files changed
+179
-168
No files matched your search
@@ -1,107 +0,0 @@
|
||||
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
|
||||
+12
-14
@@ -8,10 +8,7 @@ class SocketWrapper {
|
||||
|
||||
connect(session) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.socket) {
|
||||
this.socket.off()
|
||||
this.socket.disconnect()
|
||||
}
|
||||
this.disconnect()
|
||||
|
||||
this.socket = io('', {
|
||||
query: {
|
||||
@@ -38,25 +35,26 @@ class SocketWrapper {
|
||||
}
|
||||
}
|
||||
|
||||
once(event, callback) {
|
||||
if (this.socket) {
|
||||
this.socket.once(event, callback)
|
||||
}
|
||||
}
|
||||
|
||||
bindListeners() {
|
||||
this.listeners.forEach(({ event, callback }) => {
|
||||
this.socket.on(event, callback)
|
||||
})
|
||||
}
|
||||
|
||||
send(data) {
|
||||
this.socket.send(data)
|
||||
}
|
||||
|
||||
emit(event, data) {
|
||||
this.socket.emit(event, data)
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
if (this.socket) {
|
||||
this.socket.off()
|
||||
this.socket.disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
export default SocketWrapper
|
||||
Reference in new issue
Block a user