Skip to content

Commit ff044ba

Browse files
committed
Avoid duplicate reloads
1 parent 7071303 commit ff044ba

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
### Unreleased
22

3+
### 1.2.1
4+
- Avoid sending a reload request as long as an equal reload request is still ongoing
5+
36
### 1.2.0
47
- Fixed a bug involving the load promise of embedded collections
58
- Added a new method `isUnknown` which can be used to determine whether an URI has never been requested from the API before (except if it was purged in the meantime)

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,9 @@ function HalJsonVuex (store, axios, options) {
156156
function load (uri, forceReload) {
157157
const existsInStore = !isUnknown(uri)
158158

159-
const isLoading = existsInStore && (store.state[opts.apiName][uri]._meta || {}).loading
160-
if (isLoading) {
159+
const isAlreadyLoading = existsInStore && (store.state[opts.apiName][uri]._meta || {}).loading
160+
const isAlreadyReloading = existsInStore && (store.state[opts.apiName][uri]._meta || {}).reloading
161+
if (isAlreadyLoading || (forceReload && isAlreadyReloading)) {
161162
// Reuse the loading entity and load promise that is already waiting for a pending API request
162163
return store.state[opts.apiName][uri]
163164
}

tests/store.spec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,46 @@ describe('API store', () => {
427427
done()
428428
})
429429

430+
it('refuses to send out the same reload request again before the ongoing one has completed', async done => {
431+
// given
432+
axiosMock.onGet('http://localhost/camps/1').replyOnce(200, {
433+
id: 1,
434+
_links: {
435+
self: {
436+
href: '/camps/1'
437+
}
438+
}
439+
})
440+
axiosMock.onGet('http://localhost/camps/1').replyOnce(200, {
441+
id: 2,
442+
_links: {
443+
self: {
444+
href: '/camps/1'
445+
}
446+
}
447+
})
448+
axiosMock.onGet('http://localhost/camps/1').replyOnce(200, {
449+
id: 3,
450+
_links: {
451+
self: {
452+
href: '/camps/1'
453+
}
454+
}
455+
})
456+
const loaded = vm.api.get('/camps/1')
457+
await letNetworkRequestFinish()
458+
vm.api.reload(loaded)
459+
460+
// when
461+
const load = vm.api.reload(loaded)
462+
463+
// then
464+
await letNetworkRequestFinish()
465+
const result = await load
466+
expect(result).toMatchObject({ id: 2, _meta: { self: 'http://localhost/camps/1' } })
467+
done()
468+
})
469+
430470
it('throws when trying to access _meta in an invalid object', () => {
431471
// given
432472

0 commit comments

Comments
 (0)