fix(core): properties adapter reactivitiy issue (#5661)

This commit is contained in:
Peng Xiao
2024-01-23 01:41:44 +00:00
parent 03b60a63cd
commit ecdb5b3407
6 changed files with 51 additions and 34 deletions

View File

@@ -1,16 +1,21 @@
import type { Workspace } from '@affine/workspace/workspace';
import { atomWithObservable } from 'jotai/utils';
import { filter, map, of } from 'rxjs';
import type { Workspace as BlockSuiteWorkspace } from '@blocksuite/store';
import { atom } from 'jotai';
import { atomFamily } from 'jotai/utils';
import { currentWorkspaceAtom } from '../atoms';
import { waitForCurrentWorkspaceAtom } from '../atoms';
import { WorkspacePropertiesAdapter } from './adapter';
export const currentWorkspacePropertiesAdapterAtom =
atomWithObservable<WorkspacePropertiesAdapter>(get => {
return of(get(currentWorkspaceAtom)).pipe(
filter((workspace): workspace is Workspace => !!workspace),
map(workspace => {
return new WorkspacePropertiesAdapter(workspace.blockSuiteWorkspace);
})
);
});
// todo: remove the inner atom when workspace is closed by using workspaceAdapterAtomFamily.remove
export const workspaceAdapterAtomFamily = atomFamily(
(workspace: BlockSuiteWorkspace) => {
return atom(async () => {
await workspace.doc.whenLoaded;
return new WorkspacePropertiesAdapter(workspace);
});
}
);
export const currentWorkspacePropertiesAdapterAtom = atom(async get => {
const workspace = await get(waitForCurrentWorkspaceAtom);
return get(workspaceAdapterAtomFamily(workspace.blockSuiteWorkspace));
});