Skip to content

Commit

Permalink
avoid race condition on context loading
Browse files Browse the repository at this point in the history
Signed-off-by: dartcafe <github@dartcafe.de>
  • Loading branch information
dartcafe committed Dec 29, 2024
1 parent 515e701 commit 98bc737
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
16 changes: 9 additions & 7 deletions src/composables/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
import { RouteLocationNormalized } from 'vue-router'
import { useSessionStore } from '../stores/session.ts'
import { usePreferencesStore } from '../stores/preferences.ts'
import { Logger } from '../helpers/index.ts'

const loadContext = (to: RouteLocationNormalized) => {
async function loadContext(to: RouteLocationNormalized) {
const preferencesStore = usePreferencesStore()
const sessionStore = useSessionStore()
sessionStore.setRouter(to)
sessionStore.load().then(() => {
if (sessionStore.userStatus.isLoggedin) {
preferencesStore.load()
}
})
await sessionStore.setRouter(to)
await sessionStore.load()
if (sessionStore.userStatus.isLoggedin) {
await preferencesStore.load()
}

Logger.debug('Context loaded')
}

export { loadContext }
10 changes: 5 additions & 5 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ async function validateToken(to: RouteLocationNormalized) {
if (getCurrentUser()) {
try {
const response = await PublicAPI.getShare(to.params.token)
// if the user is logged in, we diretly route to
// if the user is logged in, we diretly route to
// the internal vote page
return {
name: 'vote',
params: {
id: response.data.share.pollId
id: response.data.share.pollId
}
}
} catch (error) {
Expand All @@ -42,7 +42,7 @@ async function validateToken(to: RouteLocationNormalized) {
}
}
}

// continue for external users
try {
// first validate the existance of the public token
Expand All @@ -52,7 +52,7 @@ async function validateToken(to: RouteLocationNormalized) {
window.location.replace(generateUrl('login'))
}

// then look for an existing personal token from
// then look for an existing personal token from
// the user's client stored cookie
// matching the public token
const personalToken = getCookieValue(<string>to.params.token)
Expand Down Expand Up @@ -210,7 +210,7 @@ router.beforeEach(async (to: RouteLocationNormalized) => {
if (to.meta.votePage) {
await pollStore.load()
}

} catch (error) {
Logger.warn('Could not load poll', error)
return {
Expand Down
8 changes: 5 additions & 3 deletions src/stores/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type Route = {
}
}

export type UserStatus = {
export type UserStatus = {
isLoggedin: boolean
isAdmin: boolean
}
Expand Down Expand Up @@ -125,7 +125,7 @@ export const useSessionStore = defineStore('session', {

viewTextPoll(state): ViewMode {
const preferencesStore = usePreferencesStore()

if (state.sessionSettings.manualViewTextPoll) {
return state.sessionSettings.manualViewTextPoll
}
Expand All @@ -149,6 +149,7 @@ export const useSessionStore = defineStore('session', {

actions: {
async load() {
Logger.debug('Loading session')
let response = null
try {
if (this.route.name === 'publicVote') {
Expand All @@ -159,14 +160,15 @@ export const useSessionStore = defineStore('session', {
this.$patch(response.data)
} catch (error) {
if (error?.code === 'ERR_CANCELED') return

this.$reset()
if (this.route.name === null) {
this.$reset()
} else {
throw error
}
}
Logger.debug('Session loaded')
},

setViewDatePoll(viewMode: ViewMode) {
Expand Down

0 comments on commit 98bc737

Please sign in to comment.