perf: 🐛 Fix the problem of frequently trigger preservation of preferences

This commit is contained in:
viarotel 2024-07-12 11:57:43 +08:00
parent 677f30cdc2
commit 8261916172
5 changed files with 103 additions and 114 deletions

View File

@ -24,7 +24,7 @@ export default {
for (let index = 0; index < this.devices.length; index++) { for (let index = 0; index < this.devices.length; index++) {
const item = this.devices[index] const item = this.devices[index]
await this.$refs.applicationProxyRef.invoke(item) await this.$refs.applicationProxyRef.invoke(item)
await sleep(2 * 1000) await sleep(1 * 1000)
} }
}, },
}, },

View File

@ -24,7 +24,7 @@ export default {
for (let index = 0; index < this.devices.length; index++) { for (let index = 0; index < this.devices.length; index++) {
const item = this.devices[index] const item = this.devices[index]
await this.$refs.screenshotProxyRef.invoke(item) await this.$refs.screenshotProxyRef.invoke(item)
await sleep(2 * 1000) await sleep(1 * 1000)
} }
}, },
}, },

View File

@ -119,14 +119,14 @@ const preferenceData = defineModel('modelValue', {
const preferenceStore = usePreferenceStore() const preferenceStore = usePreferenceStore()
const collapseValue = ref([])
const preferenceModel = computed(() => const preferenceModel = computed(() =>
omit(preferenceStore.model, props.excludes), omit(preferenceStore.model, props.excludes),
) )
const preferenceModelKeys = Object.keys(preferenceModel.value ?? {}) const preferenceModelKeys = Object.keys(preferenceModel.value ?? {})
const collapseValue = ref([])
if (preferenceModelKeys.length) { if (preferenceModelKeys.length) {
if (props.collapseProps.accordion) { if (props.collapseProps.accordion) {
collapseValue.value = preferenceModelKeys[0] collapseValue.value = preferenceModelKeys[0]
@ -136,18 +136,6 @@ if (preferenceModelKeys.length) {
} }
} }
watch(
() => props.deviceScope,
(value) => {
if (!value) {
return false
}
preferenceData.value = preferenceStore.getData(value)
},
{ immediate: true },
)
function subModel(item) { function subModel(item) {
const children = item?.children || {} const children = item?.children || {}

View File

@ -0,0 +1,70 @@
<template>
<el-select
:placeholder="$t('preferences.scope.placeholder')"
:no-data-text="$t('preferences.scope.no-data')"
filterable
class="!w-90"
>
<template #prefix>
<el-tooltip class="" effect="dark" placement="bottom-start">
<el-icon class="text-primary-300 hover:text-primary-500">
<QuestionFilled />
</el-icon>
<template #content>
<div class="space-y-1">
<div class="pb-1">
{{ $t('preferences.scope.details[0]') }}
</div>
<div class="">
{{ $t('preferences.scope.details[1]') }}
</div>
<div class="">
{{ $t('preferences.scope.details[2]') }}
</div>
</div>
</template>
</el-tooltip>
</template>
<el-option
v-for="item in options"
:key="item.id"
:label="item.label"
:value="item.value"
>
</el-option>
</el-select>
</template>
<script setup>
import { useDeviceStore } from '$/store/index.js'
const emit = defineEmits(['device-change'])
const deviceStore = useDeviceStore()
const options = computed(() => {
const value = deviceStore.list.map(item => ({
...item,
label: `${item.id}${item.$name}${
item.$remark ? `${item.$remark}` : ''
}`,
value: item.id,
}))
value.unshift({
label: `Global${window.t('preferences.scope.global')}`,
value: 'global',
})
return value
})
watch(
() => deviceStore.list.length,
() => {
emit('device-change', options.value)
},
)
</script>
<style></style>

View File

@ -4,43 +4,11 @@
class="mr-4 pb-4 flex items-center justify-between flex-none border-b border-gray-200 dark:border-gray-700" class="mr-4 pb-4 flex items-center justify-between flex-none border-b border-gray-200 dark:border-gray-700"
> >
<div class=""> <div class="">
<el-select <ScopeSelect
v-model="deviceScope" v-model="deviceScope"
value-key=""
:placeholder="$t('preferences.scope.placeholder')"
filterable
:no-data-text="$t('preferences.scope.no-data')"
class="!w-90"
@change="onScopeChange" @change="onScopeChange"
> @device-change="onDeviceChange"
<template #prefix> />
<el-tooltip class="" effect="dark" placement="bottom-start">
<el-icon class="text-primary-300 hover:text-primary-500">
<QuestionFilled />
</el-icon>
<template #content>
<div class="space-y-1">
<div class="pb-1">
{{ $t('preferences.scope.details[0]') }}
</div>
<div class="">
{{ $t('preferences.scope.details[1]') }}
</div>
<div class="">
{{ $t('preferences.scope.details[2]') }}
</div>
</div>
</template>
</el-tooltip>
</template>
<el-option
v-for="item in scopeList"
:key="item.id"
:label="item.label"
:value="item.value"
>
</el-option>
</el-select>
</div> </div>
<div class=""> <div class="">
<el-button type="" icon="Upload" plain @click="handleImport"> <el-button type="" icon="Upload" plain @click="handleImport">
@ -52,7 +20,7 @@
<el-button type="" icon="Edit" plain @click="handleEdit"> <el-button type="" icon="Edit" plain @click="handleEdit">
{{ $t('preferences.config.edit.name') }} {{ $t('preferences.config.edit.name') }}
</el-button> </el-button>
<el-button type="" icon="RefreshRight" plain @click="handleResetAll"> <el-button type="" icon="RefreshRight" plain @click="handleReset">
{{ $t('preferences.config.reset.name') }} {{ $t('preferences.config.reset.name') }}
</el-button> </el-button>
</div> </div>
@ -73,47 +41,32 @@
<script> <script>
import { debounce } from 'lodash-es' import { debounce } from 'lodash-es'
import ScopeSelect from './components/ScopeSelect/index.vue'
import PreferenceForm from './components/PreferenceForm/index.vue' import PreferenceForm from './components/PreferenceForm/index.vue'
import { usePreferenceStore } from '$/store/index.js' import { usePreferenceStore } from '$/store/index.js'
export default { export default {
components: { components: {
ScopeSelect,
PreferenceForm, PreferenceForm,
}, },
setup() { setup() {
const preferenceStore = usePreferenceStore() const preferenceStore = usePreferenceStore()
const preferenceData = ref(preferenceStore.data) const preferenceData = ref(preferenceStore.data)
const deviceScope = ref(preferenceStore.deviceScope) const deviceScope = ref(preferenceStore.deviceScope)
return { return {
preferenceStore,
preferenceData, preferenceData,
deviceScope, deviceScope,
} }
}, },
computed: { computed: {
commonPreferenceModel() {
return this.$store.preference.model?.common || {}
},
preferenceModel() { preferenceModel() {
return this.$store.preference.model || {} return this.preferenceStore.model || {}
},
scopeList() {
const value = this.$store.device.list.map(item => ({
...item,
label: `${item.id}${item.$name}${
item.$remark ? `${item.$remark}` : ''
}`,
value: item.id,
}))
value.unshift({
label: `Global${this.$t('preferences.scope.global')}`,
value: 'global',
})
return value
}, },
}, },
watch: { watch: {
@ -133,50 +86,36 @@ export default {
this.handleDevices() this.handleDevices()
}, },
}, },
// global
'scopeList': {
handler(value) {
const someValue = value.some(
item => this.$replaceIP(item.value) === this.deviceScope,
)
if (someValue) {
return
}
this.deviceScope = 'global'
this.$store.preference.setScope(this.deviceScope)
this.preferenceData = this.$store.preference.data
},
immediate: true,
},
}, },
created() { created() {
this.handleSave = debounce(this.handleSave, 1000) this.handleSave = debounce(this.handleSave, 1000)
this.handleDevices = debounce(this.handleDevices, 1000) this.handleDevices = debounce(this.handleDevices, 1000)
}, },
methods: { methods: {
onDeviceChange(options) {
const device = options.some(
item => this.$replaceIP(item.value) === this.deviceScope,
)
if (device) {
return false
}
this.deviceScope = 'global'
this.preferenceStore.setScope(this.deviceScope)
this.preferenceData = this.preferenceStore.data
},
handleDevices() { handleDevices() {
this.$root.reRenderPost() this.$root.reRenderPost()
}, },
subModel(item) { handleReset() {
const children = item?.children || {} this.preferenceStore.reset(this.deviceScope)
const value = {} this.preferenceData = this.preferenceStore.data
Object.entries(children).forEach(([key, data]) => {
if (!data.hidden) {
value[key] = data
}
})
return value
},
handleResetAll() {
this.$store.preference.reset(this.deviceScope)
this.preferenceData = this.$store.preference.data
}, },
onScopeChange(value) { onScopeChange(value) {
this.$store.preference.setScope(value) this.preferenceStore.setScope(value)
this.preferenceData = this.$store.preference.data this.preferenceData = this.preferenceStore.data
}, },
async handleImport() { async handleImport() {
@ -194,7 +133,7 @@ export default {
this.$message.success(this.$t('preferences.config.import.success')) this.$message.success(this.$t('preferences.config.import.success'))
this.preferenceData = this.$store.preference.init() this.preferenceData = this.preferenceStore.init()
} }
catch (error) { catch (error) {
if (error.message) { if (error.message) {
@ -237,17 +176,9 @@ export default {
}, },
handleSave() { handleSave() {
this.$store.preference.setData(this.preferenceData) this.preferenceStore.setData(this.preferenceData)
this.$message.success(this.$t('preferences.config.save.placeholder')) this.$message.success(this.$t('preferences.config.save.placeholder'))
}, },
handleReset(type) {
this.preferenceData = {
...this.preferenceData,
...this.$store.preference.getDefaultData(type),
}
this.$store.preference.setData(this.preferenceData)
},
}, },
} }
</script> </script>