refactor: using unified nanoid (#4519)

This commit is contained in:
Alex Yang
2023-09-27 21:53:04 -05:00
committed by GitHub
parent 56f75160f3
commit 603f82ffc2
38 changed files with 258 additions and 183 deletions

View File

@@ -54,12 +54,12 @@
"rxjs": "^7.8.1"
},
"devDependencies": {
"@blocksuite/blocks": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/editor": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/global": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/blocks": "0.0.0-20230926212737-6d4b1569-nightly",
"@blocksuite/editor": "0.0.0-20230926212737-6d4b1569-nightly",
"@blocksuite/global": "0.0.0-20230926212737-6d4b1569-nightly",
"@blocksuite/icons": "^2.1.33",
"@blocksuite/lit": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/store": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/lit": "0.0.0-20230926212737-6d4b1569-nightly",
"@blocksuite/store": "0.0.0-20230926212737-6d4b1569-nightly",
"@types/react": "^18.2.21",
"@types/react-datepicker": "^4.15.0",
"@types/react-dnd": "^3.0.2",

View File

@@ -1,5 +1,5 @@
import { uuidv4 } from '@blocksuite/store';
import { atom } from 'jotai';
import { nanoid } from 'nanoid';
export type Notification = {
key?: string;
@@ -42,7 +42,7 @@ export const removeNotificationAtom = atom(null, (_, set, key: string) => {
export const pushNotificationAtom = atom<null, [Notification], void>(
null,
(_, set, newNotification) => {
newNotification.key = newNotification.key || uuidv4();
newNotification.key = newNotification.key || nanoid();
const key = newNotification.key;
const removeNotification = () =>
set(notificationsBaseAtom, notifications =>

View File

@@ -8,9 +8,9 @@ import {
RemoveIcon,
SaveIcon,
} from '@blocksuite/icons';
import { uuidv4 } from '@blocksuite/store';
import { Button } from '@toeverything/components/button';
import { Modal } from '@toeverything/components/modal';
import { nanoid } from 'nanoid';
import { useCallback, useMemo, useState } from 'react';
import { Input, ScrollableContainer } from '../../..';
@@ -280,7 +280,7 @@ export const SaveCollectionButton = ({
const handleClick = useCallback(() => {
changeShow(true);
setInit({
id: uuidv4(),
id: nanoid(),
name: '',
filterList,
workspaceId,

View File

@@ -5,7 +5,7 @@
"main": "./src/index.ts",
"module": "./src/index.ts",
"devDependencies": {
"@blocksuite/global": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/global": "0.0.0-20230926212737-6d4b1569-nightly",
"react": "18.2.0",
"react-dom": "18.2.0",
"zod": "^3.22.2"

View File

@@ -12,12 +12,12 @@
"devDependencies": {
"@affine/env": "workspace:*",
"@affine/y-provider": "workspace:*",
"@blocksuite/block-std": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/blocks": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/editor": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/global": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/lit": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/store": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/block-std": "0.0.0-20230926212737-6d4b1569-nightly",
"@blocksuite/blocks": "0.0.0-20230926212737-6d4b1569-nightly",
"@blocksuite/editor": "0.0.0-20230926212737-6d4b1569-nightly",
"@blocksuite/global": "0.0.0-20230926212737-6d4b1569-nightly",
"@blocksuite/lit": "0.0.0-20230926212737-6d4b1569-nightly",
"@blocksuite/store": "0.0.0-20230926212737-6d4b1569-nightly",
"@types/lodash.debounce": "^4.0.7"
},
"peerDependencies": {

View File

@@ -55,16 +55,16 @@
},
"dependencies": {
"@affine/sdk": "workspace:*",
"@blocksuite/blocks": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/global": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/store": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/blocks": "0.0.0-20230926212737-6d4b1569-nightly",
"@blocksuite/global": "0.0.0-20230926212737-6d4b1569-nightly",
"@blocksuite/store": "0.0.0-20230926212737-6d4b1569-nightly",
"jotai": "^2.4.1",
"tinykeys": "^2.1.0",
"zod": "^3.22.2"
},
"devDependencies": {
"@blocksuite/editor": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/lit": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/editor": "0.0.0-20230926212737-6d4b1569-nightly",
"@blocksuite/lit": "0.0.0-20230926212737-6d4b1569-nightly",
"async-call-rpc": "^6.3.1",
"electron": "link:../../apps/electron/node_modules/electron",
"react": "^18.2.0",

View File

@@ -33,7 +33,7 @@ describe('migration', () => {
}
const blocks = doc.getMap('space:hello-world').toJSON();
const newDoc = migrateToSubdoc(doc);
const subDoc = newDoc.getMap('spaces').get('space:hello-world') as Doc;
const subDoc = newDoc.getMap('spaces').values().next().value as Doc;
const data = (subDoc.toJSON() as any).blocks;
Object.keys(data).forEach(id => {
if (id === 'xyWNqindHH') {

View File

@@ -213,6 +213,7 @@ import { applyUpdate, encodeStateAsUpdate } from 'yjs';
const migrationOrigin = 'affine-migration';
import type { Schema } from '@blocksuite/store';
import { nanoid } from 'nanoid';
type XYWH = [number, number, number, number];
@@ -404,7 +405,11 @@ function updateBlockVersions(versions: YMap<number>) {
}
}
function migrateMeta(oldDoc: YDoc, newDoc: YDoc) {
function migrateMeta(
oldDoc: YDoc,
newDoc: YDoc,
idMap: Record<string, string>
) {
const originalMeta = oldDoc.getMap('space:meta');
const originalVersions = originalMeta.get('versions') as YMap<number>;
const originalPages = originalMeta.get('pages') as YArray<YMap<unknown>>;
@@ -423,24 +428,34 @@ function migrateMeta(oldDoc: YDoc, newDoc: YDoc) {
Array.from(page.entries())
.filter(([key]) => key !== 'subpageIds')
.forEach(([key, value]) => {
map.set(key, value);
if (key === 'id') {
idMap[value] = nanoid();
map.set(key, idMap[value]);
} else {
map.set(key, value);
}
});
return map;
});
pages.push(mapList);
}
function migrateBlocks(oldDoc: YDoc, newDoc: YDoc) {
function migrateBlocks(
oldDoc: YDoc,
newDoc: YDoc,
idMap: Record<string, string>
) {
const spaces = newDoc.getMap('spaces');
const originalMeta = oldDoc.getMap('space:meta');
const originalVersions = originalMeta.get('versions') as YMap<number>;
const originalPages = originalMeta.get('pages') as YArray<YMap<unknown>>;
originalPages.forEach(page => {
const id = page.get('id') as string;
const newId = idMap[id];
const spaceId = id.startsWith('space:') ? id : `space:${id}`;
const originalBlocks = oldDoc.getMap(spaceId) as YMap<unknown>;
const subdoc = new YDoc();
spaces.set(spaceId, subdoc);
spaces.set(newId, subdoc);
const blocks = subdoc.getMap('blocks');
Array.from(originalBlocks.entries()).forEach(([key, value]) => {
const blockData = value.clone();
@@ -461,8 +476,9 @@ export function migrateToSubdoc(oldDoc: YDoc): YDoc {
return oldDoc;
}
const newDoc = new YDoc();
migrateMeta(oldDoc, newDoc);
migrateBlocks(oldDoc, newDoc);
const idMap = {} as Record<string, string>;
migrateMeta(oldDoc, newDoc, idMap);
migrateBlocks(oldDoc, newDoc, idMap);
return newDoc;
}

View File

@@ -36,6 +36,7 @@ export default defineConfig({
'zod',
'react',
'yjs',
'nanoid',
/^jotai/,
/^@blocksuite/,
/^@affine\/templates/,

View File

@@ -22,9 +22,9 @@
"dist"
],
"dependencies": {
"@blocksuite/blocks": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/global": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/store": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/blocks": "0.0.0-20230926212737-6d4b1569-nightly",
"@blocksuite/global": "0.0.0-20230926212737-6d4b1569-nightly",
"@blocksuite/store": "0.0.0-20230926212737-6d4b1569-nightly",
"jotai": "^2.4.1",
"zod": "^3.22.2"
},

View File

@@ -60,7 +60,7 @@ describe('crud', () => {
expect(localWorkspace.blockSuiteWorkspace.doc.toJSON()).toEqual({
meta: expect.anything(),
spaces: expect.objectContaining({
'space:page0': expect.anything(),
page0: expect.anything(),
}),
});

View File

@@ -1,8 +1,9 @@
import { DebugLogger } from '@affine/debug';
import type { LocalWorkspace, WorkspaceCRUD } from '@affine/env/workspace';
import { WorkspaceFlavour } from '@affine/env/workspace';
import { nanoid, Workspace as BlockSuiteWorkspace } from '@blocksuite/store';
import { Workspace as BlockSuiteWorkspace } from '@blocksuite/store';
import { createJSONStorage } from 'jotai/utils';
import { nanoid } from 'nanoid';
import { z } from 'zod';
import { getOrCreateWorkspace } from '../manager';

View File

@@ -76,7 +76,7 @@ describe('download provider', () => {
...prev,
// download provider only download the root doc
spaces: {
'space:page0': {
page0: {
blocks: {},
},
},

View File

@@ -5,12 +5,13 @@ import type {
import { getDoc } from '@affine/y-provider';
import { __unstableSchemas, AffineSchemas } from '@blocksuite/blocks/models';
import type { Y as YType } from '@blocksuite/store';
import { Schema, uuidv4, Workspace } from '@blocksuite/store';
import { Schema, Workspace } from '@blocksuite/store';
import type { DBHandlerManager } from '@toeverything/infra/handler';
import type {
EventMap,
UnwrapManagerHandlerToClientSide,
} from '@toeverything/infra/type';
import { nanoid } from 'nanoid';
import { setTimeout } from 'timers/promises';
import { beforeEach, describe, expect, test, vi } from 'vitest';
@@ -79,7 +80,7 @@ const schema = new Schema();
schema.register(AffineSchemas).register(__unstableSchemas);
beforeEach(() => {
id = uuidv4();
id = nanoid();
workspace = new Workspace({
id,
isSSR: true,

View File

@@ -38,8 +38,8 @@
},
"devDependencies": {
"@affine/y-provider": "workspace:*",
"@blocksuite/blocks": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/store": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/blocks": "0.0.0-20230926212737-6d4b1569-nightly",
"@blocksuite/store": "0.0.0-20230926212737-6d4b1569-nightly",
"vite": "^4.4.9",
"vite-plugin-dts": "3.5.3",
"y-indexeddb": "^9.0.11"

View File

@@ -8,8 +8,9 @@ import { setTimeout } from 'node:timers/promises';
import { __unstableSchemas, AffineSchemas } from '@blocksuite/blocks/models';
import { assertExists } from '@blocksuite/global/utils';
import type { Page } from '@blocksuite/store';
import { Schema, uuidv4, Workspace } from '@blocksuite/store';
import { Schema, Workspace } from '@blocksuite/store';
import { openDB } from 'idb';
import { nanoid } from 'nanoid';
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import { applyUpdate, Doc, encodeStateAsUpdate } from 'yjs';
@@ -61,7 +62,7 @@ const schema = new Schema();
schema.register(AffineSchemas).register(__unstableSchemas);
beforeEach(() => {
id = uuidv4();
id = nanoid();
workspace = new Workspace({
id,
isSSR: true,
@@ -119,7 +120,7 @@ describe('indexeddb provider', () => {
data.updates.forEach(({ update }) => {
Workspace.Y.applyUpdate(testWorkspace.doc, update);
});
const subPage = testWorkspace.doc.spaces.get('space:page0');
const subPage = testWorkspace.doc.spaces.get('page0');
{
assertExists(subPage);
await store.get(subPage.guid);

View File

@@ -9,7 +9,7 @@
".": "./src/index.ts"
},
"devDependencies": {
"@blocksuite/store": "0.0.0-20230921103931-38d8f07a-nightly"
"@blocksuite/store": "0.0.0-20230926212737-6d4b1569-nightly"
},
"peerDependencies": {
"yjs": "^13.5.51"

View File

@@ -97,6 +97,42 @@ export const createLazyProvider = (
async function syncDoc(doc: Doc) {
const guid = doc.guid;
{
// backport from `@blocksuite/store`
const prefixId = guid.startsWith('space:') ? guid.slice(6) : guid;
const possible1 = `${rootDoc.guid}:space:${prefixId}`;
const possible2 = `space:${prefixId}`;
const update1 = await datasource.queryDocState(possible1);
const update2 = await datasource.queryDocState(possible2);
let hasUpdate = false;
if (
update1 &&
update1.missing.length !== 2 &&
update1.missing[0] !== 0 &&
update1.missing[1] !== 0
) {
applyUpdate(doc, update1.missing, origin);
hasUpdate = true;
}
if (
update2 &&
update2.missing.length !== 2 &&
update2.missing[0] !== 0 &&
update2.missing[1] !== 0
) {
applyUpdate(doc, update2.missing, origin);
hasUpdate = true;
}
if (hasUpdate) {
await datasource.sendDocUpdate(
guid,
encodeStateAsUpdate(
doc,
update1 ? update1.state : update2 ? update2.state : undefined
)
);
}
}
if (!connected) {
return;
}