Skip to content

Commit

Permalink
fix: fix multiple fetches at launch
Browse files Browse the repository at this point in the history
  • Loading branch information
AlejandroAkbal committed Feb 22, 2021
1 parent dd9f4fb commit fadd6fd
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions store/booru/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { debounce } from 'lodash-es'
import {
defaultBooruList,
findBoorusWithValueByKey,
Expand Down Expand Up @@ -355,38 +356,42 @@ export const actions = {
return urlToFetch.toString()
},

async fetchPosts({ dispatch }, mode) {
// Tip: Actions that return a value have to be awaited
const url = await dispatch('createApiUrl', { mode: 'posts' })
fetchPosts: debounce(
async function ({ dispatch }, mode) {
// Tip: Actions that return a value have to be awaited
const url = await dispatch('createApiUrl', { mode: 'posts' })

try {
const response = await dispatch(
'simpleFetch',
{
url,
},
{ root: true }
)
try {
const response = await dispatch(
'simpleFetch',
{
url,
},
{ root: true }
)

if (mode === 'concat') {
dispatch('postsManager', { operation: 'concat', value: response })
} else {
dispatch('postsManager', { operation: 'set', value: response })
}
if (mode === 'concat') {
dispatch('postsManager', { operation: 'concat', value: response })
} else {
dispatch('postsManager', { operation: 'set', value: response })
}

//
} catch (error) {
dispatch(
'errorManager',
{
operation: 'set',
value: error,
message: "Couldn't fetch posts",
},
{ root: true }
)
}
},
//
} catch (error) {
dispatch(
'errorManager',
{
operation: 'set',
value: error,
message: "Couldn't fetch posts",
},
{ root: true }
)
}
},
1,
{ maxWait: 5 }
),

async fetchTags({ dispatch, commit }, tag) {
const url = await dispatch('createApiUrl', { mode: 'tags', tag })
Expand Down

0 comments on commit fadd6fd

Please sign in to comment.