Skip to content

Commit

Permalink
feat(core): add resetSync button (#10404)
Browse files Browse the repository at this point in the history
  • Loading branch information
EYHN committed Feb 24, 2025
1 parent 59a791f commit 0e581c9
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/common/nbstore/src/frontend/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,8 @@ export class DocFrontend {
sub?.unsubscribe();
});
}

async resetSync() {
await this.sync.resetSync();
}
}
10 changes: 10 additions & 0 deletions packages/common/nbstore/src/sync/doc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface DocSync {
readonly state$: Observable<DocSyncState>;
docState$(docId: string): Observable<DocSyncDocState>;
addPriority(id: string, priority: number): () => void;
resetSync(): Promise<void>;
}

export class DocSyncImpl implements DocSync {
Expand Down Expand Up @@ -127,4 +128,13 @@ export class DocSyncImpl implements DocSync {
const undo = this.peers.map(peer => peer.addPriority(id, priority));
return () => undo.forEach(fn => fn());
}

async resetSync() {
const running = this.abort !== null;
this.stop();
await this.sync.clearClocks();
if (running) {
this.start();
}
}
}
4 changes: 4 additions & 0 deletions packages/common/nbstore/src/worker/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ class WorkerDocSync implements DocSync {
subscription.unsubscribe();
};
}

resetSync(): Promise<void> {
return this.client.call('docSync.resetSync');
}
}

class WorkerBlobSync implements BlobSync {
Expand Down
1 change: 1 addition & 0 deletions packages/common/nbstore/src/worker/consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ class StoreConsumer {
const undo = this.docSync.addPriority(docId, priority);
return () => undo();
}),
'docSync.resetSync': () => this.docSync.resetSync(),
'blobSync.downloadBlob': key => this.blobSync.downloadBlob(key),
'blobSync.uploadBlob': blob => this.blobSync.uploadBlob(blob),
'blobSync.fullSync': () =>
Expand Down
1 change: 1 addition & 0 deletions packages/common/nbstore/src/worker/ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ interface GroupedWorkerOps {
state: [void, DocSyncState];
docState: [string, DocSyncDocState];
addPriority: [{ docId: string; priority: number }, boolean];
resetSync: [void, void];
};

blobSync: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import { WorkspaceServerService } from '@affine/core/modules/cloud';
import { WorkspaceService } from '@affine/core/modules/workspace';
import { UNTITLED_WORKSPACE_NAME } from '@affine/env/constant';
import { useI18n } from '@affine/i18n';
import { ArrowRightSmallIcon } from '@blocksuite/icons/rc';
import { FrameworkScope, useService } from '@toeverything/infra';
import { useCallback } from 'react';

import { DeleteLeaveWorkspace } from './delete-leave-workspace';
import { EnableCloudPanel } from './enable-cloud';
Expand All @@ -28,6 +30,17 @@ export const WorkspaceSettingDetail = ({

const workspaceInfo = useWorkspaceInfo(workspace);

const handleResetSyncStatus = useCallback(() => {
workspace?.engine.doc
.resetSync()
.then(() => {
onCloseSetting();
})
.catch(err => {
console.error(err);
});
}, [onCloseSetting, workspace]);

return (
<FrameworkScope scope={server?.scope}>
<SettingHeader
Expand All @@ -53,6 +66,19 @@ export const WorkspaceSettingDetail = ({
<SharingPanel />
<SettingWrapper>
<DeleteLeaveWorkspace onCloseSetting={onCloseSetting} />
<SettingRow
name={
<span style={{ color: 'var(--affine-text-secondary-color)' }}>
{t['com.affine.resetSyncStatus.button']()}
</span>
}
desc={t['com.affine.resetSyncStatus.description']()}
style={{ cursor: 'pointer' }}
onClick={handleResetSyncStatus}
data-testid="reset-sync-status"
>
<ArrowRightSmallIcon />
</SettingRow>
</SettingWrapper>
</FrameworkScope>
);
Expand Down

0 comments on commit 0e581c9

Please sign in to comment.