refactor: optimized refresh logic after update fields for list type

This commit is contained in:
Lykin 2023-11-17 17:20:32 +08:00
parent 80e659e03a
commit 4db3909c6a
6 changed files with 107 additions and 61 deletions

View File

@ -1097,7 +1097,9 @@ func (b *browserService) SetHashValue(param types.SetHashParam) (resp types.JSRe
resp.Msg = fmt.Sprintf(`save to type "%s" fail: %s`, param.Format, err.Error()) resp.Msg = fmt.Sprintf(`save to type "%s" fail: %s`, param.Format, err.Error())
return return
} }
if len(param.RetDecode) > 0 && len(param.RetFormat) > 0 {
displayStr, _, _ = strutil.ConvertTo(saveStr, param.RetDecode, param.RetFormat) displayStr, _, _ = strutil.ConvertTo(saveStr, param.RetDecode, param.RetFormat)
}
var updated, added, removed []types.HashEntryItem var updated, added, removed []types.HashEntryItem
var replaced []types.HashReplaceItem var replaced []types.HashReplaceItem
var affect int64 var affect int64
@ -1293,8 +1295,7 @@ func (b *browserService) SetListItem(param types.SetListParam) (resp types.JSRes
client, ctx := item.client, item.ctx client, ctx := item.client, item.ctx
key := strutil.DecodeRedisKey(param.Key) key := strutil.DecodeRedisKey(param.Key)
str := strutil.DecodeRedisKey(param.Value) str := strutil.DecodeRedisKey(param.Value)
var removed []int64 var replaced, removed []types.ListReplaceItem
updated := map[int64]string{}
if len(str) <= 0 { if len(str) <= 0 {
// remove from list // remove from list
err = client.LSet(ctx, key, param.Index, "---VALUE_REMOVED_BY_TINY_RDM---").Err() err = client.LSet(ctx, key, param.Index, "---VALUE_REMOVED_BY_TINY_RDM---").Err()
@ -1308,7 +1309,9 @@ func (b *browserService) SetListItem(param types.SetListParam) (resp types.JSRes
resp.Msg = err.Error() resp.Msg = err.Error()
return return
} }
removed = append(removed, param.Index) removed = append(removed, types.ListReplaceItem{
Index: param.Index,
})
} else { } else {
// replace index value // replace index value
var saveStr string var saveStr string
@ -1321,13 +1324,24 @@ func (b *browserService) SetListItem(param types.SetListParam) (resp types.JSRes
resp.Msg = err.Error() resp.Msg = err.Error()
return return
} }
updated[param.Index] = saveStr var displayStr string
if len(param.RetDecode) > 0 && len(param.RetFormat) > 0 {
displayStr, _, _ = strutil.ConvertTo(saveStr, param.RetDecode, param.RetFormat)
}
replaced = append(replaced, types.ListReplaceItem{
Index: param.Index,
Value: saveStr,
DisplayValue: displayStr,
})
} }
resp.Success = true resp.Success = true
resp.Data = map[string]any{ resp.Data = struct {
"removed": removed, Removed []types.ListReplaceItem `json:"removed,omitempty"`
"updated": updated, Replaced []types.ListReplaceItem `json:"replaced,omitempty"`
}{
Removed: removed,
Replaced: replaced,
} }
return return
} }

View File

