Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

refactor(editor): optimize store map (#10246)

Saul-Mironefaf6e2c79f4c
diff --git a/blocksuite/affine/block-embed/src/common/render-linked-doc.ts b/blocksuite/affine/block-embed/src/common/render-linked-doc.ts
index 4711228545..3ed5f5cd48 100644
--- a/blocksuite/affine/block-embed/src/common/render-linked-doc.ts
+++ b/blocksuite/affine/block-embed/src/common/render-linked-doc.ts
@@ -404,7 +404,6 @@ export function createLinkedDocFromSlice(
snapshots: BlockSnapshot[],
docTitle?: string
) {
- // const modelsWithChildren = (list:BlockModel[]):BlockModel[]=>list.flatMap(model=>[model,...modelsWithChildren(model.children)])
const linkedDoc = doc.workspace.createDoc({});
linkedDoc.load(() => {
const rootId = linkedDoc.addBlock('affine:page', {
diff --git a/blocksuite/affine/block-embed/src/embed-linked-doc-block/embed-linked-doc-block.ts b/blocksuite/affine/block-embed/src/embed-linked-doc-block/embed-linked-doc-block.ts
index 06be046690..75131e5605 100644
--- a/blocksuite/affine/block-embed/src/embed-linked-doc-block/embed-linked-doc-block.ts
+++ b/blocksuite/affine/block-embed/src/embed-linked-doc-block/embed-linked-doc-block.ts
@@ -246,7 +246,9 @@ export class EmbedLinkedDocBlockComponent extends EmbedBlockComponent<EmbedLinke
}
get linkedDoc() {
- return this.std.workspace.getDoc(this.model.pageId);
+ return this.std.workspace.getDoc(this.model.pageId, {
+ id: this.model.pageId,
+ });
}
private _handleDoubleClick(event: MouseEvent) {
diff --git a/blocksuite/framework/store/src/model/store/store.ts b/blocksuite/framework/store/src/model/store/store.ts
index eced871271..e76db3c8a5 100644
--- a/blocksuite/framework/store/src/model/store/store.ts
+++ b/blocksuite/framework/store/src/model/store/store.ts
@@ -22,6 +22,7 @@ import { syncBlockProps } from './utils.js';
export type StoreOptions = {
schema: Schema;
doc: Doc;
+ id?: string;
readonly?: boolean;
query?: Query;
provider?: ServiceProvider;
diff --git a/blocksuite/framework/store/src/test/test-doc.ts b/blocksuite/framework/store/src/test/test-doc.ts
index ffdb67bd77..fc169f0a06 100644
--- a/blocksuite/framework/store/src/test/test-doc.ts
+++ b/blocksuite/framework/store/src/test/test-doc.ts
@@ -23,11 +23,7 @@ export class TestDoc implements Doc {
private readonly _collection: Workspace;
- private readonly _docMap = {
- undefined: new Map<string, Store>(),
- true: new Map<string, Store>(),
- false: new Map<string, Store>(),
- };
+ private readonly _storeMap = new Map<string, Store>();
// doc/space container.
private readonly _handleYEvents = (events: Y.YEvent<YBlock | Y.Text>[]) => {
@@ -189,8 +185,8 @@ export class TestDoc implements Doc {
this._collection = collection;
}
- private _getReadonlyKey(readonly?: boolean): 'true' | 'false' | 'undefined' {
- return (readonly?.toString() as 'true' | 'false') ?? 'undefined';
+ private _getReadonlyKey(readonly?: boolean): 'true' | 'false' {
+ return (readonly?.toString() as 'true' | 'false') ?? 'false';
}
private _handleVersion() {
@@ -253,9 +249,8 @@ export class TestDoc implements Doc {
}
clearQuery(query: Query, readonly?: boolean) {
- const readonlyKey = this._getReadonlyKey(readonly);
-
- this._docMap[readonlyKey].delete(JSON.stringify(query));
+ const key = this._getQueryKey({ readonly, query });
+ this._storeMap.delete(key);
}
private _destroy() {
@@ -273,13 +268,40 @@ export class TestDoc implements Doc {
}
}
- getStore({ readonly, query, provider, extensions }: GetBlocksOptions = {}) {
+ private readonly _getQueryKey = (
+ idOrOptions: string | { readonly?: boolean; query?: Query }
+ ) => {
+ if (typeof idOrOptions === 'string') {
+ return idOrOptions;
+ }
+ const { readonly, query } = idOrOptions;
const readonlyKey = this._getReadonlyKey(readonly);
+ const key = JSON.stringify({
+ readonlyKey,
+ query,
+ });
+ return key;
+ };
- const key = JSON.stringify(query);
+ getStore({
+ readonly,
+ query,
+ provider,
+ extensions,
+ id,
+ }: GetBlocksOptions = {}) {
+ let idOrOptions: string | { readonly?: boolean; query?: Query };
+ if (id) {
+ idOrOptions = id;
+ } else if (readonly === undefined && query === undefined) {
+ idOrOptions = this.spaceDoc.guid;
+ } else {
+ idOrOptions = { readonly, query };
+ }
+ const key = this._getQueryKey(idOrOptions);
- if (this._docMap[readonlyKey].has(key)) {
- return this._docMap[readonlyKey].get(key)!;
+ if (this._storeMap.has(key)) {
+ return this._storeMap.get(key)!;
}
const doc = new Store({
@@ -293,7 +315,7 @@ export class TestDoc implements Doc {
),
});
- this._docMap[readonlyKey].set(key, doc);
+ this._storeMap.set(key, doc);
return doc;
}
diff --git a/blocksuite/framework/store/src/test/test-workspace.ts b/blocksuite/framework/store/src/test/test-workspace.ts
index 4fc4ce57f4..ef5a743fcf 100644
--- a/blocksuite/framework/store/src/test/test-workspace.ts
+++ b/blocksuite/framework/store/src/test/test-workspace.ts
@@ -180,7 +180,11 @@ export class TestWorkspace implements Workspace {
tags: [],
});
this.slots.docCreated.emit(docId);
- return this.getDoc(docId, { query, readonly }) as Store;
+ return this.getDoc(docId, {
+ id: docId,
+ query,
+ readonly,
+ }) as Store;
}
dispose() {
@@ -202,7 +206,10 @@ export class TestWorkspace implements Workspace {
return space ?? null;
}
- getDoc(docId: string, options?: GetBlocksOptions): Store | null {
+ getDoc(
+ docId: string,
+ options: GetBlocksOptions = { id: docId }
+ ): Store | null {
const collection = this.getBlockCollection(docId);
return collection?.getStore(options) ?? null;
}
diff --git a/packages/frontend/core/src/modules/workspace/impls/doc.ts b/packages/frontend/core/src/modules/workspace/impls/doc.ts
index 3c347829b1..6b607b3fcf 100644
--- a/packages/frontend/core/src/modules/workspace/impls/doc.ts
+++ b/packages/frontend/core/src/modules/workspace/impls/doc.ts
@@ -26,11 +26,7 @@ export class DocImpl implements Doc {
private readonly _collection: Workspace;
- private readonly _docMap = {
- undefined: new Map<string, Store>(),
- true: new Map<string, Store>(),
- false: new Map<string, Store>(),
- };
+ private readonly _storeMap = new Map<string, Store>();
// doc/space container.
private readonly _handleYEvents = (events: Y.YEvent<YBlock | Y.Text>[]) => {
@@ -174,8 +170,8 @@ export class DocImpl implements Doc {
this._collection = collection;
}
- private _getReadonlyKey(readonly?: boolean): 'true' | 'false' | 'undefined' {
- return (readonly?.toString() as 'true' | 'false') ?? 'undefined';
+ private _getReadonlyKey(readonly?: boolean): 'true' | 'false' {
+ return (readonly?.toString() as 'true' | 'false') ?? 'false';
}
private _handleVersion() {
@@ -238,9 +234,8 @@ export class DocImpl implements Doc {
}
clearQuery(query: Query, readonly?: boolean) {
- const readonlyKey = this._getReadonlyKey(readonly);
-
- this._docMap[readonlyKey].delete(JSON.stringify(query));
+ const key = this._getQueryKey({ readonly, query });
+ this._storeMap.delete(key);
}
private _destroy() {
@@ -258,13 +253,40 @@ export class DocImpl implements Doc {
}
}
- getStore({ readonly, query, provider, extensions }: GetBlocksOptions = {}) {
+ private readonly _getQueryKey = (
+ idOrOptions: string | { readonly?: boolean; query?: Query }
+ ) => {
+ if (typeof idOrOptions === 'string') {
+ return idOrOptions;
+ }
+ const { readonly, query } = idOrOptions;
const readonlyKey = this._getReadonlyKey(readonly);
+ const key = JSON.stringify({
+ readonlyKey,
+ query,
+ });
+ return key;
+ };
- const key = JSON.stringify(query);
+ getStore({
+ readonly,
+ query,
+ provider,
+ extensions,
+ id,
+ }: GetBlocksOptions = {}) {
+ let idOrOptions: string | { readonly?: boolean; query?: Query };
+ if (readonly || query) {
+ idOrOptions = { readonly, query };
+ } else if (!id) {
+ idOrOptions = this.workspace.idGenerator();
+ } else {
+ idOrOptions = id;
+ }
+ const key = this._getQueryKey(idOrOptions);
- if (this._docMap[readonlyKey].has(key)) {
- return this._docMap[readonlyKey].get(key) as Store;
+ if (this._storeMap.has(key)) {
+ return this._storeMap.get(key) as Store;
}
const storeExtensions = SpecProvider.getInstance().getSpec('store');
@@ -281,7 +303,7 @@ export class DocImpl implements Doc {
extensions: Array.from(extensionSet),
});
- this._docMap[readonlyKey].set(key, doc);
+ this._storeMap.set(key, doc);
return doc;
}
diff --git a/packages/frontend/core/src/modules/workspace/impls/workspace.ts b/packages/frontend/core/src/modules/workspace/impls/workspace.ts
index 3ed3d17ca3..d0e6844428 100644
--- a/packages/frontend/core/src/modules/workspace/impls/workspace.ts
+++ b/packages/frontend/core/src/modules/workspace/impls/workspace.ts
@@ -143,7 +143,11 @@ export class WorkspaceImpl implements Workspace {
tags: [],
});
this.slots.docCreated.emit(docId);
- return this.getDoc(docId, { query, readonly }) as Store;
+ return this.getDoc(docId, {
+ id: docId,
+ query,
+ readonly,
+ }) as Store;
}
dispose() {
@@ -155,7 +159,10 @@ export class WorkspaceImpl implements Workspace {
return space ?? null;
}
- getDoc(docId: string, options?: GetBlocksOptions): Store | null {
+ getDoc(
+ docId: string,
+ options: GetBlocksOptions = { id: docId }
+ ): Store | null {
const collection = this._getDoc(docId);
return collection?.getStore(options) ?? null;
}