Skip to content

Commit b528f33

Browse files
CopilotJeric-X
andauthored
refactor: remove setLifecycleCallbacks, wire backgroundRuntimeState in settingsStore
Agent-Logs-Url: https://github.com/Jeric-X/syncclipboard-mobile/sessions/65088b1a-782d-426d-b529-0631d89ec2b5 Co-authored-by: Jeric-X <10058586+Jeric-X@users.noreply.github.com>
1 parent 112f2fa commit b528f33

4 files changed

Lines changed: 24 additions & 44 deletions

File tree

App.tsx

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ import { initLogger } from './src/utils/Logger';
1212
import { useTheme } from './src/hooks/useTheme';
1313
import { setDynamicShortcuts } from 'shortcut';
1414
import { moveTaskToBack, setExcludeFromRecents } from 'native-util';
15-
import {
16-
getBackgroundServiceManager,
17-
type BackgroundServiceLifecycleCallbacks,
18-
} from './src/services/sync/BackgroundServiceManager';
15+
import { getBackgroundServiceManager } from './src/services/sync/BackgroundServiceManager';
1916

2017
const QUICK_UPLOAD_URL = 'syncclipboard://quick-upload';
2118
const QUICK_DOWNLOAD_URL = 'syncclipboard://quick-download';
@@ -81,15 +78,6 @@ export default function App() {
8178
// 启动所有服务(冷启动时保证剪贴板监控、远程同步、后台任务正常运行,后续由 BackgroundServiceManager 维护)
8279
useEffect(() => {
8380
if (!isLoaded) return;
84-
85-
// 注入 App 层生命周期回调(ForegroundService 的 stop/tempStop 事件 → 更新 settingsStore)
86-
const lifecycleCallbacks: BackgroundServiceLifecycleCallbacks = {
87-
onForegroundServiceStop: () => useSettingsStore.getState().setEnableBackgroundTasks(false),
88-
onForegroundServiceTempStop: () =>
89-
useSettingsStore.getState().setTempDisabledBackgroundTasks(true),
90-
};
91-
getBackgroundServiceManager().setLifecycleCallbacks(lifecycleCallbacks);
92-
9381
getBackgroundServiceManager()
9482
.start()
9583
.catch(() => {});

src/services/BackgroundRuntimeState.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* 将 isTempDisabledBackgroundTasks 从 settingsStore 中剥离到此独立模块,
66
* 使 BackgroundServiceManager 不再反向依赖 UI 状态层(Zustand store)。
77
*
8-
* settingsStore 仍持有该布尔值以驱动 UI 显示,但写入路径统一走此模块
9-
* 再由 settingsStore 镜像同步
8+
* settingsStore 在模块加载时订阅此单例,将变化镜像到 isTempDisabledBackgroundTasks
9+
* 以驱动 UI 响应;BackgroundServiceManager 直接写入并订阅此模块,无需感知 store
1010
*/
1111

1212
type Listener = () => void;
@@ -22,7 +22,8 @@ class BackgroundRuntimeStateClass {
2222

2323
/**
2424
* 设置临时禁用状态,并通知所有订阅者。
25-
* 由 ForegroundService 的 tempStop 事件触发(经由注入回调中转)。
25+
* 由 BackgroundServiceManager 中 ForegroundService.addTempStopListener 直接调用;
26+
* settingsStore 订阅此通知并将变化镜像到 isTempDisabledBackgroundTasks UI 状态。
2627
*/
2728
setTempDisabled(value: boolean): void {
2829
if (this._isTempDisabled === value) return;

src/services/sync/BackgroundServiceManager.ts

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,6 @@ import { setTimer, clearTimer } from 'native-timer';
2121
import { configService } from '../ConfigService';
2222
import { backgroundRuntimeState } from '../BackgroundRuntimeState';
2323

24-
/**
25-
* 由 App 层注入的生命周期回调接口。
26-
* 将 native 事件(ForegroundService stop/tempStop)中转到 UI 状态层(settingsStore),
27-
* 使 BackgroundServiceManager 本身不依赖任何 Zustand store。
28-
*/
29-
export interface BackgroundServiceLifecycleCallbacks {
30-
/** 用户从通知栏点击「停止」时触发,应将 enableBackgroundTasks 设为 false */
31-
onForegroundServiceStop(): void;
32-
/** 用户从通知栏点击「临时停止」时触发,应设置 isTempDisabledBackgroundTasks */
33-
onForegroundServiceTempStop(): void;
34-
}
35-
3624
class BackgroundServiceManager {
3725
private static instance: BackgroundServiceManager | null = null;
3826

@@ -45,9 +33,6 @@ class BackgroundServiceManager {
4533
/** 取消对 backgroundRuntimeState 的订阅 */
4634
private runtimeUnsub: (() => void) | null = null;
4735

48-
/** 由 App 层注入的生命周期回调 */
49-
private lifecycleCallbacks: BackgroundServiceLifecycleCallbacks | null = null;
50-
5136
private constructor() {}
5237

5338
static getInstance(): BackgroundServiceManager {
@@ -57,15 +42,6 @@ class BackgroundServiceManager {
5742
return BackgroundServiceManager.instance;
5843
}
5944

60-
/**
61-
* 注入 App 层生命周期回调(应在 start() 之前调用)。
62-
* 若未注入则 ForegroundService 的 stop/tempStop 事件不会更新 store,
63-
* 但 BackgroundServiceManager 自身逻辑仍正常工作。
64-
*/
65-
setLifecycleCallbacks(callbacks: BackgroundServiceLifecycleCallbacks): void {
66-
this.lifecycleCallbacks = callbacks;
67-
}
68-
6945
// ─── 工具 ───────────────────────────────────────────────
7046

7147
private async getShouldRunBackground(): Promise<boolean> {
@@ -219,11 +195,12 @@ class BackgroundServiceManager {
219195
ForegroundService.startService();
220196

221197
this.stopSub = ForegroundService.addStopListener(() => {
222-
this.lifecycleCallbacks?.onForegroundServiceStop();
198+
configService.updateConfig({ enableBackgroundTasks: false }).catch((e) => {
199+
console.error('[BackgroundServiceManager] Failed to disable background tasks:', e);
200+
});
223201
});
224202
this.tempStopSub = ForegroundService.addTempStopListener(() => {
225203
backgroundRuntimeState.setTempDisabled(true);
226-
this.lifecycleCallbacks?.onForegroundServiceTempStop();
227204
});
228205
} catch (e) {
229206
console.error('[BackgroundServiceManager] Failed to start foreground service:', e);
@@ -254,11 +231,12 @@ class BackgroundServiceManager {
254231
if (config?.enableForegroundNotification && !isRunning) {
255232
ForegroundService.startService();
256233
this.stopSub = ForegroundService.addStopListener(() => {
257-
this.lifecycleCallbacks?.onForegroundServiceStop();
234+
configService.updateConfig({ enableBackgroundTasks: false }).catch((e) => {
235+
console.error('[BackgroundServiceManager] Failed to disable background tasks:', e);
236+
});
258237
});
259238
this.tempStopSub = ForegroundService.addTempStopListener(() => {
260239
backgroundRuntimeState.setTempDisabled(true);
261-
this.lifecycleCallbacks?.onForegroundServiceTempStop();
262240
});
263241
} else if (!config?.enableForegroundNotification && isRunning) {
264242
this._cleanupListeners();

src/stores/settingsStore.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,16 @@ export const useSettingsStore = create<SettingsState>((set, get) => ({
395395
set({ error: null });
396396
},
397397
}));
398+
399+
/**
400+
* 订阅 backgroundRuntimeState 变化,将临时禁用状态同步到 settingsStore。
401+
*
402+
* backgroundRuntimeState 是真正的写入源头(由 BackgroundServiceManager 的
403+
* ForegroundService.addTempStopListener 驱动),settingsStore 只作镜像以驱动 UI。
404+
* 模块加载时立即注册,生命周期与应用相同,无需取消订阅。
405+
*/
406+
backgroundRuntimeState.subscribe(() => {
407+
useSettingsStore.setState({
408+
isTempDisabledBackgroundTasks: backgroundRuntimeState.isTempDisabled(),
409+
});
410+
});

0 commit comments

Comments
 (0)