feat: add reset function

This commit is contained in:
wancat committed 2023-08-17 21:17:48 +08:00
1 parent e122800c81
commit 313d30edda
4 files changed
+108 -66

No files matched your search

+18 -6
View File
@@ -1,16 +1,23 @@
import { ref } from 'vue'
import api from './api'
class SessionManager {
constructor(questId) {
this.questId = questId
this.session = ref(null)
}
async createdSession() {
setSession(session) {
console.log('Update session:', session)
this.session.value = session
}
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)
return data
this.setSession(data)
}
async getSessionList() {
@@ -24,20 +31,25 @@ class SessionManager {
async lastSession() {
const sessions = await this.getSessionList()
if (sessions.length === 0) {
return null
return
}
const session = sessions[sessions.length - 1]
console.log(
`Last Session ID: ${session._id}, Quest: ${session.quest}, Created At: ${session.createdAt}`
)
return session
this.setSession(session)
}
async latestOrCreate() {
return (await this.lastSession()) || this.createdSession()
async lastOrCreate() {
await this.lastSession()
if (!this.session.value) {
await this.createSession()
}
}
}
const sessionManager = new SessionManager('helloworld')
sessionManager.lastOrCreate().catch(console.error)
export default sessionManager