feat(core): new worker workspace engine (#9257)
This commit is contained in:
@@ -2,7 +2,8 @@ import type {
|
||||
Table as OrmTable,
|
||||
TableSchemaBuilder,
|
||||
} from '@toeverything/infra';
|
||||
import { Entity } from '@toeverything/infra';
|
||||
import { Entity, LiveData } from '@toeverything/infra';
|
||||
import { map } from 'rxjs';
|
||||
|
||||
import type { WorkspaceService } from '../../workspace';
|
||||
|
||||
@@ -18,13 +19,19 @@ export class WorkspaceDBTable<
|
||||
super();
|
||||
}
|
||||
|
||||
isSyncing$ = this.workspaceService.workspace.engine.doc
|
||||
.docState$(this.props.storageDocId)
|
||||
.map(docState => docState.syncing);
|
||||
isSyncing$ = LiveData.from(
|
||||
this.workspaceService.workspace.engine.doc
|
||||
.docState$(this.props.storageDocId)
|
||||
.pipe(map(docState => docState.syncing)),
|
||||
false
|
||||
);
|
||||
|
||||
isLoading$ = this.workspaceService.workspace.engine.doc
|
||||
.docState$(this.props.storageDocId)
|
||||
.map(docState => docState.loading);
|
||||
isLoading$ = LiveData.from(
|
||||
this.workspaceService.workspace.engine.doc
|
||||
.docState$(this.props.storageDocId)
|
||||
.pipe(map(docState => !docState.loaded)),
|
||||
false
|
||||
);
|
||||
|
||||
create = this.table.create.bind(this.table) as typeof this.table.create;
|
||||
update = this.table.update.bind(this.table) as typeof this.table.update;
|
||||
|
||||
@@ -46,11 +46,11 @@ export class WorkspaceDBService extends Service {
|
||||
new YjsDBAdapter(AFFiNE_WORKSPACE_DB_SCHEMA, {
|
||||
getDoc: guid => {
|
||||
const ydoc = new YDoc({
|
||||
// guid format: db${workspaceId}${guid}
|
||||
guid: `db$${this.workspaceService.workspace.id}$${guid}`,
|
||||
// guid format: db${guid}
|
||||
guid: `db$${guid}`,
|
||||
});
|
||||
this.workspaceService.workspace.engine.doc.addDoc(ydoc, false);
|
||||
this.workspaceService.workspace.engine.doc.setPriority(
|
||||
this.workspaceService.workspace.engine.doc.connectDoc(ydoc);
|
||||
this.workspaceService.workspace.engine.doc.addPriority(
|
||||
ydoc.guid,
|
||||
50
|
||||
);
|
||||
@@ -59,8 +59,7 @@ export class WorkspaceDBService extends Service {
|
||||
})
|
||||
),
|
||||
schema: AFFiNE_WORKSPACE_DB_SCHEMA,
|
||||
storageDocId: tableName =>
|
||||
`db$${this.workspaceService.workspace.id}$${tableName}`,
|
||||
storageDocId: tableName => `db$${tableName}`,
|
||||
}
|
||||
) as WorkspaceDBWithTables<AFFiNEWorkspaceDbSchema>;
|
||||
}
|
||||
@@ -79,11 +78,11 @@ export class WorkspaceDBService extends Service {
|
||||
new YjsDBAdapter(AFFiNE_WORKSPACE_USERDATA_DB_SCHEMA, {
|
||||
getDoc: guid => {
|
||||
const ydoc = new YDoc({
|
||||
// guid format: userdata${userId}${workspaceId}${guid}
|
||||
guid: `userdata$${userId}$${this.workspaceService.workspace.id}$${guid}`,
|
||||
// guid format: userdata${userId}${guid}
|
||||
guid: `userdata$${userId}$${guid}`,
|
||||
});
|
||||
this.workspaceService.workspace.engine.doc.addDoc(ydoc, false);
|
||||
this.workspaceService.workspace.engine.doc.setPriority(
|
||||
this.workspaceService.workspace.engine.doc.connectDoc(ydoc);
|
||||
this.workspaceService.workspace.engine.doc.addPriority(
|
||||
ydoc.guid,
|
||||
50
|
||||
);
|
||||
@@ -92,8 +91,7 @@ export class WorkspaceDBService extends Service {
|
||||
})
|
||||
),
|
||||
schema: AFFiNE_WORKSPACE_USERDATA_DB_SCHEMA,
|
||||
storageDocId: tableName =>
|
||||
`userdata$${userId}$${this.workspaceService.workspace.id}$${tableName}`,
|
||||
storageDocId: tableName => `userdata$${userId}$${tableName}`,
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { DocStorage } from '@toeverything/infra';
|
||||
import type { DocStorage } from '@affine/nbstore';
|
||||
|
||||
import {
|
||||
AFFiNE_WORKSPACE_DB_SCHEMA,
|
||||
@@ -6,27 +6,33 @@ import {
|
||||
} from './schema';
|
||||
|
||||
export async function transformWorkspaceDBLocalToCloud(
|
||||
localWorkspaceId: string,
|
||||
cloudWorkspaceId: string,
|
||||
_localWorkspaceId: string,
|
||||
_cloudWorkspaceId: string,
|
||||
localDocStorage: DocStorage,
|
||||
cloudDocStorage: DocStorage,
|
||||
accountId: string
|
||||
) {
|
||||
for (const tableName of Object.keys(AFFiNE_WORKSPACE_DB_SCHEMA)) {
|
||||
const localDocName = `db$${localWorkspaceId}$${tableName}`;
|
||||
const localDoc = await localDocStorage.doc.get(localDocName);
|
||||
const localDocName = `db$${tableName}`;
|
||||
const localDoc = await localDocStorage.getDoc(localDocName);
|
||||
if (localDoc) {
|
||||
const cloudDocName = `db$${cloudWorkspaceId}$${tableName}`;
|
||||
await cloudDocStorage.doc.set(cloudDocName, localDoc);
|
||||
const cloudDocName = `db$${tableName}`;
|
||||
await cloudDocStorage.pushDocUpdate({
|
||||
docId: cloudDocName,
|
||||
bin: localDoc.bin,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
for (const tableName of Object.keys(AFFiNE_WORKSPACE_USERDATA_DB_SCHEMA)) {
|
||||
const localDocName = `userdata$__local__$${localWorkspaceId}$${tableName}`;
|
||||
const localDoc = await localDocStorage.doc.get(localDocName);
|
||||
const localDocName = `userdata$__local__$${tableName}`;
|
||||
const localDoc = await localDocStorage.getDoc(localDocName);
|
||||
if (localDoc) {
|
||||
const cloudDocName = `userdata$${accountId}$${cloudWorkspaceId}$${tableName}`;
|
||||
await cloudDocStorage.doc.set(cloudDocName, localDoc);
|
||||
const cloudDocName = `userdata$${accountId}$${tableName}`;
|
||||
await cloudDocStorage.pushDocUpdate({
|
||||
docId: cloudDocName,
|
||||
bin: localDoc.bin,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user