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

@@ -13,9 +13,9 @@ import { createAffineCloudBlobStorage } from '@affine/workspace-impl';
import { assertEquals } from '@blocksuite/global/utils';
import { Workspace } from '@blocksuite/store';
import { revertUpdate } from '@toeverything/y-indexeddb';
import { useMemo } from 'react';
import { useEffect, useMemo } from 'react';
import useSWRImmutable from 'swr/immutable';
import { applyUpdate } from 'yjs';
import { applyUpdate, encodeStateAsUpdate } from 'yjs';
import {
useMutateQueryResource,
@@ -105,7 +105,7 @@ const snapshotFetcher = async (
const workspaceMap = new Map<string, Workspace>();
// assume the workspace is a cloud workspace since the history feature is only enabled for cloud workspace
const getOrCreateWorkspace = (workspaceId: string) => {
const getOrCreateShellWorkspace = (workspaceId: string) => {
let workspace = workspaceMap.get(workspaceId);
if (!workspace) {
const blobStorage = createAffineCloudBlobStorage(workspaceId);
@@ -120,6 +120,7 @@ const getOrCreateWorkspace = (workspaceId: string) => {
schema: globalBlockSuiteSchema,
});
workspaceMap.set(workspaceId, workspace);
workspace.doc.emit('sync', []);
}
return workspace;
};
@@ -143,17 +144,17 @@ export const usePageHistory = (
// workspace id + page id + timestamp + snapshot -> Page (to be used for rendering in blocksuite editor)
export const useSnapshotPage = (
workspaceId: string,
workspace: Workspace,
pageDocId: string,
ts?: string
) => {
const snapshot = usePageHistory(workspaceId, pageDocId, ts);
const snapshot = usePageHistory(workspace.id, pageDocId, ts);
const page = useMemo(() => {
if (!ts) {
return;
}
const pageId = pageDocId + '-' + ts;
const historyShellWorkspace = getOrCreateWorkspace(workspaceId);
const historyShellWorkspace = getOrCreateShellWorkspace(workspace.id);
let page = historyShellWorkspace.getPage(pageId);
if (!page && snapshot) {
page = historyShellWorkspace.createPage({
@@ -169,7 +170,15 @@ export const useSnapshotPage = (
.catch(console.error); // must load before applyUpdate
}
return page ?? undefined;
}, [pageDocId, snapshot, ts, workspaceId]);
}, [pageDocId, snapshot, ts, workspace]);
useEffect(() => {
const historyShellWorkspace = getOrCreateShellWorkspace(workspace.id);
// apply the rootdoc's update to the current workspace
// this makes sure the page reference links are not deleted ones in the preview
const update = encodeStateAsUpdate(workspace.doc);
applyUpdate(historyShellWorkspace.doc, update);
}, [workspace]);
return page;
};

View File

@@ -5,6 +5,7 @@ import { ConfirmModal, Modal } from '@affine/component/ui/modal';
import { openSettingModalAtom, type PageMode } from '@affine/core/atoms';
import { useIsWorkspaceOwner } from '@affine/core/hooks/affine/use-is-workspace-owner';
import { useAsyncCallback } from '@affine/core/hooks/affine-async-hooks';
import { useBlockSuiteWorkspacePageTitle } from '@affine/core/hooks/use-block-suite-workspace-page-title';
import { useUserSubscription } from '@affine/core/hooks/use-subscription';
import { waitForCurrentWorkspaceAtom } from '@affine/core/modules/workspace';
import { timestampToLocalTime } from '@affine/core/utils';
@@ -418,7 +419,7 @@ const PageHistoryManager = ({
return workspace.getPage(pageId)?.spaceDoc.guid ?? pageId;
}, [pageId, workspace]);
const snapshotPage = useSnapshotPage(workspaceId, pageDocId, activeVersion);
const snapshotPage = useSnapshotPage(workspace, pageDocId, activeVersion);
const t = useAFFiNEI18N();
@@ -440,10 +441,7 @@ const PageHistoryManager = ({
const defaultPreviewPageMode = useAtomValue(currentModeAtom);
const [mode, setMode] = useState<PageMode>(defaultPreviewPageMode);
const title = useMemo(
() => workspace.getPage(pageId)?.meta.title || t['Untitled'](),
[pageId, t, workspace]
);
const title = useBlockSuiteWorkspacePageTitle(workspace, pageId);
const [showRestoreConfirmModal, setShowRestoreConfirmModal] = useState(false);

View File

@@ -51,6 +51,8 @@ beforeEach(async () => {
blockSuiteWorkspace = workspace.blockSuiteWorkspace;
blockSuiteWorkspace.doc.emit('sync', []);
const initPage = async (page: Page) => {
await page.waitForLoaded();
expect(page).not.toBeNull();

View File

@@ -1,20 +1,25 @@
import type { Workspace } from '@blocksuite/store';
import { useAtomValue } from 'jotai';
import { useEffect, useMemo, useReducer } from 'react';
import { useEffect, useState } from 'react';
import type { WorkspacePropertiesAdapter } from '../modules/workspace/properties';
import {
currentWorkspacePropertiesAdapterAtom,
WorkspacePropertiesAdapter,
workspaceAdapterAtomFamily,
} from '../modules/workspace/properties';
function getProxy<T extends object>(obj: T) {
return new Proxy(obj, {});
}
const useReactiveAdapter = (adapter: WorkspacePropertiesAdapter) => {
const [, forceUpdate] = useReducer(c => c + 1, 0);
const [proxy, setProxy] = useState(adapter);
useEffect(() => {
// todo: track which properties are used and then filter by property path change
// using Y.YEvent.path
function observe() {
forceUpdate();
setProxy(getProxy(adapter));
}
adapter.properties.observeDeep(observe);
return () => {
@@ -22,7 +27,7 @@ const useReactiveAdapter = (adapter: WorkspacePropertiesAdapter) => {
};
}, [adapter]);
return adapter;
return proxy;
};
export function useCurrentWorkspacePropertiesAdapter() {
@@ -31,9 +36,6 @@ export function useCurrentWorkspacePropertiesAdapter() {
}
export function useWorkspacePropertiesAdapter(workspace: Workspace) {
const adapter = useMemo(
() => new WorkspacePropertiesAdapter(workspace),
[workspace]
);
const adapter = useAtomValue(workspaceAdapterAtomFamily(workspace));
return useReactiveAdapter(adapter);
}

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));
});

View File

@@ -99,6 +99,7 @@ export const loader: LoaderFunction = async ({ params }) => {
assertDownloadResponse(response);
const { arrayBuffer } = response;
applyUpdate(workspace.doc, new Uint8Array(arrayBuffer));
workspace.doc.emit('sync', []);
}
const page = workspace.getPage(pageId);
assertExists(page, 'cannot find page');