chore: prohibit using mergeUpdates (#3701)

This commit is contained in:
Alex Yang
2023-08-11 11:55:17 -04:00
committed by GitHub
parent e9f4912665
commit 9639143df4
14 changed files with 142 additions and 94 deletions

View File

@@ -2,7 +2,7 @@ import path from 'node:path';
import { SqliteConnection } from '@affine/native';
import { afterEach, describe, expect, it, vi } from 'vitest';
import * as Y from 'yjs';
import { applyUpdate, Doc as YDoc } from 'yjs';
import { removeWithRetry } from '../../../../tests/utils';
import { copyToTemp, migrateToSubdocAndReplaceDatabase } from '../migration';
@@ -41,14 +41,14 @@ describe('migrateToSubdocAndReplaceDatabase', () => {
expect(subdocUpdate).toBeDefined();
// apply updates
const rootDoc = new Y.Doc();
Y.applyUpdate(rootDoc, rootUpdate);
const rootDoc = new YDoc();
applyUpdate(rootDoc, rootUpdate);
// check if root doc has one subdoc
expect(rootDoc.subdocs.size).toBe(1);
// populates subdoc
Y.applyUpdate(rootDoc.subdocs.values().next().value, subdocUpdate);
applyUpdate(rootDoc.subdocs.values().next().value, subdocUpdate);
// check if root doc's meta is correct
const meta = rootDoc.getMap('meta').toJSON();
@@ -59,9 +59,7 @@ describe('migrateToSubdocAndReplaceDatabase', () => {
expect(pageMeta.title).toBe('Welcome to AFFiNEd');
// get the subdoc through id
const subDoc = rootDoc
.getMap('spaces')
.get(`space:${pageMeta.id}`) as Y.Doc;
const subDoc = rootDoc.getMap('spaces').get(`space:${pageMeta.id}`) as YDoc;
expect(subDoc).toEqual(rootDoc.subdocs.values().next().value);
await db.close();

View File

@@ -3,7 +3,7 @@ import path from 'node:path';
import fs from 'fs-extra';
import { v4 } from 'uuid';
import { afterEach, expect, test, vi } from 'vitest';
import * as Y from 'yjs';
import { Doc as YDoc, encodeStateAsUpdate } from 'yjs';
import { removeWithRetry } from '../../../../tests/utils';
import { dbSubjects } from '../subjects';
@@ -21,18 +21,18 @@ afterEach(async () => {
await removeWithRetry(tmpDir);
});
let testYDoc: Y.Doc;
let testYSubDoc: Y.Doc;
let testYDoc: YDoc;
let testYSubDoc: YDoc;
function getTestUpdates() {
testYDoc = new Y.Doc();
testYDoc = new YDoc();
const yText = testYDoc.getText('test');
yText.insert(0, 'hello');
testYSubDoc = new Y.Doc();
testYSubDoc = new YDoc();
testYDoc.getMap('subdocs').set('test-subdoc', testYSubDoc);
const updates = Y.encodeStateAsUpdate(testYDoc);
const updates = encodeStateAsUpdate(testYDoc);
return updates;
}
@@ -41,7 +41,7 @@ function getTestSubDocUpdates() {
const yText = testYSubDoc.getText('test');
yText.insert(0, 'hello');
const updates = Y.encodeStateAsUpdate(testYSubDoc);
const updates = encodeStateAsUpdate(testYSubDoc);
return updates;
}

View File

@@ -1,11 +1,11 @@
import * as Y from 'yjs';
import { applyUpdate, Doc as YDoc, encodeStateAsUpdate, transact } from 'yjs';
export function mergeUpdate(updates: Uint8Array[]) {
const yDoc = new Y.Doc();
Y.transact(yDoc, () => {
const yDoc = new YDoc();
transact(yDoc, () => {
for (const update of updates) {
Y.applyUpdate(yDoc, update);
applyUpdate(yDoc, update);
}
});
return Y.encodeStateAsUpdate(yDoc);
return encodeStateAsUpdate(yDoc);
}

View File

@@ -4,7 +4,7 @@ import { migrateToSubdoc } from '@affine/env/blocksuite';
import { SqliteConnection } from '@affine/native';
import fs from 'fs-extra';
import { nanoid } from 'nanoid';
import * as Y from 'yjs';
import { applyUpdate, Doc as YDoc, encodeStateAsUpdate } from 'yjs';
import { mainRPC } from '../main-rpc';
@@ -13,11 +13,11 @@ export const migrateToSubdocAndReplaceDatabase = async (path: string) => {
await db.connect();
const rows = await db.getAllUpdates();
const originalDoc = new Y.Doc();
const originalDoc = new YDoc();
// 1. apply all updates to the root doc
rows.forEach(row => {
Y.applyUpdate(originalDoc, row.data);
applyUpdate(originalDoc, row.data);
});
// 2. migrate using migrateToSubdoc
@@ -40,10 +40,10 @@ export const copyToTemp = async (path: string) => {
async function replaceRows(
db: SqliteConnection,
doc: Y.Doc,
doc: YDoc,
isRoot: boolean
): Promise<void> {
const migratedUpdates = Y.encodeStateAsUpdate(doc);
const migratedUpdates = encodeStateAsUpdate(doc);
const docId = isRoot ? undefined : doc.guid;
const rows = [{ data: migratedUpdates, docId: docId }];
await db.replaceUpdates(docId, rows);

View File

@@ -2,7 +2,7 @@ import assert from 'node:assert';
import type { InsertRow } from '@affine/native';
import { debounce } from 'lodash-es';
import * as Y from 'yjs';
import { applyUpdate, Doc as YDoc } from 'yjs';
import { logger } from '../logger';
import type { YOrigin } from '../type';
@@ -16,7 +16,7 @@ const FLUSH_MAX_WAIT_TIME = 10000;
// todo: trim db when it is too big
export class SecondaryWorkspaceSQLiteDB extends BaseSQLiteAdapter {
role = 'secondary';
yDoc = new Y.Doc();
yDoc = new YDoc();
firstConnected = false;
destroyed = false;
@@ -165,7 +165,7 @@ export class SecondaryWorkspaceSQLiteDB extends BaseSQLiteAdapter {
}
};
const onSubdocs = ({ added }: { added: Set<Y.Doc> }) => {
const onSubdocs = ({ added }: { added: Set<YDoc> }) => {
added.forEach(subdoc => {
this.setupListener(subdoc.guid);
});
@@ -214,7 +214,7 @@ export class SecondaryWorkspaceSQLiteDB extends BaseSQLiteAdapter {
) => {
const doc = this.getDoc(docId);
if (doc) {
Y.applyUpdate(this.yDoc, data, origin);
applyUpdate(this.yDoc, data, origin);
} else {
logger.warn(
'[SecondaryWorkspaceSQLiteDB] applyUpdate: doc not found',

View File

@@ -1,7 +1,7 @@
import type { InsertRow } from '@affine/native';
import { debounce } from 'lodash-es';
import { Subject } from 'rxjs';
import * as Y from 'yjs';
import { applyUpdate, Doc as YDoc, encodeStateAsUpdate } from 'yjs';
import { logger } from '../logger';
import type { YOrigin } from '../type';
@@ -13,7 +13,7 @@ const TRIM_SIZE = 500;
export class WorkspaceSQLiteDB extends BaseSQLiteAdapter {
role = 'primary';
yDoc = new Y.Doc();
yDoc = new YDoc();
firstConnected = false;
update$ = new Subject<void>();
@@ -78,7 +78,7 @@ export class WorkspaceSQLiteDB extends BaseSQLiteAdapter {
doc.subdocs.forEach(subdoc => {
this.setupListener(subdoc.guid);
});
const onSubdocs = ({ added }: { added: Set<Y.Doc> }) => {
const onSubdocs = ({ added }: { added: Set<YDoc> }) => {
logger.info('onSubdocs', this.workspaceId, docId, added);
added.forEach(subdoc => {
this.setupListener(subdoc.guid);
@@ -126,7 +126,7 @@ export class WorkspaceSQLiteDB extends BaseSQLiteAdapter {
getDocAsUpdates = (docId?: string) => {
const doc = docId ? this.getDoc(docId) : this.yDoc;
if (doc) {
return Y.encodeStateAsUpdate(doc);
return encodeStateAsUpdate(doc);
}
return null;
};
@@ -144,7 +144,7 @@ export class WorkspaceSQLiteDB extends BaseSQLiteAdapter {
// yjs-idb will always trim the db for the first time after DB is loaded
const doc = this.getDoc(docId);
if (doc) {
Y.applyUpdate(doc, data, origin);
applyUpdate(doc, data, origin);
} else {
logger.warn('[WorkspaceSQLiteDB] applyUpdate: doc not found', docId);
}