refactor(editor): introduce store container to make implement doc easier (#12146)

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **New Features**
  - Introduced a new store management system for handling document stores, improving efficiency and flexibility when working with document data.

- **Refactor**
  - Updated internal store handling to use a centralized store container, simplifying store retrieval and removal across various components.
  - Renamed and updated several store-related method signatures for consistency and clarity.
  - Replaced editor extension loading logic with a new local implementation for better modularity.

- **Chores**
  - Improved and streamlined the export of store-related modules for better maintainability.
  - Removed obsolete and redundant code related to previous store management approaches.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Saul-Mirone
2025-05-07 06:08:43 +00:00
parent 93b1d6c729
commit 267bb3a975
11 changed files with 144 additions and 129 deletions
+7 -4
View File
@@ -2,12 +2,15 @@ import type * as Y from 'yjs';
import type { AwarenessStore } from '../yjs/awareness.js';
import type { YBlock } from './block/types.js';
import type { Query } from './store/query.js';
import type { Store, StoreOptions } from './store/store.js';
import type { Workspace } from './workspace.js';
import type { DocMeta } from './workspace-meta.js';
export type GetBlocksOptions = Omit<StoreOptions, 'schema' | 'doc'>;
export type GetStoreOptions = Omit<StoreOptions, 'schema' | 'doc'>;
export type RemoveStoreOptions = Pick<
StoreOptions,
'query' | 'id' | 'readonly'
>;
export interface Doc {
readonly id: string;
@@ -19,8 +22,8 @@ export interface Doc {
dispose(): void;
clear(): void;
getStore(options?: GetBlocksOptions): Store;
clearQuery(query: Query, readonly?: boolean): void;
getStore(options?: GetStoreOptions): Store;
removeStore(options: RemoveStoreOptions): void;
get loaded(): boolean;
get awarenessStore(): AwarenessStore;
@@ -1,5 +1,6 @@
export * from './block/index.js';
export * from './doc.js';
export * from './store/index.js';
export * from './store-container.js';
export * from './workspace.js';
export * from './workspace-meta.js';
@@ -0,0 +1,74 @@
import type { Doc, GetStoreOptions, RemoveStoreOptions } from './doc';
import { type Query, Store } from './store';
export class StoreContainer {
private readonly _storeMap = new Map<string, Store>();
constructor(readonly doc: Doc) {}
getStore = ({
readonly,
query,
provider,
extensions,
id,
}: GetStoreOptions = {}) => {
let idOrOptions: string | { readonly?: boolean; query?: Query };
if (readonly || query) {
idOrOptions = { readonly, query };
} else if (!id) {
idOrOptions = this.doc.workspace.idGenerator();
} else {
idOrOptions = id;
}
const key = this._getQueryKey(idOrOptions);
if (this._storeMap.has(key)) {
return this._storeMap.get(key) as Store;
}
const doc = new Store({
doc: this.doc,
readonly,
query,
provider,
extensions,
});
this._storeMap.set(key, doc);
return doc;
};
removeStore = ({ readonly, query, id }: RemoveStoreOptions) => {
let idOrOptions: string | { readonly?: boolean; query?: Query };
if (readonly || query) {
idOrOptions = { readonly, query };
} else if (!id) {
return;
} else {
idOrOptions = id;
}
const key = this._getQueryKey(idOrOptions);
this._storeMap.delete(key);
};
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;
};
private _getReadonlyKey(readonly?: boolean): 'true' | 'false' {
return (readonly?.toString() as 'true' | 'false') ?? 'false';
}
}