Skip to content

Commit cb58abb

Browse files
committed
🔧 chore: 优化操作锁逻辑,添加超时检查机制,确保操作锁在超时后自动释放
1 parent e2527c3 commit cb58abb

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/renderer/services/audioService.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class AudioService {
4545
private operationLock = false;
4646
private operationLockTimer: NodeJS.Timeout | null = null;
4747
private operationLockTimeout = 5000; // 5秒超时
48+
private operationLockStartTime: number = 0;
4849

4950
constructor() {
5051
if ('mediaSession' in navigator) {
@@ -363,11 +364,19 @@ class AudioService {
363364

364365
// 设置操作锁,带超时自动释放
365366
private setOperationLock(): boolean {
367+
// 如果锁已经存在,检查是否超时
366368
if (this.operationLock) {
369+
const currentTime = Date.now();
370+
if (currentTime - this.operationLockStartTime > this.operationLockTimeout) {
371+
console.warn('操作锁已超时,强制释放');
372+
this.releaseOperationLock();
373+
return true;
374+
}
367375
return false;
368376
}
369377

370378
this.operationLock = true;
379+
this.operationLockStartTime = Date.now();
371380

372381
// 清除之前的定时器
373382
if (this.operationLockTimer) {
@@ -377,8 +386,7 @@ class AudioService {
377386
// 设置超时自动释放锁
378387
this.operationLockTimer = setTimeout(() => {
379388
console.warn('操作锁超时自动释放');
380-
this.operationLock = false;
381-
this.operationLockTimer = null;
389+
this.releaseOperationLock();
382390
}, this.operationLockTimeout);
383391

384392
return true;
@@ -387,6 +395,7 @@ class AudioService {
387395
// 释放操作锁
388396
private releaseOperationLock(): void {
389397
this.operationLock = false;
398+
this.operationLockStartTime = 0;
390399

391400
if (this.operationLockTimer) {
392401
clearTimeout(this.operationLockTimer);
@@ -485,6 +494,7 @@ class AudioService {
485494
} else {
486495
// 发送URL过期事件,通知外部需要重新获取URL
487496
this.emit('url_expired', this.currentTrack);
497+
this.releaseOperationLock();
488498
reject(new Error('音频加载失败,请尝试切换其他歌曲'));
489499
}
490500
},
@@ -497,6 +507,7 @@ class AudioService {
497507
} else {
498508
// 发送URL过期事件,通知外部需要重新获取URL
499509
this.emit('url_expired', this.currentTrack);
510+
this.releaseOperationLock();
500511
reject(new Error('音频播放失败,请尝试切换其他歌曲'));
501512
}
502513
},

0 commit comments

Comments
 (0)