refactor: replace some 'watch' with 'watchEffect'

This commit is contained in:
Lykin 2024-01-03 10:57:46 +08:00
parent 84b73bd5e7
commit 18f1b976c6
9 changed files with 114 additions and 137 deletions

View File

@ -1,7 +1,7 @@
<script setup>
import ContentPane from './components/content/ContentPane.vue'
import BrowserPane from './components/sidebar/BrowserPane.vue'
import { computed, onMounted, reactive, ref, watch } from 'vue'
import { computed, onMounted, reactive, ref, watchEffect } from 'vue'
import { debounce } from 'lodash'
import { useThemeVars } from 'naive-ui'
import Ribbon from './components/sidebar/Ribbon.vue'
@ -43,14 +43,11 @@ const handleResize = () => {
saveSidebarWidth()
}
watch(
() => tabStore.nav,
(nav) => {
if (nav === 'log') {
logPaneRef.value?.refresh()
}
},
)
watchEffect(() => {
if (tabStore.nav === 'log') {
logPaneRef.value?.refresh()
}
})
const logoWrapperWidth = computed(() => {
return `${data.navMenuWidth + prefStore.behavior.asideWidth - 4}px`

View File

@ -1,5 +1,5 @@
<script setup>
import { computed, reactive, watch } from 'vue'
import { computed, reactive, watchEffect } from 'vue'
import { types } from '@/consts/support_redis_type.js'
import useDialog from 'stores/dialog'
import NewStringValue from '@/components/new_value/NewStringValue.vue'
@ -63,21 +63,18 @@ const title = computed(() => {
})
const dialogStore = useDialog()
watch(
() => dialogStore.addFieldsDialogVisible,
(visible) => {
if (visible) {
const { server, db, key, keyCode, type } = dialogStore.addFieldParam
newForm.server = server
newForm.db = db
newForm.key = key
newForm.keyCode = keyCode
newForm.type = type
newForm.opType = 0
newForm.value = null
}
},
)
watchEffect(() => {
if (dialogStore.addFieldsDialogVisible) {
const { server, db, key, keyCode, type } = dialogStore.addFieldParam
newForm.server = server
newForm.db = db
newForm.key = key
newForm.keyCode = keyCode
newForm.type = type
newForm.opType = 0
newForm.value = null
}
})
const browserStore = useBrowserStore()
const tab = useTabStore()

View File

@ -1,5 +1,5 @@
<script setup>
import { computed, nextTick, reactive, ref, watch } from 'vue'
import { computed, nextTick, reactive, ref, watchEffect } from 'vue'
import useDialog from 'stores/dialog'
import { useI18n } from 'vue-i18n'
import { isEmpty, map, size } from 'lodash'
@ -18,28 +18,26 @@ const deleteForm = reactive({
const dialogStore = useDialog()
const browserStore = useBrowserStore()
watch(
() => dialogStore.deleteKeyDialogVisible,
(visible) => {
if (visible) {
const { server, db, key } = dialogStore.deleteKeyParam
deleteForm.server = server
deleteForm.db = db
deleteForm.key = key
deleteForm.loadingAffected = false
// deleteForm.async = true
loading.value = false
deleting.value = false
if (key instanceof Array) {
deleteForm.showAffected = true
deleteForm.affectedKeys = key
} else {
deleteForm.showAffected = false
deleteForm.affectedKeys = []
}
watchEffect(() => {
if (dialogStore.deleteKeyDialogVisible) {
const { server, db, key } = dialogStore.deleteKeyParam
deleteForm.server = server
deleteForm.db = db
deleteForm.key = key
deleteForm.loadingAffected = false
// deleteForm.async = true
loading.value = false
deleting.value = false
if (key instanceof Array) {
deleteForm.showAffected = true
deleteForm.affectedKeys = key
} else {
deleteForm.showAffected = false
deleteForm.affectedKeys = []
}
},
)
}
})
const loading = ref(false)
const deleting = ref(false)

View File

@ -1,5 +1,5 @@
<script setup>
import { reactive, ref, watch } from 'vue'
import { reactive, ref, watchEffect } from 'vue'
import useDialog from 'stores/dialog'
import { useI18n } from 'vue-i18n'
import useBrowserStore from 'stores/browser.js'
@ -14,19 +14,17 @@ const flushForm = reactive({
const dialogStore = useDialog()
const browserStore = useBrowserStore()
watch(
() => dialogStore.flushDBDialogVisible,
(visible) => {
if (visible) {
const { server, db } = dialogStore.flushDBParam
flushForm.server = server
flushForm.db = db
flushForm.async = true
flushForm.confirm = false
loading.value = false
}
},
)
watchEffect(() => {
if (dialogStore.flushDBDialogVisible) {
const { server, db } = dialogStore.flushDBParam
flushForm.server = server
flushForm.db = db
flushForm.async = true
flushForm.confirm = false
loading.value = false
}
})
const loading = ref(false)
const i18n = useI18n()

View File

@ -1,5 +1,5 @@
<script setup>
import { computed, reactive, ref, watch } from 'vue'
import { computed, reactive, ref, watchEffect } from 'vue'
import useDialog from 'stores/dialog'
import { useI18n } from 'vue-i18n'
import useConnectionStore from 'stores/connections.js'
@ -36,14 +36,11 @@ const isRenameMode = computed(() => !isEmpty(editGroup.value))
const dialogStore = useDialog()
const connectionStore = useConnectionStore()
watch(
() => dialogStore.groupDialogVisible,
(visible) => {
if (visible) {
groupForm.name = editGroup.value = dialogStore.editGroup
}
},
)
watchEffect(() => {
if (dialogStore.groupDialogVisible) {
groupForm.name = editGroup.value = dialogStore.editGroup
}
})
const i18n = useI18n()
const onConfirm = async () => {

View File

@ -1,5 +1,5 @@
<script setup>
import { computed, reactive, ref, watch } from 'vue'
import { computed, reactive, ref, watchEffect } from 'vue'
import useDialog from 'stores/dialog'
import { useI18n } from 'vue-i18n'
import { types } from '@/consts/support_redis_type.js'
@ -26,18 +26,16 @@ const typeOptions = computed(() => {
})
const dialogStore = useDialog()
watch(
() => dialogStore.keyFilterDialogVisible,
(visible) => {
if (visible) {
const { server, db, type, pattern } = dialogStore.keyFilterParam
filterForm.server = server
filterForm.db = db || 0
filterForm.type = type || ''
filterForm.pattern = pattern || '*'
}
},
)
watchEffect(() => {
if (dialogStore.keyFilterDialogVisible) {
const { server, db, type, pattern } = dialogStore.keyFilterParam
filterForm.server = server
filterForm.db = db || 0
filterForm.type = type || ''
filterForm.pattern = pattern || '*'
}
})
const browserStore = useBrowserStore()
const onConfirm = () => {}

View File

@ -1,5 +1,5 @@
<script setup>
import { computed, h, nextTick, reactive, ref, watch } from 'vue'
import { computed, h, nextTick, reactive, ref, watchEffect } from 'vue'
import { types, typesColor } from '@/consts/support_redis_type.js'
import useDialog from 'stores/dialog'
import { endsWith, get, isEmpty, keys, map, trim } from 'lodash'
@ -64,29 +64,26 @@ const defaultValue = {
}
const dialogStore = useDialog()
watch(
() => dialogStore.newKeyDialogVisible,
(visible) => {
if (visible) {
const { prefix, server, db } = dialogStore.newKeyParam
const separator = browserStore.getSeparator(server)
newForm.server = server
if (isEmpty(prefix)) {
newForm.key = ''
watchEffect(() => {
if (dialogStore.newKeyDialogVisible) {
const { prefix, server, db } = dialogStore.newKeyParam
const separator = browserStore.getSeparator(server)
newForm.server = server
if (isEmpty(prefix)) {
newForm.key = ''
} else {
if (!endsWith(prefix, separator)) {
newForm.key = prefix + separator
} else {
if (!endsWith(prefix, separator)) {
newForm.key = prefix + separator
} else {
newForm.key = prefix
}
newForm.key = prefix
}
newForm.db = db
newForm.type = options.value[0].value
newForm.ttl = -1
newForm.value = null
}
},
)
newForm.db = db
newForm.type = options.value[0].value
newForm.ttl = -1
newForm.value = null
}
})
const renderTypeLabel = (option) => {
return h(

View File

@ -1,5 +1,5 @@
<script setup>
import { reactive, watch } from 'vue'
import { reactive, watchEffect } from 'vue'
import useDialog from 'stores/dialog'
import { useI18n } from 'vue-i18n'
import useBrowserStore from 'stores/browser.js'
@ -15,18 +15,16 @@ const renameForm = reactive({
const dialogStore = useDialog()
const browserStore = useBrowserStore()
const tab = useTabStore()
watch(
() => dialogStore.renameDialogVisible,
(visible) => {
if (visible) {
const { server, db, key } = dialogStore.renameKeyParam
renameForm.server = server
renameForm.db = db
renameForm.key = key
renameForm.newKey = key
}
},
)
watchEffect(() => {
if (dialogStore.renameDialogVisible) {
const { server, db, key } = dialogStore.renameKeyParam
renameForm.server = server
renameForm.db = db
renameForm.key = key
renameForm.newKey = key
}
})
const i18n = useI18n()
const onRename = async () => {

View File

@ -1,5 +1,5 @@
<script setup>
import { reactive, watch } from 'vue'
import { reactive, watchEffect } from 'vue'
import useDialog from 'stores/dialog'
import useTabStore from 'stores/tab.js'
import Binary from '@/components/icons/Binary.vue'
@ -18,27 +18,24 @@ const dialogStore = useDialog()
const browserStore = useBrowserStore()
const tabStore = useTabStore()
watch(
() => dialogStore.ttlDialogVisible,
(visible) => {
if (visible) {
// get ttl from current tab
const tab = tabStore.currentTab
if (tab != null) {
ttlForm.server = tab.name
ttlForm.db = tab.db
ttlForm.key = tab.key
ttlForm.keyCode = tab.keyCode
if (tab.ttl < 0) {
// forever
ttlForm.ttl = -1
} else {
ttlForm.ttl = tab.ttl
}
watchEffect(() => {
if (dialogStore.ttlDialogVisible) {
// get ttl from current tab
const tab = tabStore.currentTab
if (tab != null) {
ttlForm.server = tab.name
ttlForm.db = tab.db
ttlForm.key = tab.key
ttlForm.keyCode = tab.keyCode
if (tab.ttl < 0) {
// forever
ttlForm.ttl = -1
} else {
ttlForm.ttl = tab.ttl
}
}
},
)
}
})
const onClose = () => {
dialogStore.closeTTLDialog()