perf(web): 使用map查找勾选的源

This commit is contained in:
Xwite 2023-05-06 22:58:36 +08:00
parent 771ff323fe
commit d27dd0a5a3
2 changed files with 15 additions and 38 deletions

View File

@ -50,24 +50,14 @@ import SourceItem from "./SourceItem.vue";
const store = useSourceStore();
const sourceUrlSelect = ref([]);
const searchKey = ref("");
const { sources } = storeToRefs(store);
const { sources, sourcesMap, sourceUrlKey } = storeToRefs(store);
const isBookSource = computed(() => {
return /bookSource/.test(window.location.href);
});
const sourceSelect = computed(() => {
let temp = sourceUrlSelect.value,
selectUrlsLength = temp.length;
if (selectUrlsLength == 0) return [];
let searchKey = "sourceUrl";
if (isBookSource.value) searchKey = "bookSourceUrl";
return sources.value.filter((source) => {
let searchIndex = temp.indexOf(source[searchKey]);
if (searchIndex > -1) {
temp.splice(searchIndex, 1);
return true;
}
return false;
});
if (sourceUrlSelect.value.length == 0) return [];
let searchKey = sourceUrlKey.value;
return sourceUrlSelect.value.map(sourceUrl => sourcesMap.value.get(sourceUrl));
});
const deleteSelectSources = () => {
API.deleteSource(sourceSelect.value).then(({ data }) => {

View File

@ -21,6 +21,12 @@ export const useSourceStore = defineStore("source", {
},
getters: {
sources: (state) => (isBookSource ? state.bookSources : state.rssSources),
sourceUrlKey: (state) => isBookSource ? "bookSourceUrl" : "sourceUrl",
sourcesMap: (state) => {
let map = new Map();
state.sources.forEach(source => map.set(source[state.sourceUrlKey], source));
return map;
},
currentSourceUrl: (state) =>
isBookSource
? state.currentSource.bookSourceUrl
@ -57,31 +63,13 @@ export const useSourceStore = defineStore("source", {
},
//保存当前编辑源
saveCurrentSource() {
let source = this.currentSource,
sources,
searchKey;
if (isBookSource) {
sources = this.bookSources;
searchKey = "bookSourceUrl";
} else {
sources = this.rssSources;
searchKey = "sourceUrl";
}
let index = sources.findIndex(
(element) => element[searchKey] === source[searchKey]
);
//去掉响应 toRaw?
source = JSON.parse(JSON.stringify(source));
if (index > -1) {
sources.splice(index, 1, source);
} else {
sources.push(source);
}
let source = this.currentSource, map = this.sourcesMap;
map.set(source[this.sourceUrlKey], source);
this.saveSources(Array.from(map.values()));
},
// 更改当前编辑的源
changeCurrentSource(source) {
const newContent = JSON.stringify(source);
this.currentSource = JSON.parse(newContent);
this.currentSource = JSON.parse(JSON.stringify((source)));
},
async setPushReturnSources(returnSoures) {
if (isBookSource) {
@ -104,8 +92,7 @@ export const useSourceStore = defineStore("source", {
localStorage.setItem("tabName", tabName);
},
changeEditTabSource(source) {
const newContent = JSON.stringify(source);
this.editTabSource = JSON.parse(newContent);
this.editTabSource = JSON.parse(JSON.stringify((source)));
},
editHistory(history) {
let historyObj;