@@ -11,26 +11,6 @@ import {
11
11
import { MinAppType } from '@renderer/types'
12
12
import { useCallback } from 'react'
13
13
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
-
34
14
/**
35
15
* Usage:
36
16
*
@@ -54,8 +34,21 @@ export const useMinappPopup = () => {
54
34
const openMinapp = useCallback (
55
35
( app : MinAppType , keepAlive : boolean = false ) => {
56
36
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
+
59
52
dispatch ( setOpenedOneOffMinapp ( null ) )
60
53
dispatch ( setCurrentMinappId ( app . id ) )
61
54
dispatch ( setMinappShow ( true ) )
0 commit comments