From 4db3909c6a4fd98478d7ccb70aa4273eca26b0b3 Mon Sep 17 00:00:00 2001 From: Lykin <137850705+tiny-craft@users.noreply.github.com> Date: Fri, 17 Nov 2023 17:20:32 +0800 Subject: [PATCH] refactor: optimized refresh logic after update fields for list type --- backend/services/browser_service.go | 30 ++++-- backend/types/js_resp.go | 16 ++-- backend/types/redis_wrapper.go | 2 +- .../content_value/ContentValueList.vue | 11 +-- frontend/src/stores/browser.js | 92 +++++++++++++------ frontend/src/stores/tab.js | 17 ++-- 6 files changed, 107 insertions(+), 61 deletions(-) diff --git a/backend/services/browser_service.go b/backend/services/browser_service.go index 727c9f2..e76f49d 100644 --- a/backend/services/browser_service.go +++ b/backend/services/browser_service.go @@ -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()) return } - displayStr, _, _ = strutil.ConvertTo(saveStr, param.RetDecode, param.RetFormat) + if len(param.RetDecode) > 0 && len(param.RetFormat) > 0 { + displayStr, _, _ = strutil.ConvertTo(saveStr, param.RetDecode, param.RetFormat) + } var updated, added, removed []types.HashEntryItem var replaced []types.HashReplaceItem var affect int64 @@ -1293,8 +1295,7 @@ func (b *browserService) SetListItem(param types.SetListParam) (resp types.JSRes client, ctx := item.client, item.ctx key := strutil.DecodeRedisKey(param.Key) str := strutil.DecodeRedisKey(param.Value) - var removed []int64 - updated := map[int64]string{} + var replaced, removed []types.ListReplaceItem if len(str) <= 0 { // remove from list 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() return } - removed = append(removed, param.Index) + removed = append(removed, types.ListReplaceItem{ + Index: param.Index, + }) } else { // replace index value var saveStr string @@ -1321,13 +1324,24 @@ func (b *browserService) SetListItem(param types.SetListParam) (resp types.JSRes resp.Msg = err.Error() 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.Data = map[string]any{ - "removed": removed, - "updated": updated, + resp.Data = struct { + Removed []types.ListReplaceItem `json:"removed,omitempty"` + Replaced []types.ListReplaceItem `json:"replaced,omitempty"` + }{ + Removed: removed, + Replaced: replaced, } return } diff --git a/backend/types/js_resp.go b/backend/types/js_resp.go index 25c6f29..ddee3bf 100644 --- a/backend/types/js_resp.go +++ b/backend/types/js_resp.go @@ -50,13 +50,15 @@ type SetKeyParam struct { } type SetListParam struct { - Server string `json:"server"` - DB int `json:"db"` - Key any `json:"key"` - Index int64 `json:"index"` - Value any `json:"value"` - Format string `json:"format,omitempty"` - Decode string `json:"decode,omitempty"` + Server string `json:"server"` + DB int `json:"db"` + Key any `json:"key"` + Index int64 `json:"index"` + Value any `json:"value"` + Format string `json:"format,omitempty"` + Decode string `json:"decode,omitempty"` + RetFormat string `json:"retFormat,omitempty"` + RetDecode string `json:"retDecode,omitempty"` } type SetHashParam struct { diff --git a/backend/types/redis_wrapper.go b/backend/types/redis_wrapper.go index 2167211..46adb19 100644 --- a/backend/types/redis_wrapper.go +++ b/backend/types/redis_wrapper.go @@ -7,7 +7,7 @@ type ListEntryItem struct { type ListReplaceItem struct { Index int64 `json:"index"` - Value any `json:"v"` + Value any `json:"v,omitempty"` DisplayValue string `json:"dv,omitempty"` } diff --git a/frontend/src/components/content_value/ContentValueList.vue b/frontend/src/components/content_value/ContentValueList.vue index 4d3427a..fe1717f 100644 --- a/frontend/src/components/content_value/ContentValueList.vue +++ b/frontend/src/components/content_value/ContentValueList.vue @@ -130,7 +130,7 @@ const saveEdit = async (pos, value, decode, format) => { throw new Error('row not exists') } - const { updated, success, msg } = await browserStore.updateListItem({ + const { success, msg } = await browserStore.updateListItem({ server: props.name, db: props.db, key: keyName.value, @@ -140,13 +140,6 @@ const saveEdit = async (pos, value, decode, format) => { format, }) 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')) } else { $message.error(msg) @@ -332,12 +325,12 @@ defineExpose({ } @@ -1225,24 +1225,57 @@ const useBrowserStore = defineStore('browser', { * @param {string|number[]} value * @param {decodeTypes} decode * @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 { - 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) { - const { updated = {} } = data - // if (!isEmpty(updated)) { - // const tab = useTabStore() - // tab.upsertValueEntries({ - // server, - // db, - // key, - // type: 'list', - // entries: updated, - // }) - // } - return { success, updated } + /** @type {{replaced: ListReplaceItem[]}} **/ + const { replaced = [], removed = [] } = data + const tab = useTabStore() + if (!isEmpty(replaced)) { + tab.replaceValueEntries({ + server, + db, + key, + type: 'list', + entries: replaced, + }) + } + if (!isEmpty(removed)) { + const removedIndex = map(removed, 'index') + tab.removeValueEntries({ + server, + db, + key, + type: 'list', + entries: removedIndex, + }) + } + return { success } } else { return { success, msg } } @@ -1264,16 +1297,17 @@ const useBrowserStore = defineStore('browser', { const { data, success, msg } = await SetListItem({ server, db, key, index }) if (success) { const { removed = [] } = data - // if (!isEmpty(removed)) { - // const tab = useTabStore() - // tab.removeValueEntries({ - // server, - // db, - // key, - // type: 'list', - // entries: removed, - // }) - // } + const tab = useTabStore() + if (!isEmpty(removed)) { + const removedIndexes = map(removed, 'index') + tab.removeValueEntries({ + server, + db, + key, + type: 'list', + entries: removedIndexes, + }) + } return { success, removed } } else { return { success, msg } diff --git a/frontend/src/stores/tab.js b/frontend/src/stores/tab.js index 0b17f97..4385b8c 100644 --- a/frontend/src/stores/tab.js +++ b/frontend/src/stores/tab.js @@ -276,7 +276,7 @@ const useTabStore = defineStore('tab', { }, /** - * replace entries + * update entries' value * @param {string} server * @param {number} db * @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 {number} db * @param {string|number[]} key @@ -348,11 +348,14 @@ const useTabStore = defineStore('tab', { } switch (type.toLowerCase()) { - case 'list': // {index:number, v:string, dv:[string]}[] + case 'list': // ListReplaceItem[] tab.value = tab.value || [] for (const entry of entries) { - if (size(tab.value) < entry.index) { - tab.value[entry.index] = entry + if (size(tab.value) > entry.index) { + tab.value[entry.index] = { + v: entry.v, + dv: entry.dv, + } } else { // out of range, append tab.value.push(entry) @@ -361,7 +364,7 @@ const useTabStore = defineStore('tab', { } break - case 'hash': + case 'hash': // HashReplaceItem[] tab.value = tab.value || [] for (const idx of index) { const entry = get(tab.value, idx) @@ -445,7 +448,7 @@ const useTabStore = defineStore('tab', { case 'list': // string[] | number[] tab.value = tab.value || [] if (typeof entries[0] === 'number') { - // remove by index、 + // remove by index, sort by desc first entries.sort((a, b) => b - a) const removed = pullAt(tab.value, ...entries) tab.length -= size(removed)