Skip to content

Commit 3132150

Browse files
authored
Revert "feat: optimize minapp cache with LRU (#8160)" (#8205)
This reverts commit f0043b4.
1 parent 24f7bac commit 3132150

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

src/renderer/src/hooks/useMinappPopup.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,6 @@ import {
1111
import { MinAppType } from '@renderer/types'
1212
import { useCallback } from 'react'
1313

14-
const updateLRUCache = <T extends { id: string }>(apps: T[], app: T, limitation: number): T[] => {
15-
const existingIndex = apps.findIndex((item) => item.id === app.id)
16-
17-
if (existingIndex !== -1) {
18-
// 找到已存在的 app,将其移动到最前面
19-
const updatedApps = [...apps]
20-
updatedApps.splice(existingIndex, 1)
21-
updatedApps.unshift(app)
22-
return updatedApps
23-
}
24-
25-
const updatedApps = [app, ...apps]
26-
27-
if (updatedApps.length > limitation) {
28-
return updatedApps.slice(0, limitation)
29-
}
30-
31-
return updatedApps
32-
}
33-
3414
/**
3515
* Usage:
3616
*
@@ -54,8 +34,21 @@ export const useMinappPopup = () => {
5434
const openMinapp = useCallback(
5535
(app: MinAppType, keepAlive: boolean = false) => {
5636
if (keepAlive) {
57-
const updatedApps = updateLRUCache(openedKeepAliveMinapps, app, maxKeepAliveMinapps)
58-
dispatch(setOpenedKeepAliveMinapps(updatedApps))
37+
// 如果小程序已经打开,只切换显示
38+
if (openedKeepAliveMinapps.some((item) => item.id === app.id)) {
39+
dispatch(setCurrentMinappId(app.id))
40+
dispatch(setMinappShow(true))
41+
return
42+
}
43+
44+
// 如果缓存数量未达上限,添加到缓存列表
45+
if (openedKeepAliveMinapps.length < maxKeepAliveMinapps) {
46+
dispatch(setOpenedKeepAliveMinapps([app, ...openedKeepAliveMinapps]))
47+
} else {
48+
// 缓存数量达到上限,移除最后一个,添加新的
49+
dispatch(setOpenedKeepAliveMinapps([app, ...openedKeepAliveMinapps.slice(0, maxKeepAliveMinapps - 1)]))
50+
}
51+
5952
dispatch(setOpenedOneOffMinapp(null))
6053
dispatch(setCurrentMinappId(app.id))
6154
dispatch(setMinappShow(true))

0 commit comments

Comments
 (0)