mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-17 01:56:27 +08:00
feat(core): await sync doc (#4247)
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
import type { Doc as YDoc } from 'yjs';
|
||||
import { applyUpdate, encodeStateAsUpdate } from 'yjs';
|
||||
|
||||
import type { DocState } from './types';
|
||||
|
||||
export interface DatasourceDocAdapter {
|
||||
@@ -26,6 +29,40 @@ export interface DatasourceDocAdapter {
|
||||
): () => void;
|
||||
}
|
||||
|
||||
export async function syncDocFromDataSource(
|
||||
rootDoc: YDoc,
|
||||
datasource: DatasourceDocAdapter
|
||||
) {
|
||||
const downloadDocStateRecursively = async (doc: YDoc) => {
|
||||
const docState = await datasource.queryDocState(doc.guid);
|
||||
if (docState) {
|
||||
applyUpdate(doc, docState.missing, 'sync-doc-from-datasource');
|
||||
}
|
||||
await Promise.all(
|
||||
[...doc.subdocs].map(async subdoc => {
|
||||
await downloadDocStateRecursively(subdoc);
|
||||
})
|
||||
);
|
||||
};
|
||||
await downloadDocStateRecursively(rootDoc);
|
||||
}
|
||||
|
||||
export async function syncDataSourceFromDoc(
|
||||
rootDoc: YDoc,
|
||||
datasource: DatasourceDocAdapter
|
||||
) {
|
||||
const uploadDocStateRecursively = async (doc: YDoc) => {
|
||||
await datasource.sendDocUpdate(doc.guid, encodeStateAsUpdate(doc));
|
||||
await Promise.all(
|
||||
[...doc.subdocs].map(async subdoc => {
|
||||
await uploadDocStateRecursively(subdoc);
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
await uploadDocStateRecursively(rootDoc);
|
||||
}
|
||||
|
||||
/**
|
||||
* query the datasource from source, and save the latest update to target
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user