@ -57,6 +57,8 @@ type SetListParam struct {
Value any `json:"value"` Value any `json:"value"`
Format string `json:"format,omitempty"` Format string `json:"format,omitempty"`
Decode string `json:"decode,omitempty"` Decode string `json:"decode,omitempty"`
RetFormat string `json:"retFormat,omitempty"`
RetDecode string `json:"retDecode,omitempty"`
} }
type SetHashParam struct { type SetHashParam struct {

View File

@ -7,7 +7,7 @@ type ListEntryItem struct {
type ListReplaceItem struct { type ListReplaceItem struct {
Index int64 `json:"index"` Index int64 `json:"index"`
Value any `json:"v"` Value any `json:"v,omitempty"`
DisplayValue string `json:"dv,omitempty"` DisplayValue string `json:"dv,omitempty"`
} }

View File

@ -130,7 +130,7 @@ const saveEdit = async (pos, value, decode, format) => {
throw new Error('row not exists') throw new Error('row not exists')
} }
const { updated, success, msg } = await browserStore.updateListItem({ const { success, msg } = await browserStore.updateListItem({
server: props.name, server: props.name,
db: props.db, db: props.db,
key: keyName.value, key: keyName.value,
@ -140,13 +140,6 @@ const saveEdit = async (pos, value, decode, format) => {
format, format,
}) })
if (success) { if (success) {
row.v = updated[index] || ''
const { value: displayVal } = await browserStore.convertValue({
value: row.v,
decode: props.decode,
format: props.format,
})
row.dv = displayVal
$message.success(i18n.t('dialogue.save_value_succ')) $message.success(i18n.t('dialogue.save_value_succ'))
} else { } else {
$message.error(msg) $message.error(msg)
@ -332,12 +325,12 @@ defineExpose({
<!-- table --> <!-- table -->
<n-data-table <n-data-table
v-show="!inFullEdit" v-show="!inFullEdit"
:row-key="(row) => row.no"
:bordered="false" :bordered="false"
:bottom-bordered="false" :bottom-bordered="false"
:columns="columns" :columns="columns"
:data="props.value" :data="props.value"
:loading="props.loading" :loading="props.loading"
:row-key="(row) => row.no"
:row-props="rowProps" :row-props="rowProps"
:single-column="true" :single-column="true"
:single-line="false" :single-line="false"

View File

@ -1009,10 +1009,10 @@ const useBrowserStore = defineStore('browser', {
* @param {string} field * @param {string} field
* @param {string} [newField] * @param {string} [newField]
* @param {string} [value] * @param {string} [value]
* @param {string} [decode] * @param {decodeTypes} [decode]
* @param {string} [format] * @param {formatTypes} [format]
* @param {string} [retDecode] * @param {decodeTypes} [retDecode]
* @param {string} [retFormat] * @param {formatTypes} [retFormat]
* @param {boolean} [refresh] * @param {boolean} [refresh]
* @param {number} [index] index for retrieve affect entries quickly * @param {number} [index] index for retrieve affect entries quickly
* @returns {Promise<{[msg]: string, success: boolean, [updated]: {}}>} * @returns {Promise<{[msg]: string, success: boolean, [updated]: {}}>}
@ -1225,24 +1225,57 @@ const useBrowserStore = defineStore('browser', {
* @param {string|number[]} value * @param {string|number[]} value
* @param {decodeTypes} decode * @param {decodeTypes} decode
* @param {formatTypes} format * @param {formatTypes} format
* @returns {Promise<{[msg]: string, success: boolean, [updated]: {}}>} * @param {decodeTypes} [retDecode]
* @param {formatTypes} [retFormat]
* @returns {Promise<{[msg]: string, success: boolean}>}
*/ */
async updateListItem({ server, db, key, index, value, decode = decodeTypes.NONE, format = formatTypes.RAW }) { async updateListItem({
server,
db,
key,
index,
value,
decode = decodeTypes.NONE,
format = formatTypes.RAW,
retDecode,
retFormat,
}) {
try { try {
const { data, success, msg } = await SetListItem({ server, db, key, index, value, decode, format }) const { data, success, msg } = await SetListItem({
server,
db,
key,
index,
value,
decode,
format,
retDecode,
retFormat,
})
if (success) { if (success) {
const { updated = {} } = data /** @type {{replaced: ListReplaceItem[]}} **/
// if (!isEmpty(updated)) { const { replaced = [], removed = [] } = data
// const tab = useTabStore() const tab = useTabStore()
// tab.upsertValueEntries({ if (!isEmpty(replaced)) {
// server, tab.replaceValueEntries({
// db, server,
// key, db,
// type: 'list', key,
// entries: updated, type: 'list',
// }) entries: replaced,
// } })
return { success, updated } }
if (!isEmpty(removed)) {
const removedIndex = map(removed, 'index')
tab.removeValueEntries({
server,
db,
key,
type: 'list',
entries: removedIndex,
})
}
return { success }
} else { } else {
return { success, msg } return { success, msg }
} }
@ -1264,16 +1297,17 @@ const useBrowserStore = defineStore('browser', {
const { data, success, msg } = await SetListItem({ server, db, key, index }) const { data, success, msg } = await SetListItem({ server, db, key, index })
if (success) { if (success) {
const { removed = [] } = data const { removed = [] } = data
// if (!isEmpty(removed)) { const tab = useTabStore()
// const tab = useTabStore() if (!isEmpty(removed)) {
// tab.removeValueEntries({ const removedIndexes = map(removed, 'index')
// server, tab.removeValueEntries({
// db, server,
// key, db,
// type: 'list', key,
// entries: removed, type: 'list',
// }) entries: removedIndexes,
// } })
}
return { success, removed } return { success, removed }
} else { } else {
return { success, msg } return { success, msg }

View File

@ -276,7 +276,7 @@ const useTabStore = defineStore('tab', {
}, },
/** /**
* replace entries * update entries' value
* @param {string} server * @param {string} server
* @param {number} db * @param {number} db
* @param {string|number[]} key * @param {string|number[]} key
@ -333,7 +333,7 @@ const useTabStore = defineStore('tab', {
}, },
/** /**
* replace entry item key or field in value * replace entry item key or field in value(modify the index key)
* @param {string} server * @param {string} server
* @param {number} db * @param {number} db
* @param {string|number[]} key * @param {string|number[]} key
@ -348,11 +348,14 @@ const useTabStore = defineStore('tab', {
} }
switch (type.toLowerCase()) { switch (type.toLowerCase()) {
case 'list': // {index:number, v:string, dv:[string]}[] case 'list': // ListReplaceItem[]
tab.value = tab.value || [] tab.value = tab.value || []
for (const entry of entries) { for (const entry of entries) {
if (size(tab.value) < entry.index) { if (size(tab.value) > entry.index) {
tab.value[entry.index] = entry tab.value[entry.index] = {
v: entry.v,
dv: entry.dv,
}
} else { } else {
// out of range, append // out of range, append
tab.value.push(entry) tab.value.push(entry)
@ -361,7 +364,7 @@ const useTabStore = defineStore('tab', {
} }
break break
case 'hash': case 'hash': // HashReplaceItem[]
tab.value = tab.value || [] tab.value = tab.value || []
for (const idx of index) { for (const idx of index) {
const entry = get(tab.value, idx) const entry = get(tab.value, idx)
@ -445,7 +448,7 @@ const useTabStore = defineStore('tab', {
case 'list': // string[] | number[] case 'list': // string[] | number[]
tab.value = tab.value || [] tab.value = tab.value || []
if (typeof entries[0] === 'number') { if (typeof entries[0] === 'number') {
// remove by index // remove by index, sort by desc first
entries.sort((a, b) => b - a) entries.sort((a, b) => b - a)
const removed = pullAt(tab.value, ...entries) const removed = pullAt(tab.value, ...entries)
tab.length -= size(removed) tab.length -= size(removed)