reduce favorite tab memory usage

This commit is contained in:
pa
2025-10-28 10:57:22 +09:00
committed by Natsumi
parent d0cc76eb84
commit f5f60b048c
2 changed files with 23 additions and 21 deletions

View File

@@ -32,11 +32,11 @@ export const useFavoriteStore = defineStore('Favorite', () => {
favoriteAvatars_: [] favoriteAvatars_: []
}); });
let cachedFavorites = new Map(); const cachedFavorites = ref(new Map());
const currentFavoriteTab = ref('friend'); const currentFavoriteTab = ref('friend');
const cachedFavoriteGroups = ref(new Map()); const cachedFavoriteGroups = ref({});
const cachedFavoriteGroupsByTypeName = computed(() => { const cachedFavoriteGroupsByTypeName = computed(() => {
const group = {}; const group = {};
@@ -185,9 +185,9 @@ export const useFavoriteStore = defineStore('Favorite', () => {
() => watchState.isLoggedIn, () => watchState.isLoggedIn,
(isLoggedIn) => { (isLoggedIn) => {
friendStore.localFavoriteFriends.clear(); friendStore.localFavoriteFriends.clear();
cachedFavorites.clear(); cachedFavorites.value.clear();
cachedFavoritesByObjectId.value.clear(); cachedFavoritesByObjectId.value.clear();
cachedFavoriteGroups.value.clear(); cachedFavoriteGroups.value = {};
favoriteFriendGroups.value = []; favoriteFriendGroups.value = [];
favoriteWorldGroups.value = []; favoriteWorldGroups.value = [];
favoriteAvatarGroups.value = []; favoriteAvatarGroups.value = [];
@@ -309,7 +309,7 @@ export const useFavoriteStore = defineStore('Favorite', () => {
function handleFavoriteGroupClear(args) { function handleFavoriteGroupClear(args) {
const key = `${args.params.type}:${args.params.group}`; const key = `${args.params.type}:${args.params.group}`;
for (const ref of cachedFavorites.values()) { for (const ref of cachedFavorites.value.values()) {
if (ref.$isDeleted || ref.$groupKey !== key) { if (ref.$isDeleted || ref.$groupKey !== key) {
continue; continue;
} }
@@ -346,7 +346,7 @@ export const useFavoriteStore = defineStore('Favorite', () => {
function expireFavorites() { function expireFavorites() {
friendStore.localFavoriteFriends.clear(); friendStore.localFavoriteFriends.clear();
cachedFavorites.clear(); cachedFavorites.value.clear();
cachedFavoritesByObjectId.value.clear(); cachedFavoritesByObjectId.value.clear();
state.favoriteObjects.clear(); state.favoriteObjects.clear();
state.favoriteFriends_ = []; state.favoriteFriends_ = [];
@@ -412,7 +412,6 @@ export const useFavoriteStore = defineStore('Favorite', () => {
name: '', name: '',
$selected: false $selected: false
}; };
state.favoriteObjects.set(objectId, ctx);
if (type === 'friend') { if (type === 'friend') {
ref = userStore.cachedUsers.get(objectId); ref = userStore.cachedUsers.get(objectId);
if (typeof ref === 'undefined') { if (typeof ref === 'undefined') {
@@ -437,6 +436,7 @@ export const useFavoriteStore = defineStore('Favorite', () => {
ctx.name = ref.name; ctx.name = ref.name;
} }
} }
state.favoriteObjects.set(objectId, ctx);
isTypeChanged = true; isTypeChanged = true;
} else { } else {
if (ctx.type !== type) { if (ctx.type !== type) {
@@ -575,13 +575,14 @@ export const useFavoriteStore = defineStore('Favorite', () => {
} }
function expireFavoriteGroups() { function expireFavoriteGroups() {
for (const ref of cachedFavoriteGroups.value.values()) { for (const key in cachedFavoriteGroups.value) {
ref.$isExpired = true; cachedFavoriteGroups.value[key].$isExpired = true;
} }
} }
function deleteExpiredFavoriteGroups() { function deleteExpiredFavoriteGroups() {
for (const ref of cachedFavoriteGroups.value.values()) { for (const key in cachedFavoriteGroups.value) {
const ref = cachedFavoriteGroups.value[key];
if (ref.$isDeleted || ref.$isExpired === false) { if (ref.$isDeleted || ref.$isExpired === false) {
continue; continue;
} }
@@ -645,7 +646,8 @@ export const useFavoriteStore = defineStore('Favorite', () => {
}; };
const assigns = new Set(); const assigns = new Set();
// assign the same name first // assign the same name first
for (ref of cachedFavoriteGroups.value.values()) { for (const key in cachedFavoriteGroups.value) {
const ref = cachedFavoriteGroups.value[key];
if (ref.$isDeleted) { if (ref.$isDeleted) {
continue; continue;
} }
@@ -666,7 +668,8 @@ export const useFavoriteStore = defineStore('Favorite', () => {
} }
} }
for (ref of cachedFavoriteGroups.value.values()) { for (const key in cachedFavoriteGroups.value) {
const ref = cachedFavoriteGroups.value[key];
if (ref.$isDeleted || assigns.has(ref.id)) { if (ref.$isDeleted || assigns.has(ref.id)) {
continue; continue;
} }
@@ -687,7 +690,7 @@ export const useFavoriteStore = defineStore('Favorite', () => {
} }
// update favorites // update favorites
for (ref of cachedFavorites.values()) { for (ref of cachedFavorites.value.values()) {
if (ref.$isDeleted) { if (ref.$isDeleted) {
continue; continue;
} }
@@ -755,7 +758,7 @@ export const useFavoriteStore = defineStore('Favorite', () => {
* @returns {any} * @returns {any}
*/ */
function applyFavoriteGroup(json) { function applyFavoriteGroup(json) {
let ref = cachedFavoriteGroups.value.get(json.id); let ref = cachedFavoriteGroups.value[json.id];
if (typeof ref === 'undefined') { if (typeof ref === 'undefined') {
ref = { ref = {
id: '', id: '',
@@ -772,7 +775,7 @@ export const useFavoriteStore = defineStore('Favorite', () => {
// //
...json ...json
}; };
cachedFavoriteGroups.value.set(ref.id, ref); cachedFavoriteGroups.value[ref.id] = ref;
} else { } else {
Object.assign(ref, json); Object.assign(ref, json);
ref.$isExpired = false; ref.$isExpired = false;
@@ -786,7 +789,7 @@ export const useFavoriteStore = defineStore('Favorite', () => {
* @returns {any} * @returns {any}
*/ */
function applyFavoriteCached(json) { function applyFavoriteCached(json) {
let ref = cachedFavorites.get(json.id); let ref = cachedFavorites.value.get(json.id);
if (typeof ref === 'undefined') { if (typeof ref === 'undefined') {
ref = { ref = {
id: '', id: '',
@@ -800,7 +803,7 @@ export const useFavoriteStore = defineStore('Favorite', () => {
// //
...json ...json
}; };
cachedFavorites.set(ref.id, ref); cachedFavorites.value.set(ref.id, ref);
cachedFavoritesByObjectId.value.set(ref.favoriteId, ref); cachedFavoritesByObjectId.value.set(ref.favoriteId, ref);
if ( if (
ref.type === 'friend' && ref.type === 'friend' &&
@@ -831,7 +834,7 @@ export const useFavoriteStore = defineStore('Favorite', () => {
* *
*/ */
function deleteExpiredFavorites() { function deleteExpiredFavorites() {
for (const ref of cachedFavorites.values()) { for (const ref of cachedFavorites.value.values()) {
if (ref.$isDeleted || ref.$isExpired === false) { if (ref.$isDeleted || ref.$isExpired === false) {
continue; continue;
} }
@@ -869,7 +872,7 @@ export const useFavoriteStore = defineStore('Favorite', () => {
avatar: [0, favoriteRequest.getFavoriteAvatars] avatar: [0, favoriteRequest.getFavoriteAvatars]
}; };
const tags = []; const tags = [];
for (const ref of cachedFavorites.values()) { for (const ref of cachedFavorites.value.values()) {
if (ref.$isDeleted) { if (ref.$isDeleted) {
continue; continue;
} }

View File

@@ -241,9 +241,8 @@ export const useFriendStore = defineStore('Friend', () => {
function updateLocalFavoriteFriends() { function updateLocalFavoriteFriends() {
const favoriteStore = useFavoriteStore(); const favoriteStore = useFavoriteStore();
const { cachedFavorites } = favoriteStore;
localFavoriteFriends.value.clear(); localFavoriteFriends.value.clear();
for (const ref of cachedFavorites.values()) { for (const ref of favoriteStore.cachedFavorites.values()) {
if ( if (
!ref.$isDeleted && !ref.$isDeleted &&
ref.type === 'friend' && ref.type === 'friend' &&