Skip to content

Commit 84edb33

Browse files
committed
refactor: 优化远程剪贴板连接逻辑,移除不必要的参数传递
1 parent beb4427 commit 84edb33

2 files changed

Lines changed: 29 additions & 26 deletions

File tree

src/longRunningTask/RemoteClipboardMonitorTask.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,23 @@ class RemoteClipboardMonitorTask extends LongRunningTask {
2121
readonly name = 'remoteClipboardMonitor';
2222

2323
private _activeServer: ServerConfig | null = null;
24+
private _activePollingInterval: number | undefined = undefined;
2425

2526
async start(): Promise<void> {
2627
const server = await configService.getActiveServer();
2728
if (!server) {
2829
clipboardSyncState.setRemoteContent(null);
2930
return;
3031
}
31-
this._activeServer = server;
3232
const config = await configService.getConfig();
33-
await remoteClipboardMonitor.connect(server, config?.remotePollingInterval);
33+
this._activeServer = server;
34+
this._activePollingInterval = config?.remotePollingInterval;
35+
await remoteClipboardMonitor.connect();
3436
}
3537

3638
async stop(): Promise<void> {
3739
this._activeServer = null;
40+
this._activePollingInterval = undefined;
3841
await remoteClipboardMonitor.disconnect();
3942
}
4043

@@ -44,20 +47,23 @@ class RemoteClipboardMonitorTask extends LongRunningTask {
4447

4548
override async onConfigChanged(): Promise<void> {
4649
const newServer = await configService.getActiveServer();
50+
const config = await configService.getConfig();
51+
const newPollingInterval = config?.remotePollingInterval;
52+
4753
const serverChanged = JSON.stringify(newServer) !== JSON.stringify(this._activeServer);
54+
const pollingIntervalChanged = newPollingInterval !== this._activePollingInterval;
55+
56+
if (!newServer) {
57+
await this.stop();
58+
clipboardSyncState.setRemoteContent(null);
59+
return;
60+
}
4861

49-
if (serverChanged) {
62+
if (serverChanged || pollingIntervalChanged) {
5063
await this.stop();
51-
if (newServer) {
52-
await this.start();
53-
} else {
54-
clipboardSyncState.setRemoteContent(null);
55-
}
56-
} else if (this._activeServer) {
57-
const config = await configService.getConfig();
58-
if (!remoteClipboardMonitor.isConnected()) {
59-
await remoteClipboardMonitor.connect(this._activeServer, config?.remotePollingInterval);
60-
}
64+
await this.start();
65+
} else if (!remoteClipboardMonitor.isConnected()) {
66+
await this.start();
6167
}
6268
}
6369

src/services/sync/RemoteClipboardMonitor.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { setTimer, clearTimer } from 'native-timer';
1212
import { getAPIClient } from '../ClientFactory';
1313
import { profileDtoToContent } from '../../utils/clipboard/convert';
1414
import { clipboardSyncState } from './SyncState';
15+
import { configService } from '../ConfigService';
1516

1617
/** 远程剪贴板变化回调:仅在内容哈希变化时触发 */
1718
export type RemoteClipboardChangedCallback = (content: ClipboardContent) => void;
@@ -22,8 +23,6 @@ class RemoteClipboardMonitor {
2223
private callbacks = new Set<RemoteClipboardChangedCallback>();
2324
private pollingTag: string | null = null;
2425
private _signalRConnected = false;
25-
private _server: ServerConfig | null = null;
26-
private _pollingInterval: number | undefined = undefined;
2726
/** 上次触发回调时的内容哈希,用于过滤重复通知 */
2827
private _lastContentHash: string | null = null;
2928
/**
@@ -89,13 +88,14 @@ class RemoteClipboardMonitor {
8988
* 建立远程监听(SignalR 或轮询)。
9089
* 不触发初始获取,由调用方负责。
9190
*/
92-
async connect(server: ServerConfig, pollingInterval?: number): Promise<void> {
93-
this._server = server;
94-
this._pollingInterval = pollingInterval;
91+
async connect(): Promise<void> {
92+
const server = await configService.getActiveServer();
93+
if (!server) return;
94+
const config = await configService.getConfig();
9595
if (server.type === 'syncclipboard') {
9696
await this._connectSignalR(server);
9797
} else {
98-
this._startPolling(pollingInterval);
98+
this._startPolling(config?.remotePollingInterval);
9999
}
100100
}
101101

@@ -104,24 +104,22 @@ class RemoteClipboardMonitor {
104104
* 若未连接则先重连,若已连接则直接刷新。
105105
* 无需区分服务器类型,内部统一处理。
106106
*/
107-
async resumeAndRefresh(server: ServerConfig, pollingInterval?: number): Promise<void> {
107+
async resumeAndRefresh(): Promise<void> {
108108
if (!this.isConnected()) {
109-
await this.connect(server, pollingInterval);
109+
await this.connect();
110110
}
111111
await this.refresh();
112112
}
113113

114114
/**
115115
* App 返回前台时由外部(RemoteClipboardMonitorTask.onForeground)调用。
116-
* 使用上次已知的服务器配置重新连接并刷新
116+
* 重新连接并刷新
117117
*/
118118
async handleForeground(): Promise<void> {
119-
if (!this._server) return;
120-
await this.resumeAndRefresh(this._server, this._pollingInterval);
119+
await this.resumeAndRefresh();
121120
}
122121

123122
async disconnect(): Promise<void> {
124-
this._server = null;
125123
this._lastContentHash = null;
126124
this._stopPolling();
127125
await this._disconnectSignalR();
@@ -205,7 +203,6 @@ class RemoteClipboardMonitor {
205203
* @throws 无服务器连接或拉取失败时抛出异常
206204
*/
207205
async fetchLatest(): Promise<ClipboardContent> {
208-
if (!this._server) throw new Error('No active server');
209206
const apiClient = await getAPIClient();
210207
const profile = await apiClient.getClipboard();
211208
if (!profile) throw new Error('No clipboard data returned');

0 commit comments

Comments
 (0)