Skip to content

Commit 6032069

Browse files
Guillaume Chauiksim
Guillaume Chau
authored and
iksim
committed
fix(vuex): cache edited state, closes vuejs#956
1 parent 574755c commit 6032069

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/backend/vuex.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ class VuexBackend {
179179
index,
180180
snapshot: this.getStoreSnapshot()
181181
})
182+
this.cacheStateSnapshot(index, true)
182183
}
183184

184185
/**
@@ -205,7 +206,11 @@ class VuexBackend {
205206

206207
resetSnapshotCache () {
207208
this.stateSnapshotCache = [
208-
{ index: -1, state: this.baseStateSnapshot }
209+
{
210+
index: -1,
211+
state: this.baseStateSnapshot,
212+
permanent: true
213+
}
209214
]
210215
}
211216

@@ -489,17 +494,25 @@ class VuexBackend {
489494
return result
490495
}
491496

492-
cacheStateSnapshot (index) {
497+
cacheStateSnapshot (index, permanent = false) {
498+
this.removeCachedStateSnapshot(index)
493499
this.stateSnapshotCache.push({
494500
index,
495-
state: clone(this.store.state)
501+
state: clone(this.store.state),
502+
permanent
496503
})
497504
// Delete old cached snapshots
498-
if (this.stateSnapshotCache.length > SharedData.cacheVuexSnapshotsLimit) {
499-
this.stateSnapshotCache.splice(1, 1)
505+
if (this.stateSnapshotCache.filter(s => !s.permanent).length > SharedData.cacheVuexSnapshotsLimit) {
506+
const i = this.stateSnapshotCache.findIndex(s => !s.permanent)
507+
if (i !== -1) this.stateSnapshotCache.splice(i, 1)
500508
}
501509
}
502510

511+
removeCachedStateSnapshot (index) {
512+
const i = this.stateSnapshotCache.findIndex(s => s.idex === index)
513+
if (i !== -1) this.stateSnapshotCache.splice(i, 1)
514+
}
515+
503516
/**
504517
* Get the serialized state and getters from the store
505518
*/

0 commit comments

Comments
 (0)