Skip to content

Commit

Permalink
fix(store): move debounce functions
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
  • Loading branch information
Antreesy committed Feb 12, 2024
1 parent d89b57f commit 5fdc171
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/store/pollStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import pollService from '../services/pollService.js'

const state = {
polls: {},
pollDebounceFunctions: {},
activePoll: null,
}

Expand Down Expand Up @@ -61,13 +62,10 @@ const mutations = {

// Add debounce function for getting the poll data
addDebounceGetPollDataFunction(state, { token, pollId, debounceGetPollDataFunction }) {
if (!state.polls?.pollDebounceFunctions) {
Vue.set(state.polls, 'pollDebounceFunctions', {})
if (!state.pollDebounceFunctions[token]) {
Vue.set(state.pollDebounceFunctions, token, {})
}
if (!state.polls.pollDebounceFunctions?.[token]) {
Vue.set(state.polls.pollDebounceFunctions, [token], {})
}
Vue.set(state.polls.pollDebounceFunctions[token], pollId, debounceGetPollDataFunction)
Vue.set(state.pollDebounceFunctions[token], pollId, debounceGetPollDataFunction)
},
}

Expand Down Expand Up @@ -100,7 +98,7 @@ const actions = {
debounceGetPollData(context, { token, pollId }) {
// Create debounce function for getting this particular poll data
// if it does not exist yet
if (!context.state.polls?.pollDebounceFunctions?.[token]?.[pollId]) {
if (!context.state.pollDebounceFunctions[token]?.[pollId]) {
const debounceGetPollDataFunction = debounce(async () => {
await context.dispatch('getPollData', {
token,
Expand All @@ -115,7 +113,7 @@ const actions = {
})
}
// Call the debounce function for getting the poll data
context.state.polls.pollDebounceFunctions[token][pollId]()
context.state.pollDebounceFunctions[token][pollId]()
},

async submitVote(context, { token, pollId, vote }) {
Expand Down

0 comments on commit 5fdc171

Please sign in to comment.