mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-26 10:45:57 +08:00
perf: use lazy load provider for IDB and SQLITE (#3351)
This commit is contained in:
@@ -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);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user