@@ -11,6 +11,12 @@ import { showShortcutToast } from './shortcutToast';
11
11
let actionTimeout : NodeJS . Timeout | null = null ;
12
12
const ACTION_DELAY = 300 ; // 毫秒
13
13
14
+ // 添加一个操作锁,记录最后一次操作的时间和动作
15
+ let lastActionInfo = {
16
+ action : '' ,
17
+ timestamp : 0
18
+ } ;
19
+
14
20
interface ShortcutConfig {
15
21
key : string ;
16
22
enabled : boolean ;
@@ -31,17 +37,33 @@ let appShortcuts: ShortcutsConfig = {};
31
37
* @param action 快捷键动作
32
38
*/
33
39
export async function handleShortcutAction ( action : string ) {
40
+ const now = Date . now ( ) ;
41
+
34
42
// 如果存在未完成的动作,则忽略当前请求
35
43
if ( actionTimeout ) {
36
- console . log ( '忽略快速连续的动作请求:' , action ) ;
44
+ console . log ( '[AppShortcuts] 忽略快速连续的动作请求:' , action ) ;
45
+ return ;
46
+ }
47
+
48
+ // 检查是否是同一个动作的重复触发(300ms内)
49
+ if ( lastActionInfo . action === action && now - lastActionInfo . timestamp < ACTION_DELAY ) {
50
+ console . log ( `[AppShortcuts] 忽略重复的 ${ action } 动作,距上次仅 ${ now - lastActionInfo . timestamp } ms` ) ;
37
51
return ;
38
52
}
53
+
54
+ // 更新最后一次操作信息
55
+ lastActionInfo = {
56
+ action,
57
+ timestamp : now
58
+ } ;
39
59
40
60
// 设置防抖锁
41
61
actionTimeout = setTimeout ( ( ) => {
42
62
actionTimeout = null ;
43
63
} , ACTION_DELAY ) ;
44
64
65
+ console . log ( `[AppShortcuts] 执行动作: ${ action } , 时间戳: ${ now } ` ) ;
66
+
45
67
const playerStore = usePlayerStore ( ) ;
46
68
const settingsStore = useSettingsStore ( ) ;
47
69
@@ -93,16 +115,19 @@ export async function handleShortcutAction(action: string) {
93
115
case 'toggleFavorite' : {
94
116
const isFavorite = playerStore . favoriteList . includes ( Number ( playerStore . playMusic . id ) ) ;
95
117
const numericId = Number ( playerStore . playMusic . id ) ;
118
+ console . log ( `[AppShortcuts] toggleFavorite 当前状态: ${ isFavorite } , ID: ${ numericId } ` ) ;
96
119
if ( isFavorite ) {
97
120
playerStore . removeFromFavorite ( numericId ) ;
121
+ console . log ( `[AppShortcuts] 已从收藏中移除: ${ numericId } ` ) ;
98
122
} else {
99
123
playerStore . addToFavorite ( numericId ) ;
124
+ console . log ( `[AppShortcuts] 已添加到收藏: ${ numericId } ` ) ;
100
125
}
101
126
showToast (
102
127
isFavorite
103
- ? t ( 'player.playBar.favorite ' , { name : playerStore . playMusic . name } )
104
- : t ( 'player.playBar.unFavorite ' , { name : playerStore . playMusic . name } ) ,
105
- isFavorite ? 'ri-heart-fill ' : 'ri-heart-line '
128
+ ? t ( 'player.playBar.unFavorite ' , { name : playerStore . playMusic . name } )
129
+ : t ( 'player.playBar.favorite ' , { name : playerStore . playMusic . name } ) ,
130
+ isFavorite ? 'ri-heart-line ' : 'ri-heart-fill '
106
131
) ;
107
132
break ;
108
133
}
@@ -114,10 +139,9 @@ export async function handleShortcutAction(action: string) {
114
139
console . error ( `执行快捷键动作 ${ action } 时出错:` , error ) ;
115
140
} finally {
116
141
// 确保在出错时也能清除超时
117
- if ( actionTimeout ) {
118
- clearTimeout ( actionTimeout ) ;
119
- actionTimeout = null ;
120
- }
142
+ clearTimeout ( actionTimeout ) ;
143
+ actionTimeout = null ;
144
+ console . log ( `[AppShortcuts] 动作完成: ${ action } , 时间戳: ${ Date . now ( ) } , 耗时: ${ Date . now ( ) - now } ms` ) ;
121
145
}
122
146
}
123
147
0 commit comments