mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 12:55:00 +00:00
refactor(editor): optimize store map (#10246)
This commit is contained in:
@@ -22,6 +22,7 @@ import { syncBlockProps } from './utils.js';
|
||||
export type StoreOptions = {
|
||||
schema: Schema;
|
||||
doc: Doc;
|
||||
id?: string;
|
||||
readonly?: boolean;
|
||||
query?: Query;
|
||||
provider?: ServiceProvider;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user