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 store = useSourceStore();
const sourceUrlSelect = ref([]); const sourceUrlSelect = ref([]);
const searchKey = ref(""); const searchKey = ref("");
const { sources } = storeToRefs(store); const { sources, sourcesMap, sourceUrlKey } = storeToRefs(store);
const isBookSource = computed(() => { const isBookSource = computed(() => {
return /bookSource/.test(window.location.href); return /bookSource/.test(window.location.href);
}); });
const sourceSelect = computed(() => { const sourceSelect = computed(() => {
let temp = sourceUrlSelect.value, if (sourceUrlSelect.value.length == 0) return [];
selectUrlsLength = temp.length; let searchKey = sourceUrlKey.value;
if (selectUrlsLength == 0) return []; return sourceUrlSelect.value.map(sourceUrl => sourcesMap.value.get(sourceUrl));
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;
});
}); });
const deleteSelectSources = () => { const deleteSelectSources = () => {
API.deleteSource(sourceSelect.value).then(({ data }) => { API.deleteSource(sourceSelect.value).then(({ data }) => {

View File

@ -21,6 +21,12 @@ export const useSourceStore = defineStore("source", {
}, },
getters: { getters: {
sources: (state) => (isBookSource ? state.bookSources : state.rssSources), 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) => currentSourceUrl: (state) =>
isBookSource isBookSource
? state.currentSource.bookSourceUrl ? state.currentSource.bookSourceUrl
@ -57,31 +63,13 @@ export const useSourceStore = defineStore("source", {
}, },
//保存当前编辑源 //保存当前编辑源
saveCurrentSource() { saveCurrentSource() {
let source = this.currentSource, let source = this.currentSource, map = this.sourcesMap;
sources, map.set(source[this.sourceUrlKey], source);
searchKey; this.saveSources(Array.from(map.values()));
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);
}
}, },
// 更改当前编辑的源 // 更改当前编辑的源
changeCurrentSource(source) { changeCurrentSource(source) {
const newContent = JSON.stringify(source); this.currentSource = JSON.parse(JSON.stringify((source)));
this.currentSource = JSON.parse(newContent);
}, },
async setPushReturnSources(returnSoures) { async setPushReturnSources(returnSoures) {
if (isBookSource) { if (isBookSource) {
@ -104,8 +92,7 @@ export const useSourceStore = defineStore("source", {
localStorage.setItem("tabName", tabName); localStorage.setItem("tabName", tabName);
}, },
changeEditTabSource(source) { changeEditTabSource(source) {
const newContent = JSON.stringify(source); this.editTabSource = JSON.parse(JSON.stringify((source)));
this.editTabSource = JSON.parse(newContent);
}, },
editHistory(history) { editHistory(history) {
let historyObj; let historyObj;