feat(nbstore): share worker between workspaces (#9947)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -39,6 +39,10 @@ export class UserDB extends Entity<{
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
override dispose(): void {
|
||||
this.engine.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
export type UserDBWithTables = UserDB & {
|
||||
|
||||
Reference in New Issue
Block a user