refactor: workspace provider (#2218)
This commit is contained in:
1 parent
ec39c23fb7
commit
9096ac2960
18 files changed
+377
-255
No files matched your search
@@ -1,18 +1,24 @@
|
||||
# @toeverything/y-indexeddb
|
||||
|
||||
> This package haven't been published yet.
|
||||
|
||||
## Usage
|
||||
|
||||
```ts
|
||||
import { createIndexedDBProvider } from '@toeverything/y-indexeddb';
|
||||
import { createIndexedDBProvider, downloadBinary } from '@toeverything/y-indexeddb';
|
||||
import * as Y from 'yjs';
|
||||
const yDoc = new Y.Doc();
|
||||
|
||||
// sync yDoc with indexedDB
|
||||
const provider = createIndexedDBProvider('docName', yDoc);
|
||||
provider.connect();
|
||||
await provider.whenSynced.then(() => {
|
||||
console.log('synced');
|
||||
provider.disconnect();
|
||||
});
|
||||
|
||||
// dowload binary data from indexedDB for once
|
||||
downloadBinary('docName').then(blob => {
|
||||
if (blob !== false) {
|
||||
Y.applyUpdate(yDoc, blob);
|
||||
}
|
||||
});
|
||||
```
|
||||
@@ -340,7 +340,10 @@ describe('utils', () => {
|
||||
provider.connect();
|
||||
await provider.whenSynced;
|
||||
provider.disconnect();
|
||||
const update = await downloadBinary(workspace.id, rootDBName);
|
||||
const update = (await downloadBinary(
|
||||
workspace.id,
|
||||
rootDBName
|
||||
)) as Uint8Array;
|
||||
expect(update).toBeInstanceOf(Uint8Array);
|
||||
const newWorkspace = new Workspace({
|
||||
id,
|
||||
|
||||
@@ -129,7 +129,7 @@ export async function tryMigrate(
|
||||
export async function downloadBinary(
|
||||
id: string,
|
||||
dbName = DEFAULT_DB_NAME
|
||||
): Promise<UpdateMessage['update']> {
|
||||
): Promise<UpdateMessage['update'] | false> {
|
||||
const dbPromise = openDB<BlockSuiteBinaryDB>(dbName, dbVersion, {
|
||||
upgrade: upgradeDB,
|
||||
});
|
||||
@@ -137,7 +137,7 @@ export async function downloadBinary(
|
||||
const t = db.transaction('workspace', 'readonly').objectStore('workspace');
|
||||
const doc = await t.get(id);
|
||||
if (!doc) {
|
||||
return new Uint8Array(0);
|
||||
return false;
|
||||
} else {
|
||||
return mergeUpdates(doc.updates.map(({ update }) => update));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user