Skip to content

Commit 3bce674

Browse files
author
Guillaume Chau
committed
perf(vuex): dynamic debounce
1 parent 9e92ed4 commit 3bce674

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

packages/app-frontend/src/views/vuex/VuexStateInspector.vue

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ import { searchDeepInObject, sortByKey, stringify, parse } from '@utils/util'
124124
import debounce from 'lodash/debounce'
125125
import groupBy from 'lodash/groupBy'
126126
import { mapState, mapGetters, mapActions, mapMutations } from 'vuex'
127+
import { mutationBuffer } from './module'
127128
128129
export default {
129130
components: {
@@ -253,6 +254,10 @@ export default {
253254
}
254255
},
255256
257+
created () {
258+
this.loadStateDebounce = 300
259+
},
260+
256261
mounted () {
257262
bridge.on('vuex:mutation', this.onMutation)
258263
if (this.isOnlyMutationPayload && this.$shared.vuexAutoload) {
@@ -312,9 +317,25 @@ export default {
312317
}
313318
}, 250),
314319
315-
loadState: debounce(function () {
316-
this.loadStateNow()
317-
}, 300),
320+
loadState () {
321+
// Debouncing
322+
clearTimeout(this.loadStateTimer)
323+
this.loadStateTimer = setTimeout(() => {
324+
this.loadStateNow()
325+
}, this.loadStateDebounce)
326+
327+
// Increase debounce delay
328+
this.loadStateDebounce += 600
329+
if (this.loadStateDebounce > 2000) {
330+
this.loadStateDebounce = 2000
331+
}
332+
333+
// Reset debounce delay after a period of inactivity
334+
clearTimeout(this.loadStateDebounceResetTimer)
335+
this.loadStateDebounceResetTimer = setTimeout(() => {
336+
this.loadStateDebounce = 300
337+
}, 3000)
338+
},
318339
319340
loadStateNow () {
320341
const history = this.filteredHistory
@@ -323,10 +344,18 @@ export default {
323344
324345
onMutation () {
325346
if (this.$shared.vuexAutoload) {
326-
const unwatch = this.$watch(() => this.history.length, (value, oldValue) => {
327-
unwatch()
347+
if (this.unwatchHistoryLength) this.unwatchHistoryLength()
348+
if (mutationBuffer.length) {
349+
// Wait for history to receive mutations batch
350+
this.unwatchHistoryLength = this.$watch(() => this.history.length, (value, oldValue) => {
351+
this.unwatchHistoryLength()
352+
if (!mutationBuffer.length) {
353+
this.loadState()
354+
}
355+
})
356+
} else {
328357
this.loadState()
329-
})
358+
}
330359
}
331360
},
332361

packages/app-frontend/src/views/vuex/actions.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { snapshotsCache } from './cache'
22
import Resolve from './resolve'
33
import SharedData from '@utils/shared-data'
44
import debounce from 'lodash/debounce'
5-
6-
const mutationBuffer = []
5+
import { mutationBuffer } from './module'
76

87
export function receiveMutation ({ commit }, entry) {
98
mutationBuffer.push(entry)

packages/app-frontend/src/views/vuex/module.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const ANY_RE = new RegExp('.*', 'i')
88

99
let uid = 0
1010

11+
export const mutationBuffer = []
12+
1113
const state = {
1214
hasVuex: false,
1315
base: null, // type Snapshot = { state: {}, getters: {} }

0 commit comments

Comments
 (0)