Skip to content

Commit 7071303

Browse files
committed
Add a reloading meta flag to reloading store entries
1 parent cc8297d commit 7071303

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,27 @@ function HalJsonVuex (store, axios, options) {
155155
*/
156156
function load (uri, forceReload) {
157157
const existsInStore = !isUnknown(uri)
158-
const isLoading = existsInStore && (store.state[opts.apiName][uri]._meta || {}).loading
159158

160-
if (!existsInStore) {
161-
store.commit('addEmpty', uri)
162-
}
159+
const isLoading = existsInStore && (store.state[opts.apiName][uri]._meta || {}).loading
163160
if (isLoading) {
164161
// Reuse the loading entity and load promise that is already waiting for a pending API request
165162
return store.state[opts.apiName][uri]
166163
}
167164

165+
if (!existsInStore) {
166+
store.commit('addEmpty', uri)
167+
} else if (forceReload) {
168+
store.commit('reloading', uri)
169+
}
170+
168171
let dataFinishedLoading = Promise.resolve(store.state[opts.apiName][uri])
169-
if (!existsInStore || forceReload) {
172+
if (!existsInStore) {
170173
dataFinishedLoading = loadFromApi(uri)
174+
} else if (forceReload) {
175+
dataFinishedLoading = loadFromApi(uri).catch(error => {
176+
store.commit('reloadingFailed', uri)
177+
throw error
178+
})
171179
} else if (store.state[opts.apiName][uri]._meta.load) {
172180
// reuse the existing promise from the store if possible
173181
dataFinishedLoading = store.state[opts.apiName][uri]._meta.load

src/storeModule.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ export const mutations = {
2222
Vue.set(state, uri, data[uri])
2323
})
2424
},
25+
/**
26+
* Marks a single entity in the Vuex store as reloading, meaning a reloading network request is currently ongoin.
27+
* @param state Vuex state
28+
* @param uri URI of the entity that is currently being reloaded
29+
*/
30+
reloading (state, uri) {
31+
if (state[uri]) Vue.set(state[uri]._meta, 'reloading', true)
32+
},
33+
/**
34+
* Marks a single entity in the Vuex store as normal again, after it has been marked as reloading before.
35+
* @param state Vuex state
36+
* @param uri URI of the entity that is currently being reloaded
37+
*/
38+
reloadingFailed (state, uri) {
39+
if (state[uri]) Vue.set(state[uri]._meta, 'reloading', false)
40+
},
2541
/**
2642
* Removes a single entity from the Vuex store.
2743
* @param state Vuex state

0 commit comments

Comments
 (0)