feat(nbstore): share worker between workspaces (#9947)

This commit is contained in:
EYHN
2025-02-05 07:57:03 +00:00
parent 972d76d685
commit ee0cfe4dc7
30 changed files with 430 additions and 434 deletions

View File

@@ -33,7 +33,9 @@ export const BUILD_IN_SERVERS: (ServerMetadata & { config: ServerConfig })[] =
? [
{
id: 'affine-cloud',
baseUrl: location.origin,
baseUrl: BUILD_CONFIG.isElectron
? 'http://localhost:8080'
: location.origin,
config: {
serverName: 'Affine Cloud',
features: [

View File

@@ -1,5 +1,5 @@
import type {
WorkerClient,
StoreClient,
WorkerInitOptions,
} from '@affine/nbstore/worker/client';
import { createIdentifier } from '@toeverything/infra';
@@ -17,7 +17,7 @@ export interface NbstoreProvider {
key: string,
options: WorkerInitOptions
): {
store: WorkerClient;
store: StoreClient;
dispose: () => void;
};
}

View File

@@ -1,8 +1,9 @@
import { Entity, LiveData } from '@toeverything/infra';
import { finalize, of, switchMap } from 'rxjs';
import { Observable, of, switchMap } from 'rxjs';
import type { AuthService } from '../../cloud';
import type { UserspaceService } from '../services/userspace';
import type { UserDBWithTables } from './user-db';
export class CurrentUserDB extends Entity {
constructor(
@@ -19,11 +20,12 @@ export class CurrentUserDB extends Entity {
switchMap(userId => {
if (userId) {
const ref = this.userDBService.openDB(userId);
return of(ref.obj).pipe(
finalize(() => {
return new Observable<UserDBWithTables>(subscriber => {
subscriber.next(ref.obj);
return () => {
ref.release();
})
);
};
});
} else {
return of(null);
}

View File

@@ -1,6 +1,6 @@
import { IndexedDBDocStorage } from '@affine/nbstore/idb';
import { SqliteDocStorage } from '@affine/nbstore/sqlite';
import type { WorkerClient } from '@affine/nbstore/worker/client';
import type { StoreClient } from '@affine/nbstore/worker/client';
import { Entity } from '@toeverything/infra';
import type { ServerService } from '../../cloud';
@@ -10,7 +10,7 @@ export class UserDBEngine extends Entity<{
userId: string;
}> {
private readonly userId = this.props.userId;
readonly client: WorkerClient;
readonly client: StoreClient;
DocStorageType =
BUILD_CONFIG.isElectron || BUILD_CONFIG.isIOS

View File

@@ -39,6 +39,10 @@ export class UserDB extends Entity<{
});
});
}
override dispose(): void {
this.engine.dispose();
}
}
export type UserDBWithTables = UserDB & {

View File

@@ -1,5 +1,5 @@
import type {
WorkerClient,
StoreClient,
WorkerInitOptions,
} from '@affine/nbstore/worker/client';
import { Entity } from '@toeverything/infra';
@@ -12,7 +12,7 @@ export class WorkspaceEngine extends Entity<{
isSharedMode?: boolean;
engineWorkerInitOptions: WorkerInitOptions;
}> {
worker?: WorkerClient;
client?: StoreClient;
started = false;
constructor(
@@ -23,24 +23,24 @@ export class WorkspaceEngine extends Entity<{
}
get doc() {
if (!this.worker) {
if (!this.client) {
throw new Error('Engine is not initialized');
}
return this.worker.docFrontend;
return this.client.docFrontend;
}
get blob() {
if (!this.worker) {
if (!this.client) {
throw new Error('Engine is not initialized');
}
return this.worker.blobFrontend;
return this.client.blobFrontend;
}
get awareness() {
if (!this.worker) {
if (!this.client) {
throw new Error('Engine is not initialized');
}
return this.worker.awarenessFrontend;
return this.client.awarenessFrontend;
}
start() {
@@ -54,7 +54,7 @@ export class WorkspaceEngine extends Entity<{
`workspace:${this.workspaceService.workspace.flavour}:${this.workspaceService.workspace.id}`,
this.props.engineWorkerInitOptions
);
this.worker = store;
this.client = store;
this.disposables.push(dispose);
this.eventBus.emit(WorkspaceEngineBeforeStart, this);