mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-03 18:40:00 +08:00
refactor(editor): optimize store map (#10246)
This commit is contained in:
@@ -404,7 +404,6 @@ export function createLinkedDocFromSlice(
|
|||||||
snapshots: BlockSnapshot[],
|
snapshots: BlockSnapshot[],
|
||||||
docTitle?: string
|
docTitle?: string
|
||||||
) {
|
) {
|
||||||
// const modelsWithChildren = (list:BlockModel[]):BlockModel[]=>list.flatMap(model=>[model,...modelsWithChildren(model.children)])
|
|
||||||
const linkedDoc = doc.workspace.createDoc({});
|
const linkedDoc = doc.workspace.createDoc({});
|
||||||
linkedDoc.load(() => {
|
linkedDoc.load(() => {
|
||||||
const rootId = linkedDoc.addBlock('affine:page', {
|
const rootId = linkedDoc.addBlock('affine:page', {
|
||||||
|
|||||||
@@ -246,7 +246,9 @@ export class EmbedLinkedDocBlockComponent extends EmbedBlockComponent<EmbedLinke
|
|||||||
}
|
}
|
||||||
|
|
||||||
get linkedDoc() {
|
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) {
|
private _handleDoubleClick(event: MouseEvent) {
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import { syncBlockProps } from './utils.js';
|
|||||||
export type StoreOptions = {
|
export type StoreOptions = {
|
||||||
schema: Schema;
|
schema: Schema;
|
||||||
doc: Doc;
|
doc: Doc;
|
||||||
|
id?: string;
|
||||||
readonly?: boolean;
|
readonly?: boolean;
|
||||||
query?: Query;
|
query?: Query;
|
||||||
provider?: ServiceProvider;
|
provider?: ServiceProvider;
|
||||||
|
|||||||
@@ -23,11 +23,7 @@ export class TestDoc implements Doc {
|
|||||||
|
|
||||||
private readonly _collection: Workspace;
|
private readonly _collection: Workspace;
|
||||||
|
|
||||||
private readonly _docMap = {
|
private readonly _storeMap = new Map<string, Store>();
|
||||||
undefined: new Map<string, Store>(),
|
|
||||||
true: new Map<string, Store>(),
|
|
||||||
false: new Map<string, Store>(),
|
|
||||||
};
|
|
||||||
|
|
||||||
// doc/space container.
|
// doc/space container.
|
||||||
private readonly _handleYEvents = (events: Y.YEvent<YBlock | Y.Text>[]) => {
|
private readonly _handleYEvents = (events: Y.YEvent<YBlock | Y.Text>[]) => {
|
||||||
@@ -189,8 +185,8 @@ export class TestDoc implements Doc {
|
|||||||
this._collection = collection;
|
this._collection = collection;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _getReadonlyKey(readonly?: boolean): 'true' | 'false' | 'undefined' {
|
private _getReadonlyKey(readonly?: boolean): 'true' | 'false' {
|
||||||
return (readonly?.toString() as 'true' | 'false') ?? 'undefined';
|
return (readonly?.toString() as 'true' | 'false') ?? 'false';
|
||||||
}
|
}
|
||||||
|
|
||||||
private _handleVersion() {
|
private _handleVersion() {
|
||||||
@@ -253,9 +249,8 @@ export class TestDoc implements Doc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
clearQuery(query: Query, readonly?: boolean) {
|
clearQuery(query: Query, readonly?: boolean) {
|
||||||
const readonlyKey = this._getReadonlyKey(readonly);
|
const key = this._getQueryKey({ readonly, query });
|
||||||
|
this._storeMap.delete(key);
|
||||||
this._docMap[readonlyKey].delete(JSON.stringify(query));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _destroy() {
|
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 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)) {
|
if (this._storeMap.has(key)) {
|
||||||
return this._docMap[readonlyKey].get(key)!;
|
return this._storeMap.get(key)!;
|
||||||
}
|
}
|
||||||
|
|
||||||
const doc = new Store({
|
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;
|
return doc;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -180,7 +180,11 @@ export class TestWorkspace implements Workspace {
|
|||||||
tags: [],
|
tags: [],
|
||||||
});
|
});
|
||||||
this.slots.docCreated.emit(docId);
|
this.slots.docCreated.emit(docId);
|
||||||
return this.getDoc(docId, { query, readonly }) as Store;
|
return this.getDoc(docId, {
|
||||||
|
id: docId,
|
||||||
|
query,
|
||||||
|
readonly,
|
||||||
|
}) as Store;
|
||||||
}
|
}
|
||||||
|
|
||||||
dispose() {
|
dispose() {
|
||||||
@@ -202,7 +206,10 @@ export class TestWorkspace implements Workspace {
|
|||||||
return space ?? null;
|
return space ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
getDoc(docId: string, options?: GetBlocksOptions): Store | null {
|
getDoc(
|
||||||
|
docId: string,
|
||||||
|
options: GetBlocksOptions = { id: docId }
|
||||||
|
): Store | null {
|
||||||
const collection = this.getBlockCollection(docId);
|
const collection = this.getBlockCollection(docId);
|
||||||
return collection?.getStore(options) ?? null;
|
return collection?.getStore(options) ?? null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,11 +26,7 @@ export class DocImpl implements Doc {
|
|||||||
|
|
||||||
private readonly _collection: Workspace;
|
private readonly _collection: Workspace;
|
||||||
|
|
||||||
private readonly _docMap = {
|
private readonly _storeMap = new Map<string, Store>();
|
||||||
undefined: new Map<string, Store>(),
|
|
||||||
true: new Map<string, Store>(),
|
|
||||||
false: new Map<string, Store>(),
|
|
||||||
};
|
|
||||||
|
|
||||||
// doc/space container.
|
// doc/space container.
|
||||||
private readonly _handleYEvents = (events: Y.YEvent<YBlock | Y.Text>[]) => {
|
private readonly _handleYEvents = (events: Y.YEvent<YBlock | Y.Text>[]) => {
|
||||||
@@ -174,8 +170,8 @@ export class DocImpl implements Doc {
|
|||||||
this._collection = collection;
|
this._collection = collection;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _getReadonlyKey(readonly?: boolean): 'true' | 'false' | 'undefined' {
|
private _getReadonlyKey(readonly?: boolean): 'true' | 'false' {
|
||||||
return (readonly?.toString() as 'true' | 'false') ?? 'undefined';
|
return (readonly?.toString() as 'true' | 'false') ?? 'false';
|
||||||
}
|
}
|
||||||
|
|
||||||
private _handleVersion() {
|
private _handleVersion() {
|
||||||
@@ -238,9 +234,8 @@ export class DocImpl implements Doc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
clearQuery(query: Query, readonly?: boolean) {
|
clearQuery(query: Query, readonly?: boolean) {
|
||||||
const readonlyKey = this._getReadonlyKey(readonly);
|
const key = this._getQueryKey({ readonly, query });
|
||||||
|
this._storeMap.delete(key);
|
||||||
this._docMap[readonlyKey].delete(JSON.stringify(query));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _destroy() {
|
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 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)) {
|
if (this._storeMap.has(key)) {
|
||||||
return this._docMap[readonlyKey].get(key) as Store;
|
return this._storeMap.get(key) as Store;
|
||||||
}
|
}
|
||||||
|
|
||||||
const storeExtensions = SpecProvider.getInstance().getSpec('store');
|
const storeExtensions = SpecProvider.getInstance().getSpec('store');
|
||||||
@@ -281,7 +303,7 @@ export class DocImpl implements Doc {
|
|||||||
extensions: Array.from(extensionSet),
|
extensions: Array.from(extensionSet),
|
||||||
});
|
});
|
||||||
|
|
||||||
this._docMap[readonlyKey].set(key, doc);
|
this._storeMap.set(key, doc);
|
||||||
|
|
||||||
return doc;
|
return doc;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -143,7 +143,11 @@ export class WorkspaceImpl implements Workspace {
|
|||||||
tags: [],
|
tags: [],
|
||||||
});
|
});
|
||||||
this.slots.docCreated.emit(docId);
|
this.slots.docCreated.emit(docId);
|
||||||
return this.getDoc(docId, { query, readonly }) as Store;
|
return this.getDoc(docId, {
|
||||||
|
id: docId,
|
||||||
|
query,
|
||||||
|
readonly,
|
||||||
|
}) as Store;
|
||||||
}
|
}
|
||||||
|
|
||||||
dispose() {
|
dispose() {
|
||||||
@@ -155,7 +159,10 @@ export class WorkspaceImpl implements Workspace {
|
|||||||
return space ?? null;
|
return space ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
getDoc(docId: string, options?: GetBlocksOptions): Store | null {
|
getDoc(
|
||||||
|
docId: string,
|
||||||
|
options: GetBlocksOptions = { id: docId }
|
||||||
|
): Store | null {
|
||||||
const collection = this._getDoc(docId);
|
const collection = this._getDoc(docId);
|
||||||
return collection?.getStore(options) ?? null;
|
return collection?.getStore(options) ?? null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user