From 3132150fb89ec0d1150c62aadc301477627bdcff Mon Sep 17 00:00:00 2001 From: fullex <106392080+0xfullex@users.noreply.github.com> Date: Wed, 16 Jul 2025 17:34:50 +0800 Subject: [PATCH] Revert "feat: optimize minapp cache with LRU (#8160)" (#8205) This reverts commit f0043b4be5ab57a0a1471544617f346ea757cc98. --- src/renderer/src/hooks/useMinappPopup.ts | 37 ++++++++++-------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/src/renderer/src/hooks/useMinappPopup.ts b/src/renderer/src/hooks/useMinappPopup.ts index 1c87cd92f0..010136ae2a 100644 --- a/src/renderer/src/hooks/useMinappPopup.ts +++ b/src/renderer/src/hooks/useMinappPopup.ts @@ -11,26 +11,6 @@ import { import { MinAppType } from '@renderer/types' import { useCallback } from 'react' -const updateLRUCache = (apps: T[], app: T, limitation: number): T[] => { - const existingIndex = apps.findIndex((item) => item.id === app.id) - - if (existingIndex !== -1) { - // 找到已存在的 app,将其移动到最前面 - const updatedApps = [...apps] - updatedApps.splice(existingIndex, 1) - updatedApps.unshift(app) - return updatedApps - } - - const updatedApps = [app, ...apps] - - if (updatedApps.length > limitation) { - return updatedApps.slice(0, limitation) - } - - return updatedApps -} - /** * Usage: * @@ -54,8 +34,21 @@ export const useMinappPopup = () => { const openMinapp = useCallback( (app: MinAppType, keepAlive: boolean = false) => { if (keepAlive) { - const updatedApps = updateLRUCache(openedKeepAliveMinapps, app, maxKeepAliveMinapps) - dispatch(setOpenedKeepAliveMinapps(updatedApps)) + // 如果小程序已经打开,只切换显示 + if (openedKeepAliveMinapps.some((item) => item.id === app.id)) { + dispatch(setCurrentMinappId(app.id)) + dispatch(setMinappShow(true)) + return + } + + // 如果缓存数量未达上限,添加到缓存列表 + if (openedKeepAliveMinapps.length < maxKeepAliveMinapps) { + dispatch(setOpenedKeepAliveMinapps([app, ...openedKeepAliveMinapps])) + } else { + // 缓存数量达到上限,移除最后一个,添加新的 + dispatch(setOpenedKeepAliveMinapps([app, ...openedKeepAliveMinapps.slice(0, maxKeepAliveMinapps - 1)])) + } + dispatch(setOpenedOneOffMinapp(null)) dispatch(setCurrentMinappId(app.id)) dispatch(setMinappShow(true))