Skip to content

Commit de13935

Browse files
committed
Also add a trivial load promise to fully loaded objects in the store
1 parent 248b913 commit de13935

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/index.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,7 @@ function HalJsonVuex (store, axios, options) {
173173
dataFinishedLoading = store.state[opts.apiName][uri]._meta.load
174174
}
175175

176-
// We mutate the store state here without telling Vuex about it, so it won't complain and won't make load reactive.
177-
// The promise is needed in the store for some special cases when a loading entity is requested a second time with
178-
// this.api.get(...) or this.api.reload(...).
179-
store.state[opts.apiName][uri]._meta.load = markAsDoneWhenResolved(dataFinishedLoading)
176+
setLoadPromiseOnStore(uri, dataFinishedLoading)
180177

181178
return store.state[opts.apiName][uri]
182179
}
@@ -353,6 +350,22 @@ function HalJsonVuex (store, axios, options) {
353350
embeddedStandaloneCollectionKey: 'items'
354351
})
355352
store.commit('add', normalizedData)
353+
354+
Object.keys(normalizedData).forEach(uri => {
355+
setLoadPromiseOnStore(uri)
356+
})
357+
}
358+
359+
/**
360+
* Mutate the store state without telling Vuex about it, so it won't complain and won't make the load promise
361+
* reactive.
362+
* The promise is needed in the store for some special cases when a loading entity is requested a second time with
363+
* this.api.get(...) or this.api.reload(...), or when an embedded collection is reloaded.
364+
* @param uri
365+
* @param promise
366+
*/
367+
function setLoadPromiseOnStore (uri, promise = null) {
368+
store.state[opts.apiName][uri]._meta.load = markAsDoneWhenResolved(promise || Promise.resolve(store.state[opts.apiName][uri]))
356369
}
357370

358371
/**

tests/store.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ describe('API store', () => {
497497
// then
498498
expect(vm.$store.state.api).toMatchObject(embeddedSingleEntity.storeState)
499499
await letNetworkRequestFinish()
500-
expect(vm.$store.state.api['/campTypes/20']).toEqual(campType.storeState)
500+
expect(vm.$store.state.api['/campTypes/20']).toMatchObject(campType.storeState)
501501
done()
502502
})
503503

@@ -537,7 +537,7 @@ describe('API store', () => {
537537
// then
538538
expect(vm.$store.state.api).toMatchObject(embeddedSingleEntity.storeState)
539539
await letNetworkRequestFinish()
540-
expect(vm.$store.state.api['/campTypes/20']).toEqual(campTypeData.storeState)
540+
expect(vm.$store.state.api['/campTypes/20']).toMatchObject(campTypeData.storeState)
541541
done()
542542
})
543543

@@ -601,8 +601,8 @@ describe('API store', () => {
601601
}
602602
]
603603
}
604-
axiosMock.onGet('http://localhost/camps/1').reply(200, campData.serverResponse)
605-
axiosMock.onGet('http://localhost/camps/1').reply(200, campData.serverResponse2)
604+
axiosMock.onGet('http://localhost/camps/1').replyOnce(200, campData.serverResponse)
605+
axiosMock.onGet('http://localhost/camps/1').replyOnce(200, campData.serverResponse2)
606606
vm.api.get('/camps/1').activityTypes()
607607
await letNetworkRequestFinish()
608608
const embeddedCollection = vm.api.get('/camps/1').activityTypes()

0 commit comments

Comments
 (0)