refactor: centralize all variable to sessionManager

This commit is contained in:
wancat committed 2023-10-30 23:12:50 +08:00
1 parent 5a7c4da628
commit 9c3c6adb16
6 files changed
+55 -38

No files matched your search

-4
View File
@@ -30,13 +30,9 @@
<script setup> <script setup>
import sessionManager from '../utils/session' import sessionManager from '../utils/session'
import socket from '../utils/socket'
import socketTerminal from '../utils/terminal'
const reset = async () => { const reset = async () => {
console.log('resetting...') console.log('resetting...')
await sessionManager.createSession() await sessionManager.createSession()
socket.connect(sessionManager.getSession())
socketTerminal.clear()
} }
</script> </script>
+3 -3
View File
@@ -10,12 +10,12 @@
<script setup> <script setup>
import { onMounted, ref } from 'vue' import { onMounted, ref } from 'vue'
import socketTerminal from '../utils/terminal' import sessionManager from '../utils/session'
const terminal = ref(null) const terminal = ref(null)
onMounted(() => { onMounted(() => {
socketTerminal.mount(terminal.value) sessionManager.term.mount(terminal.value)
window.addEventListener('resize', () => socketTerminal.resizeScreen()) window.addEventListener('resize', () => sessionManager.term.resizeScreen())
}) })
</script> </script>
+28
View File
@@ -1,6 +1,8 @@
import { ref } from 'vue' import { ref } from 'vue'
import { FileGraph } from '@linux-odyssey/file-graph' import { FileGraph } from '@linux-odyssey/file-graph'
import api from './api' import api from './api'
import Socket from './socket'
import SocketTerminal from './terminal'
class SessionManager { class SessionManager {
constructor(questId) { constructor(questId) {
@@ -16,6 +18,30 @@ class SessionManager {
) )
this.pwd = ref('') this.pwd = ref('')
this.status = ref('inactive') 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() { getSession() {
@@ -27,6 +53,8 @@ class SessionManager {
this.graph.value = new FileGraph(session.graph) this.graph.value = new FileGraph(session.graph)
this.hints.value = session.hints this.hints.value = session.hints
this.status.value = session.status this.status.value = session.status
this.term.clear()
this.socket.connect(session)
} }
handleGraphUpdate(event) { handleGraphUpdate(event) {
+15 -1
View File
@@ -22,6 +22,14 @@ class SocketWrapper {
console.log('Socket connected to session', session._id) console.log('Socket connected to session', session._id)
}) })
this.socket.on('disconnect', () => {
console.log('Socket disconnected from session', session._id)
})
this.socket.on('error', (err) => {
console.log('Socket error', err)
})
this.bindListeners() this.bindListeners()
} }
@@ -32,6 +40,12 @@ class SocketWrapper {
} }
} }
once(event, callback) {
if (this.socket) {
this.socket.once(event, callback)
}
}
bindListeners() { bindListeners() {
this.listeners.forEach(({ event, callback }) => { this.listeners.forEach(({ event, callback }) => {
this.socket.on(event, callback) this.socket.on(event, callback)
@@ -47,4 +61,4 @@ class SocketWrapper {
} }
} }
export default new SocketWrapper() export default SocketWrapper
+8 -8
View File
@@ -1,6 +1,5 @@
import { Terminal } from 'xterm' import { Terminal } from 'xterm'
import { FitAddon } from 'xterm-addon-fit' import { FitAddon } from 'xterm-addon-fit'
import socket from './socket'
import 'xterm/css/xterm.css' import 'xterm/css/xterm.css'
class SocketTerminal { class SocketTerminal {
@@ -24,13 +23,14 @@ class SocketTerminal {
this.fitAddon = new FitAddon() this.fitAddon = new FitAddon()
this.term.loadAddon(this.fitAddon) this.term.loadAddon(this.fitAddon)
this.fitAddon.fit() this.fitAddon.fit()
this.socket = socket }
this.socket.on('terminal', (message) => {
write(message) {
this.term.write(message) this.term.write(message)
}) }
this.term.onData((key) => {
this.socket.emit('terminal', key) onData(callback) {
}) this.term.onData(callback)
} }
resizeScreen() { resizeScreen() {
@@ -48,4 +48,4 @@ class SocketTerminal {
} }
} }
export default new SocketTerminal(40, 100) export default SocketTerminal
+1 -22
View File
@@ -1,6 +1,5 @@
<script setup> <script setup>
import { computed, onMounted } from 'vue' import { computed, onMounted } from 'vue'
import socket from '../utils/socket'
import sessionManager from '../utils/session' import sessionManager from '../utils/session'
import GameHeaderPart from '../components/GameHeaderPart.vue' import GameHeaderPart from '../components/GameHeaderPart.vue'
import CommandlistPart from '../components/CommandlistPart.vue' import CommandlistPart from '../components/CommandlistPart.vue'
@@ -16,27 +15,7 @@ const completed = computed(() => {
}) })
onMounted(async () => { onMounted(async () => {
await sessionManager.getActiveSession() await sessionManager.init()
socket.connect(sessionManager.getSession())
socket.on('connect', function open() {
console.log('Connected to the server.')
})
socket.on('disconnect', function close() {
console.log('Disconnected from the server.')
})
socket.on('graph', (event) => {
sessionManager.handleGraphUpdate(event)
})
socket.on('hints', (event) => {
sessionManager.handleHintUpdate(event)
})
socket.on('tasks', (tasks) => {
sessionManager.setTasks(tasks)
})
socket.on('message', console.log)
socket.on('status', (event) => {
sessionManager.handleStatusUpdate(event)
})
}) })
</script> </script>