Skip to content

Revert "feat: optimize minapp cache with LRU (#8160)" #8205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 15 additions & 22 deletions src/renderer/src/hooks/useMinappPopup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,6 @@ import {
import { MinAppType } from '@renderer/types'
import { useCallback } from 'react'

const updateLRUCache = <T extends { id: string }>(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:
*
Expand All @@ -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))
Expand Down
Loading