refactor: remove sync binary (#1297)

This commit is contained in:
Himself65
2023-03-03 13:36:42 -06:00
committed by GitHub
parent fd510834ed
commit 296cd215c1
22 changed files with 161 additions and 202 deletions
+9 -18
View File
@@ -1,8 +1,8 @@
import { createAffineProviders } from '../../blocksuite';
import { Unreachable } from '../../components/affine/affine-error-eoundary';
import {
AffineRemoteUnSyncedWorkspace,
RemWorkspaceFlavour,
transformToAffineSyncedWorkspace,
} from '../../shared';
import { apis } from '../../shared/apis';
import { createEmptyBlockSuiteWorkspace } from '../../utils';
@@ -42,26 +42,17 @@ export const fetcher = async (
if (query === QueryKey.getWorkspaces) {
return apis.getWorkspaces().then(workspaces => {
return workspaces.map(workspace => {
const blockSuiteWorkspace = createEmptyBlockSuiteWorkspace(
workspace.id,
(k: string) =>
// fixme: token could be expired
({ api: '/api/workspace', token: apis.auth.token }[k])
);
const remWorkspace: AffineRemoteUnSyncedWorkspace = {
...workspace,
flavour: RemWorkspaceFlavour.AFFINE,
firstBinarySynced: false,
blockSuiteWorkspace: createEmptyBlockSuiteWorkspace(
workspace.id,
(k: string) =>
// fixme: token could be expired
({ api: '/api/workspace', token: apis.auth.token }[k])
),
syncBinary: async () => {
const binary = await apis.downloadWorkspace(
workspace.id,
workspace.public
);
if (remWorkspace.firstBinarySynced) {
return null;
}
return transformToAffineSyncedWorkspace(remWorkspace, binary);
},
blockSuiteWorkspace,
providers: [...createAffineProviders(blockSuiteWorkspace)],
};
return remWorkspace;
});
+1 -12
View File
@@ -44,9 +44,7 @@ export const AffinePlugin: WorkspacePlugin<RemWorkspaceFlavour.AFFINE> = {
await apis.deleteWorkspace({
id: workspace.id,
});
if (workspace.firstBinarySynced) {
workspace.providers.forEach(p => p.cleanup());
}
workspace.providers.forEach(p => p.cleanup());
},
prefetchData: async dataCenter => {
if (localStorage.getItem(kAffineLocal)) {
@@ -74,9 +72,6 @@ export const AffinePlugin: WorkspacePlugin<RemWorkspaceFlavour.AFFINE> = {
blockSuiteWorkspace,
providers: [...createAffineProviders(blockSuiteWorkspace)],
flavour: RemWorkspaceFlavour.AFFINE,
syncBinary: async () => {
return { ...affineWorkspace };
},
};
return affineWorkspace;
});
@@ -106,9 +101,6 @@ export const AffinePlugin: WorkspacePlugin<RemWorkspaceFlavour.AFFINE> = {
.then(async workspaces => {
const promises = workspaces.map(workspace => {
assertEquals(workspace.flavour, RemWorkspaceFlavour.AFFINE);
if (!workspace.firstBinarySynced) {
return workspace.syncBinary();
}
return workspace;
});
return Promise.all(promises)
@@ -151,9 +143,6 @@ export const AffinePlugin: WorkspacePlugin<RemWorkspaceFlavour.AFFINE> = {
});
},
PageDetail: ({ currentWorkspace, currentPageId }) => {
if (!currentWorkspace.firstBinarySynced) {
return <div>Loading</div>;
}
const page = currentWorkspace.blockSuiteWorkspace.getPage(currentPageId);
if (!page) {
throw new PageNotFoundError(
+18 -50
View File
@@ -1,5 +1,5 @@
import { DebugLogger } from '@affine/debug';
import { config } from '@affine/env';
import { config, DEFAULT_WORKSPACE_NAME } from '@affine/env';
import { assertEquals, nanoid } from '@blocksuite/store';
import React from 'react';
import { IndexeddbPersistence } from 'y-indexeddb';
@@ -10,6 +10,7 @@ import { WorkspaceSettingDetail } from '../../components/affine/workspace-settin
import { BlockSuitePageList } from '../../components/blocksuite/block-suite-page-list';
import { PageDetailEditor } from '../../components/page-detail-editor';
import {
BlockSuiteWorkspace,
LoadPriority,
LocalWorkspace,
RemWorkspaceFlavour,
@@ -20,6 +21,8 @@ import { WorkspacePlugin } from '..';
const logger = new DebugLogger('local-plugin');
export const kStoreKey = 'affine-local-workspace';
// fixme(himself65): this is a hacking that first workspace will disappear somehow
const hashMap = new Map<string, BlockSuiteWorkspace>();
export const LocalPlugin: WorkspacePlugin<RemWorkspaceFlavour.LOCAL> = {
flavour: RemWorkspaceFlavour.LOCAL,
@@ -92,34 +95,17 @@ export const LocalPlugin: WorkspacePlugin<RemWorkspaceFlavour.LOCAL> = {
if (config.enableIndexedDBProvider) {
const workspaces = await Promise.all(
ids.map(id => {
const blockSuiteWorkspace = createEmptyBlockSuiteWorkspace(
id,
(_: string) => undefined
);
const blockSuiteWorkspace = hashMap.has(id)
? (hashMap.get(id) as BlockSuiteWorkspace)
: createEmptyBlockSuiteWorkspace(id, (_: string) => undefined);
hashMap.set(id, blockSuiteWorkspace);
const workspace: LocalWorkspace = {
id,
flavour: RemWorkspaceFlavour.LOCAL,
blockSuiteWorkspace: blockSuiteWorkspace,
providers: [...createLocalProviders(blockSuiteWorkspace)],
syncBinary: async () => {
if (!config.enableIndexedDBProvider) {
return {
...workspace,
};
}
const persistence = new IndexeddbPersistence(
blockSuiteWorkspace.room as string,
blockSuiteWorkspace.doc
);
return persistence.whenSynced.then(() => {
persistence.destroy();
return {
...workspace,
};
});
},
};
return workspace.syncBinary();
return workspace;
})
);
workspaces.forEach(workspace => {
@@ -146,7 +132,8 @@ export const LocalPlugin: WorkspacePlugin<RemWorkspaceFlavour.LOCAL> = {
workspaceId,
(_: string) => undefined
);
blockSuiteWorkspace.meta.setName('Untitled Workspace');
hashMap.set(workspaceId, blockSuiteWorkspace);
blockSuiteWorkspace.meta.setName(DEFAULT_WORKSPACE_NAME);
localStorage.setItem(kStoreKey, JSON.stringify([workspaceId]));
blockSuiteWorkspace.createPage(nanoid());
const workspace: LocalWorkspace = {
@@ -154,33 +141,14 @@ export const LocalPlugin: WorkspacePlugin<RemWorkspaceFlavour.LOCAL> = {
flavour: RemWorkspaceFlavour.LOCAL,
blockSuiteWorkspace: blockSuiteWorkspace,
providers: [...createLocalProviders(blockSuiteWorkspace)],
syncBinary: async () => {
if (!config.enableIndexedDBProvider) {
return {
...workspace,
};
}
const persistence = new IndexeddbPersistence(
blockSuiteWorkspace.room as string,
blockSuiteWorkspace.doc
);
return persistence.whenSynced.then(() => {
persistence.destroy();
return {
...workspace,
};
});
},
};
await workspace.syncBinary();
if (signal?.aborted) {
const persistence = new IndexeddbPersistence(
blockSuiteWorkspace.room as string,
blockSuiteWorkspace.doc
);
await persistence.clearData();
return;
}
const persistence = new IndexeddbPersistence(
blockSuiteWorkspace.room as string,
blockSuiteWorkspace.doc
);
await persistence.whenSynced.then(() => {
persistence.destroy();
});
dataCenter.workspaces = [workspace];
}
},