mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
refactor: add getOrCreateSession to simplify process
This commit is contained in:
1 parent
11bc793b12
commit
5a7c4da628
6 files changed
+72
-32
No files matched your search
@@ -58,34 +58,14 @@ class SessionManager {
|
||||
this.setSession(data)
|
||||
}
|
||||
|
||||
async getSessionList() {
|
||||
const res = await api.get('/sessions', {
|
||||
params: { quest_id: this.questId },
|
||||
async getActiveSession() {
|
||||
const res = await api.post('/sessions/active', {
|
||||
quest_id: this.questId,
|
||||
})
|
||||
const { data } = res
|
||||
return data
|
||||
}
|
||||
|
||||
async lastSession() {
|
||||
const sessions = await this.getSessionList()
|
||||
if (sessions.length === 0) {
|
||||
return
|
||||
}
|
||||
const { _id: sessionId } = sessions[sessions.length - 1]
|
||||
const { data: session } = await api.get(`/sessions/${sessionId}`)
|
||||
console.log(
|
||||
`Last Session ID: ${session._id}, Quest: ${session.quest}, Created At: ${session.createdAt}`
|
||||
)
|
||||
const session = res.data
|
||||
console.log(session)
|
||||
this.setSession(session)
|
||||
}
|
||||
|
||||
async lastOrCreate() {
|
||||
await this.lastSession()
|
||||
if (!this.session.value) {
|
||||
await this.createSession()
|
||||
}
|
||||
console.log(this.session.value)
|
||||
}
|
||||
}
|
||||
|
||||
const sessionManager = new SessionManager('helloworld')
|
||||
|
||||
@@ -16,7 +16,7 @@ const completed = computed(() => {
|
||||
})
|
||||
|
||||
onMounted(async () => {
|
||||
await sessionManager.lastOrCreate()
|
||||
await sessionManager.getActiveSession()
|
||||
socket.connect(sessionManager.getSession())
|
||||
socket.on('connect', function open() {
|
||||
console.log('Connected to the server.')
|
||||
|
||||
Reference in new issue
Block a user