mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-08 20:57:08 +08:00
feat(core): support better battery save mode (#13383)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Introduced a Document Summary module, enabling live and cached document summaries with cloud revalidation. * Added a feature flag for enabling battery save mode. * Added explicit pause and resume controls for sync operations, accessible via UI events and programmatically. * **Improvements** * Enhanced sync and indexing logic to support pausing, resuming, and battery save mode, with improved job prioritization. * Updated navigation and preview components to use the new document summary service and improved priority handling. * Improved logging and state reporting for sync and indexing processes. * Refined backlink handling with reactive loading states and cloud revalidation. * Replaced backlink and link management to use a new dedicated document links service. * Enhanced workspace engine to conditionally enable battery save mode based on feature flags and workspace flavor. * **Bug Fixes** * Removed unnecessary debug console logs from various components for cleaner output. * **Refactor** * Replaced battery save mode methods with explicit pause/resume methods throughout the app and services. * Modularized and streamlined document summary and sync-related code for better maintainability. * Restructured backlink components to improve visibility handling and data fetching. * Simplified and improved document backlink data fetching with retry and loading state management. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -87,17 +87,18 @@ export class StoreManagerClient {
|
||||
});
|
||||
}
|
||||
|
||||
enableBatterySaveMode() {
|
||||
pause() {
|
||||
this.connections.forEach(connection => {
|
||||
connection.store.enableBatterySaveMode().catch(err => {
|
||||
console.error('error enabling battery save mode', err);
|
||||
connection.store.pauseSync().catch(err => {
|
||||
console.error('error pausing', err);
|
||||
});
|
||||
});
|
||||
}
|
||||
disableBatterySaveMode() {
|
||||
|
||||
resume() {
|
||||
this.connections.forEach(connection => {
|
||||
connection.store.disableBatterySaveMode().catch(err => {
|
||||
console.error('error disabling battery save mode', err);
|
||||
connection.store.resumeSync().catch(err => {
|
||||
console.error('error resuming', err);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -135,6 +136,13 @@ export class StoreClient {
|
||||
disableBatterySaveMode(): Promise<void> {
|
||||
return this.client.call('sync.disableBatterySaveMode');
|
||||
}
|
||||
|
||||
pauseSync() {
|
||||
return this.client.call('sync.pauseSync');
|
||||
}
|
||||
resumeSync() {
|
||||
return this.client.call('sync.resumeSync');
|
||||
}
|
||||
}
|
||||
|
||||
class WorkerDocStorage implements DocStorage {
|
||||
|
||||
@@ -135,34 +135,44 @@ class StoreConsumer {
|
||||
}
|
||||
|
||||
private readonly ENABLE_BATTERY_SAVE_MODE_DELAY = 1000;
|
||||
private enableBatterySaveModeTimeout: NodeJS.Timeout | null = null;
|
||||
private enabledBatterySaveMode = false;
|
||||
private syncPauseTimeout: NodeJS.Timeout | null = null;
|
||||
private syncPaused = false;
|
||||
|
||||
enableBatterySaveMode() {
|
||||
if (this.enableBatterySaveModeTimeout || this.enabledBatterySaveMode) {
|
||||
private pauseSync() {
|
||||
if (this.syncPauseTimeout || this.syncPaused) {
|
||||
return;
|
||||
}
|
||||
this.enableBatterySaveModeTimeout = setTimeout(() => {
|
||||
if (!this.enabledBatterySaveMode) {
|
||||
this.indexerSync.enableBatterySaveMode();
|
||||
this.enabledBatterySaveMode = true;
|
||||
console.log('[BatterySaveMode] enabled');
|
||||
this.syncPauseTimeout = setTimeout(() => {
|
||||
if (!this.syncPaused) {
|
||||
this.indexerSync.pauseSync();
|
||||
this.syncPaused = true;
|
||||
console.log('[IndexerSync] paused');
|
||||
}
|
||||
}, this.ENABLE_BATTERY_SAVE_MODE_DELAY);
|
||||
}
|
||||
|
||||
disableBatterySaveMode() {
|
||||
if (this.enableBatterySaveModeTimeout) {
|
||||
clearTimeout(this.enableBatterySaveModeTimeout);
|
||||
this.enableBatterySaveModeTimeout = null;
|
||||
private resumeSync() {
|
||||
if (this.syncPauseTimeout) {
|
||||
clearTimeout(this.syncPauseTimeout);
|
||||
this.syncPauseTimeout = null;
|
||||
}
|
||||
if (this.enabledBatterySaveMode) {
|
||||
this.indexerSync.disableBatterySaveMode();
|
||||
this.enabledBatterySaveMode = false;
|
||||
console.log('[BatterySaveMode] disabled');
|
||||
if (this.syncPaused) {
|
||||
this.indexerSync.resumeSync();
|
||||
this.syncPaused = false;
|
||||
console.log('[IndexerSync] resumed');
|
||||
}
|
||||
}
|
||||
|
||||
private enableBatterySaveMode() {
|
||||
console.log('[IndexerSync] enable battery save mode');
|
||||
this.indexerSync.enableBatterySaveMode();
|
||||
}
|
||||
|
||||
private disableBatterySaveMode() {
|
||||
console.log('[IndexerSync] disable battery save mode');
|
||||
this.indexerSync.disableBatterySaveMode();
|
||||
}
|
||||
|
||||
private registerHandlers(consumer: OpConsumer<WorkerOps>) {
|
||||
const collectJobs = new Map<
|
||||
string,
|
||||
@@ -316,6 +326,8 @@ class StoreConsumer {
|
||||
this.indexerSync.aggregate$(table, query, field, options),
|
||||
'sync.enableBatterySaveMode': () => this.enableBatterySaveMode(),
|
||||
'sync.disableBatterySaveMode': () => this.disableBatterySaveMode(),
|
||||
'sync.pauseSync': () => this.pauseSync(),
|
||||
'sync.resumeSync': () => this.resumeSync(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,6 +143,8 @@ interface GroupedWorkerOps {
|
||||
sync: {
|
||||
enableBatterySaveMode: [void, void];
|
||||
disableBatterySaveMode: [void, void];
|
||||
pauseSync: [void, void];
|
||||
resumeSync: [void, void];
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user