Skip to content

Commit

Permalink
fix: indexeddb provider (#1827)
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 authored Apr 6, 2023
1 parent ee8f6a7 commit 164d357
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 44 deletions.
14 changes: 1 addition & 13 deletions apps/electron/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ __metadata:
react: ^18.2.0
react-dom: ^18.2.0
ws: ^8.13.0
y-indexeddb: ^9.0.10
y-protocols: ^1.0.5
yjs: ^13.5.52
zod: ^3.21.4
Expand Down Expand Up @@ -12007,7 +12006,7 @@ __metadata:
languageName: node
linkType: hard

"lib0@npm:^0.2.35, lib0@npm:^0.2.42, lib0@npm:^0.2.68, lib0@npm:^0.2.72, lib0@npm:^0.2.73":
"lib0@npm:^0.2.42, lib0@npm:^0.2.68, lib0@npm:^0.2.72, lib0@npm:^0.2.73":
version: 0.2.73
resolution: "lib0@npm:0.2.73"
dependencies:
Expand Down Expand Up @@ -17045,17 +17044,6 @@ __metadata:
languageName: node
linkType: hard

"y-indexeddb@npm:^9.0.10":
version: 9.0.10
resolution: "y-indexeddb@npm:9.0.10"
dependencies:
lib0: ^0.2.35
peerDependencies:
yjs: ^13.0.0
checksum: 6a57825b599cdf77da7c9857b1acc0f782492fc41531618bd7392bdfbcf11c783ff1a30b82ae080b050a5ebafd54754a978de7a6ac42144ec59eb1fbdebd090b
languageName: node
linkType: hard

"y-protocols@npm:^1.0.5":
version: 1.0.5
resolution: "y-protocols@npm:1.0.5"
Expand Down
1 change: 0 additions & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"react-dom": "^18.2.0",
"react-is": "^18.2.0",
"swr": "^2.1.2",
"y-indexeddb": "^9.0.10",
"y-protocols": "^1.0.5",
"yjs": "^13.5.52",
"zod": "^3.21.4"
Expand Down
1 change: 0 additions & 1 deletion packages/workspace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"lib0": "^0.2.73",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"y-indexeddb": "^9.0.10",
"y-protocols": "^1.0.5",
"yjs": "^13.5.52",
"zod": "^3.21.4"
Expand Down
37 changes: 22 additions & 15 deletions packages/workspace/src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import type {
} from '@affine/workspace/type';
import type { Workspace as BlockSuiteWorkspace } from '@blocksuite/store';
import { assertExists } from '@blocksuite/store';
import { IndexeddbPersistence } from 'y-indexeddb';
import {
createIndexedDBProvider as create,
EarlyDisconnectError,
} from '@toeverything/y-indexeddb';

import { createBroadCastChannelProvider } from './broad-cast-channel';
import { localProviderLogger } from './logger';
Expand Down Expand Up @@ -57,40 +60,44 @@ const createAffineWebSocketProvider = (
const createIndexedDBProvider = (
blockSuiteWorkspace: BlockSuiteWorkspace
): LocalIndexedDBProvider => {
let indexeddbProvider: IndexeddbPersistence | null = null;
const indexeddbProvider = create(
blockSuiteWorkspace.id,
blockSuiteWorkspace.doc
);
const callbacks = new Set<() => void>();
return {
flavour: 'local-indexeddb',
callbacks,
// fixme: remove background long polling
background: true,
cleanup: () => {
assertExists(indexeddbProvider);
indexeddbProvider.clearData();
callbacks.clear();
indexeddbProvider = null;
// todo: cleanup data
},
connect: () => {
localProviderLogger.info(
'connect indexeddb provider',
blockSuiteWorkspace.id
);
indexeddbProvider = new IndexeddbPersistence(
blockSuiteWorkspace.id,
blockSuiteWorkspace.doc
);
indexeddbProvider.whenSynced.then(() => {
callbacks.forEach(cb => cb());
});
indexeddbProvider.connect();
indexeddbProvider.whenSynced
.then(() => {
callbacks.forEach(cb => cb());
})
.catch(error => {
if (error instanceof EarlyDisconnectError) {
return;
} else {
throw error;
}
});
},
disconnect: () => {
assertExists(indexeddbProvider);
localProviderLogger.info(
'disconnect indexeddb provider',
blockSuiteWorkspace.id
);
indexeddbProvider.destroy();
indexeddbProvider = null;
indexeddbProvider.disconnect();
},
};
};
Expand Down
15 changes: 1 addition & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ __metadata:
swr: ^2.1.2
typescript: ^5.0.3
webpack: ^5.77.0
y-indexeddb: ^9.0.10
y-protocols: ^1.0.5
yjs: ^13.5.52
zod: ^3.21.4
Expand All @@ -234,7 +233,6 @@ __metadata:
react: ^18.2.0
react-dom: ^18.2.0
ws: ^8.13.0
y-indexeddb: ^9.0.10
y-protocols: ^1.0.5
yjs: ^13.5.52
zod: ^3.21.4
Expand Down Expand Up @@ -13611,7 +13609,7 @@ __metadata:
languageName: node
linkType: hard

"lib0@npm:^0.2.35, lib0@npm:^0.2.42, lib0@npm:^0.2.68, lib0@npm:^0.2.72, lib0@npm:^0.2.73":
"lib0@npm:^0.2.42, lib0@npm:^0.2.68, lib0@npm:^0.2.72, lib0@npm:^0.2.73":
version: 0.2.73
resolution: "lib0@npm:0.2.73"
dependencies:
Expand Down Expand Up @@ -19280,17 +19278,6 @@ __metadata:
languageName: node
linkType: hard

"y-indexeddb@npm:^9.0.10":
version: 9.0.10
resolution: "y-indexeddb@npm:9.0.10"
dependencies:
lib0: ^0.2.35
peerDependencies:
yjs: ^13.0.0
checksum: 6a57825b599cdf77da7c9857b1acc0f782492fc41531618bd7392bdfbcf11c783ff1a30b82ae080b050a5ebafd54754a978de7a6ac42144ec59eb1fbdebd090b
languageName: node
linkType: hard

"y-protocols@npm:^1.0.5":
version: 1.0.5
resolution: "y-protocols@npm:1.0.5"
Expand Down

2 comments on commit 164d357

@vercel
Copy link

@vercel vercel bot commented on 164d357 Apr 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 164d357 Apr 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

affine-storybook – ./packages/component

affine-storybook-git-master-toeverything.vercel.app
affine-storybook.vercel.app
affine-storybook-toeverything.vercel.app

Please sign in to comment.