Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(y-indexeddb): cleanup #2207

Merged
merged 5 commits into from May 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/y-indexeddb/src/__tests__/index.spec.ts
Expand Up @@ -13,6 +13,7 @@ import { applyUpdate, Doc, encodeStateAsUpdate } from 'yjs';

import type { WorkspacePersist } from '../index';
import {
CleanupWhenConnectingError,
createIndexedDBProvider,
dbVersion,
DEFAULT_DB_NAME,
Expand Down Expand Up @@ -186,6 +187,17 @@ describe('indexeddb provider', () => {
}
});

test('cleanup when connecting', async () => {
const provider = createIndexedDBProvider(workspace.id, workspace.doc);
provider.connect();
expect(() => provider.cleanup()).rejects.toThrowError(
CleanupWhenConnectingError
);
await provider.whenSynced;
provider.disconnect();
await provider.cleanup();
});

test('merge', async () => {
setMergeCount(5);
const provider = createIndexedDBProvider(
Expand Down
9 changes: 9 additions & 0 deletions packages/y-indexeddb/src/index.ts
Expand Up @@ -90,6 +90,12 @@ export class EarlyDisconnectError extends Error {
}
}

export class CleanupWhenConnectingError extends Error {
constructor() {
super('Cleanup when connecting');
}
}

export const markMilestone = async (
id: string,
doc: Doc,
Expand Down Expand Up @@ -275,6 +281,9 @@ export const createIndexedDBProvider = (
doc.off('destroy', handleDestroy);
},
async cleanup() {
if (connect) {
himself65 marked this conversation as resolved.
Show resolved Hide resolved
throw new CleanupWhenConnectingError();
}
(await dbPromise).delete('workspace', id);
himself65 marked this conversation as resolved.
Show resolved Hide resolved
},
whenSynced: Promise.resolve(),
Expand Down