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

@@ -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 & {