feat: improve code

This commit is contained in:
MingLiang Wang
2022-12-26 17:33:01 +08:00
parent e04547be9c
commit ad9079319b
2 changed files with 16 additions and 19 deletions
@@ -27,7 +27,8 @@ const DynamicBlocksuite = ({
useEffect(() => {
const openWorkspace: LoadWorkspaceHandler = (
workspaceId: string,
websocket = false
websocket = false,
user
) =>
// eslint-disable-next-line no-async-promise-executor
new Promise(async resolve => {
@@ -63,22 +64,16 @@ const DynamicBlocksuite = ({
const indexDBProvider = workspace.providers.find(
p => p instanceof IndexedDBDocProvider
);
await new Promise<undefined>(resolve => {
downloadWorkspace({ workspaceId })
.then(async updates => {
updates &&
StoreWorkspace.Y.applyUpdate(
workspace.doc,
new Uint8Array(updates)
);
// if after update, the space:meta is empty, then we need to get map with doc
workspace.doc.getMap('space:meta');
resolve(undefined);
})
.catch(() => {
resolve(undefined);
});
});
if (user) {
const updates = await downloadWorkspace({ workspaceId });
updates &&
StoreWorkspace.Y.applyUpdate(
workspace.doc,
new Uint8Array(updates)
);
// if after update, the space:meta is empty, then we need to get map with doc
workspace.doc.getMap('space:meta');
}
if (indexDBProvider) {
(indexDBProvider as IndexedDBDocProvider).whenSynced.then(() => {
resolve(workspace);
@@ -45,7 +45,8 @@ export const AppStateProvider = ({ children }: { children?: ReactNode }) => {
(async () => {
const workspacesList = await Promise.all(
state.workspacesMeta.map(async ({ id }) => {
const workspace = (await loadWorkspaceHandler?.(id, false)) || null;
const workspace =
(await loadWorkspaceHandler?.(id, false, state.user)) || null;
return { id, workspace };
})
);
@@ -87,7 +88,8 @@ export const AppStateProvider = ({ children }: { children?: ReactNode }) => {
if (state.currentWorkspaceId === workspaceId) {
return state.currentWorkspace;
}
const workspace = (await loadWorkspaceHandler?.(workspaceId, true)) || null;
const workspace =
(await loadWorkspaceHandler?.(workspaceId, true, state.user)) || null;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error