Skip to content

Commit

Permalink
fix(ui): safer settings changed check
Browse files Browse the repository at this point in the history
Fixes #4010
  • Loading branch information
robertsLando committed Nov 22, 2024
1 parent a0f9e0c commit 890fbd0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
25 changes: 24 additions & 1 deletion src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,36 @@ export function deepEqual(a, b) {
return a === b || JSON.stringify(a) === JSON.stringify(b)
}

export function safeDeepEqual(a, b) {
if (a === b) return true

if (typeof a !== 'object' || typeof b !== 'object') return false

if (Array.isArray(a) !== Array.isArray(b)) return false

if (Array.isArray(a)) {
return arraysEqual(a, b)
}

const keysA = Object.keys(a)
const keysB = Object.keys(b)

if (keysA.length !== keysB.length) return false

for (const key of keysA) {
if (!safeDeepEqual(a[key], b[key])) return false
}

return true
}

export function arraysEqual(a, b) {
if (a === b) return true
if (a == null || b == null) return false
if (a.length !== b.length) return false

for (let i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false
if (!safeDeepEqual(a[i], b[i])) return false
}

return true
Expand Down
14 changes: 7 additions & 7 deletions src/views/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2046,7 +2046,7 @@
import { mapActions, mapState } from 'pinia'
import ConfigApis from '@/apis/ConfigApis'
import { parse } from 'native-url'
import { wait, copy, isUndef, deepEqual } from '../lib/utils'
import { wait, copy, isUndef, safeDeepEqual } from '../lib/utils'
import { rfRegions, znifferRegions } from '../lib/items'
import cronstrue from 'cronstrue'
import useBaseStore from '../stores/base'
Expand Down Expand Up @@ -2098,12 +2098,12 @@ export default {
},
},
settingsChanged() {
if (!deepEqual(this.newMqtt, this.mqtt)) return true
if (!deepEqual(this.newGateway, this.gateway)) return true
if (!deepEqual(this.newZwave, this.zwave)) return true
if (!deepEqual(this.newBackup, this.backup)) return true
if (!deepEqual(this.newZniffer, this.zniffer)) return true
if (!deepEqual(this.ui, this.prevUi)) return true
if (!safeDeepEqual(this.newMqtt, this.mqtt)) return true
if (!safeDeepEqual(this.newGateway, this.gateway)) return true
if (!safeDeepEqual(this.newZwave, this.zwave)) return true
if (!safeDeepEqual(this.newBackup, this.backup)) return true
if (!safeDeepEqual(this.newZniffer, this.zniffer)) return true
if (!safeDeepEqual(this.ui, this.prevUi)) return true
return false
},
Expand Down

0 comments on commit 890fbd0

Please sign in to comment.