perf: use lazy load provider for IDB and SQLITE (#3351)

This commit is contained in:
Peng Xiao
2023-07-26 00:56:48 +08:00
committed by GitHub
parent e3f66d7e22
commit 20ee9d485d
25 changed files with 481 additions and 758 deletions

View File

@@ -10,7 +10,6 @@ import { createBroadcastChannelProvider } from '@blocksuite/store/providers/broa
import {
createIndexedDBProvider as create,
downloadBinary,
EarlyDisconnectError,
} from '@toeverything/y-indexeddb';
import type { Doc } from 'yjs';
@@ -40,17 +39,6 @@ const createIndexedDBBackgroundProvider: DocProviderCreator = (
connect: () => {
logger.info('connect indexeddb provider', id);
indexeddbProvider.connect();
indexeddbProvider.whenSynced
.then(() => {
connected = true;
})
.catch(error => {
connected = false;
if (error instanceof EarlyDisconnectError) {
return;
}
throw error;
});
},
disconnect: () => {
assertExists(indexeddbProvider);
@@ -61,7 +49,6 @@ const createIndexedDBBackgroundProvider: DocProviderCreator = (
};
};
const cache: WeakMap<Doc, Uint8Array> = new WeakMap();
const indexedDBDownloadOrigin = 'indexeddb-download-provider';
const createIndexedDBDownloadProvider: DocProviderCreator = (
@@ -74,18 +61,11 @@ const createIndexedDBDownloadProvider: DocProviderCreator = (
_resolve = resolve;
_reject = reject;
});
async function downloadBinaryRecursively(doc: Doc) {
if (cache.has(doc)) {
const binary = cache.get(doc) as Uint8Array;
async function downloadAndApply(doc: Doc) {
const binary = await downloadBinary(doc.guid);
if (binary) {
Y.applyUpdate(doc, binary, indexedDBDownloadOrigin);
} else {
const binary = await downloadBinary(doc.guid);
if (binary) {
Y.applyUpdate(doc, binary, indexedDBDownloadOrigin);
cache.set(doc, binary);
}
}
await Promise.all([...doc.subdocs].map(downloadBinaryRecursively));
}
return {
flavour: 'local-indexeddb',
@@ -98,7 +78,7 @@ const createIndexedDBDownloadProvider: DocProviderCreator = (
},
sync: () => {
logger.info('sync indexeddb provider', id);
downloadBinaryRecursively(doc).then(_resolve).catch(_reject);
downloadAndApply(doc).then(_resolve).catch(_reject);
},
};
};