diff --git a/libs/datasource/jwt/src/adapter/yjs/binary.ts b/libs/datasource/jwt/src/adapter/yjs/binary.ts index d475cd05f2..5c677a4f51 100644 --- a/libs/datasource/jwt/src/adapter/yjs/binary.ts +++ b/libs/datasource/jwt/src/adapter/yjs/binary.ts @@ -3,29 +3,29 @@ import { Array as YArray, Map as YMap } from 'yjs'; import { RemoteKvService } from '@toeverything/datasource/remote-kv'; export class YjsRemoteBinaries { - readonly #binaries: YMap>; // binary instance - readonly #remote_storage?: RemoteKvService; + readonly _binaries: YMap>; // binary instance + readonly _remoteStorage?: RemoteKvService; constructor(binaries: YMap>, remote_token?: string) { - this.#binaries = binaries; + this._binaries = binaries; if (remote_token) { - this.#remote_storage = new RemoteKvService(remote_token); + this._remoteStorage = new RemoteKvService(remote_token); } else { console.warn(`Remote storage is not ready`); } } has(name: string): boolean { - return this.#binaries.has(name); + return this._binaries.has(name); } async get(name: string): Promise | undefined> { - if (this.#binaries.has(name)) { - return this.#binaries.get(name); + if (this._binaries.has(name)) { + return this._binaries.get(name); } else { // TODO: Remote Load try { - const file = await this.#remote_storage?.instance.getBuffData( + const file = await this._remoteStorage?.instance.getBuffData( name ); console.log(file); @@ -38,16 +38,16 @@ export class YjsRemoteBinaries { } async set(name: string, binary: YArray) { - if (!this.#binaries.has(name)) { + if (!this._binaries.has(name)) { console.log(name, 'name'); if (binary.length === 1) { - this.#binaries.set(name, binary); - if (this.#remote_storage) { + this._binaries.set(name, binary); + if (this._remoteStorage) { // TODO: Remote Save, if there is an object with the same name remotely, the upload is skipped, because the file name is the hash of the file content - const has_file = this.#remote_storage.instance.exist(name); + const has_file = this._remoteStorage.instance.exist(name); if (!has_file) { const upload_file = new File(binary.toArray(), name); - await this.#remote_storage.instance + await this._remoteStorage.instance .upload(upload_file) .catch(err => { throw new Error(`${err} upload error`); diff --git a/libs/datasource/jwt/src/adapter/yjs/block.ts b/libs/datasource/jwt/src/adapter/yjs/block.ts index 2faf3b80fe..ad60aa31e0 100644 --- a/libs/datasource/jwt/src/adapter/yjs/block.ts +++ b/libs/datasource/jwt/src/adapter/yjs/block.ts @@ -32,49 +32,49 @@ type YjsBlockInstanceProps = { }; export class YjsBlockInstance implements BlockInstance { - readonly #id: string; - readonly #block: YMap; - readonly #binary?: YArray; - readonly #children: YArray; - readonly #set_block: ( + readonly _id: string; + readonly _block: YMap; + readonly _binary?: YArray; + readonly _children: YArray; + readonly _setBlock: ( id: string, block: BlockItem ) => Promise; - readonly #get_updated: (id: string) => number | undefined; - readonly #get_creator: (id: string) => string | undefined; - readonly #get_block_instance: (id: string) => YjsBlockInstance | undefined; - readonly #children_listeners: Map; - readonly #content_listeners: Map; + readonly _getUpdated: (id: string) => number | undefined; + readonly _getCreator: (id: string) => string | undefined; + readonly _getBlockInstance: (id: string) => YjsBlockInstance | undefined; + readonly _childrenListeners: Map; + readonly _contentListeners: Map; // eslint-disable-next-line @typescript-eslint/naming-convention - #children_map: Map; + _childrenMap: Map; constructor(props: YjsBlockInstanceProps) { - this.#id = props.id; - this.#block = props.block; - this.#binary = props.binary; + this._id = props.id; + this._block = props.block; + this._binary = props.binary; - this.#children = props.block.get('children') as YArray; - this.#children_map = getMapFromYArray(this.#children); - this.#set_block = props.setBlock; - this.#get_updated = props.getUpdated; - this.#get_creator = props.getCreator; - this.#get_block_instance = props.getBlockInstance; + this._children = props.block.get('children') as YArray; + this._childrenMap = getMapFromYArray(this._children); + this._setBlock = props.setBlock; + this._getUpdated = props.getUpdated; + this._getCreator = props.getCreator; + this._getBlockInstance = props.getBlockInstance; - this.#children_listeners = new Map(); - this.#content_listeners = new Map(); + this._childrenListeners = new Map(); + this._contentListeners = new Map(); - const content = this.#block.get('content') as YMap; + const content = this._block.get('content') as YMap; - this.#children.observe(event => - ChildrenListenerHandler(this.#children_listeners, event) + this._children.observe(event => + ChildrenListenerHandler(this._childrenListeners, event) ); content?.observeDeep(events => - ContentListenerHandler(this.#content_listeners, events) + ContentListenerHandler(this._contentListeners, events) ); // TODO: flavor needs optimization - this.#block.observeDeep(events => - ContentListenerHandler(this.#content_listeners, events) + this._block.observeDeep(events => + ContentListenerHandler(this._contentListeners, events) ); } @@ -99,85 +99,85 @@ export class YjsBlockInstance implements BlockInstance { } addChildrenListener(name: string, listener: BlockListener): void { - this.#children_listeners.set(name, listener); + this._childrenListeners.set(name, listener); } removeChildrenListener(name: string): void { - this.#children_listeners.delete(name); + this._childrenListeners.delete(name); } addContentListener(name: string, listener: BlockListener): void { - this.#content_listeners.set(name, listener); + this._contentListeners.set(name, listener); } removeContentListener(name: string): void { - this.#content_listeners.delete(name); + this._contentListeners.delete(name); } get id() { - return this.#id; + return this._id; } get content(): YjsContentOperation { if (this.type === BlockTypes.block) { - const content = this.#block.get('content'); + const content = this._block.get('content'); if (content instanceof YAbstractType) { return new YjsContentOperation(content); } else { throw new Error(`Invalid content type: ${typeof content}`); } - } else if (this.type === BlockTypes.binary && this.#binary) { - return new YjsContentOperation(this.#binary); + } else if (this.type === BlockTypes.binary && this._binary) { + return new YjsContentOperation(this._binary); } throw new Error( - `Invalid content type: ${this.type}, ${this.#block.get( + `Invalid content type: ${this.type}, ${this._block.get( 'content' - )}, ${this.#binary}` + )}, ${this._binary}` ); } get type(): BlockItem['type'] { - return this.#block.get( + return this._block.get( 'type' ) as BlockItem['type']; } get flavor(): BlockItem['flavor'] { - return this.#block.get( + return this._block.get( 'flavor' ) as BlockItem['flavor']; } // TODO: bad case. Need to optimize. setFlavor(flavor: BlockItem['flavor']) { - this.#block.set('flavor', flavor); + this._block.set('flavor', flavor); } get created(): BlockItem['created'] { - return this.#block.get( + return this._block.get( 'created' ) as BlockItem['created']; } get updated(): number { - return this.#get_updated(this.#id) || this.created; + return this._getUpdated(this._id) || this.created; } get creator(): string | undefined { - return this.#get_creator(this.#id); + return this._getCreator(this._id); } get children(): string[] { - return this.#children.toArray(); + return this._children.toArray(); } getChildren(ids?: (string | undefined)[]): YjsBlockInstance[] { const query_ids = ids?.filter((id): id is string => !!id) || []; - const exists_ids = this.#children.map(id => id); + const exists_ids = this._children.map(id => id); const filter_ids = query_ids.length ? query_ids : exists_ids; return exists_ids .filter(id => filter_ids.includes(id)) - .map(id => this.#get_block_instance(id)) + .map(id => this._getBlockInstance(id)) .filter((v): v is YjsBlockInstance => !!v); } @@ -196,7 +196,7 @@ export class YjsBlockInstance implements BlockInstance { return pos; } } else if (before) { - const current_pos = this.#children_map.get(before || ''); + const current_pos = this._childrenMap.get(before || ''); if ( typeof current_pos === 'number' && Number.isInteger(current_pos) @@ -207,7 +207,7 @@ export class YjsBlockInstance implements BlockInstance { } } } else if (after) { - const current_pos = this.#children_map.get(after || ''); + const current_pos = this._childrenMap.get(after || ''); if ( typeof current_pos === 'number' && Number.isInteger(current_pos) @@ -227,44 +227,44 @@ export class YjsBlockInstance implements BlockInstance { ): Promise { const content = block[GET_BLOCK_ITEM](); if (content) { - const lastIndex = this.#children_map.get(block.id); + const lastIndex = this._childrenMap.get(block.id); if (typeof lastIndex === 'number') { - this.#children.delete(lastIndex); - this.#children_map = getMapFromYArray(this.#children); + this._children.delete(lastIndex); + this._childrenMap = getMapFromYArray(this._children); } const position = this.position_calculator( - this.#children_map.size, + this._childrenMap.size, pos ); if (typeof position === 'number') { - this.#children.insert(position, [block.id]); + this._children.insert(position, [block.id]); } else { - this.#children.push([block.id]); + this._children.push([block.id]); } - await this.#set_block(block.id, content); - this.#children_map = getMapFromYArray(this.#children); + await this._setBlock(block.id, content); + this._childrenMap = getMapFromYArray(this._children); } } removeChildren(ids: (string | undefined)[]): Promise { return new Promise(resolve => { - if (this.#children.doc) { - transact(this.#children.doc, () => { + if (this._children.doc) { + transact(this._children.doc, () => { const failed = []; for (const id of ids) { let idx = -1; - for (const block_id of this.#children) { + for (const block_id of this._children) { idx += 1; if (block_id === id) { - this.#children.delete(idx); + this._children.delete(idx); break; } } if (id) failed.push(id); } - this.#children_map = getMapFromYArray(this.#children); + this._childrenMap = getMapFromYArray(this._children); resolve(failed); }); } else { @@ -274,7 +274,7 @@ export class YjsBlockInstance implements BlockInstance { } public scopedHistory(scope: any[]): HistoryManager { - return new YjsHistoryManager(this.#block, scope); + return new YjsHistoryManager(this._block, scope); } [GET_BLOCK_ITEM]() { @@ -283,7 +283,7 @@ export class YjsBlockInstance implements BlockInstance { return { type: this.type, flavor: this.flavor, - children: this.#children.slice(), + children: this._children.slice(), created: this.created, content: this.content, }; diff --git a/libs/datasource/jwt/src/adapter/yjs/gatekeeper.ts b/libs/datasource/jwt/src/adapter/yjs/gatekeeper.ts index 1dd739de4f..1ee3e93ca3 100644 --- a/libs/datasource/jwt/src/adapter/yjs/gatekeeper.ts +++ b/libs/datasource/jwt/src/adapter/yjs/gatekeeper.ts @@ -2,35 +2,35 @@ import { Map as YMap } from 'yjs'; export class GateKeeper { // eslint-disable-next-line @typescript-eslint/naming-convention - #user_id: string; - #creators: YMap; - #common: YMap; + _userId: string; + _creators: YMap; + _common: YMap; constructor(userId: string, creators: YMap, common: YMap) { - this.#user_id = userId; - this.#creators = creators; - this.#common = common; + this._userId = userId; + this._creators = creators; + this._common = common; } getCreator(block_id: string): string | undefined { - return this.#creators.get(block_id) || this.#common.get(block_id); + return this._creators.get(block_id) || this._common.get(block_id); } setCreator(block_id: string) { - if (!this.#creators.get(block_id)) { - this.#creators.set(block_id, this.#user_id); + if (!this._creators.get(block_id)) { + this._creators.set(block_id, this._userId); } } setCommon(block_id: string) { - if (!this.#creators.get(block_id) && !this.#common.get(block_id)) { - this.#common.set(block_id, this.#user_id); + if (!this._creators.get(block_id) && !this._common.get(block_id)) { + this._common.set(block_id, this._userId); } } private check_delete(block_id: string): boolean { - const creator = this.#creators.get(block_id); - return creator === this.#user_id || !!this.#common.get(block_id); + const creator = this._creators.get(block_id); + return creator === this._userId || !!this._common.get(block_id); } checkDeleteLists(block_ids: string[]) { @@ -47,7 +47,7 @@ export class GateKeeper { } clear() { - this.#creators.clear(); - this.#common.clear(); + this._creators.clear(); + this._common.clear(); } } diff --git a/libs/datasource/jwt/src/adapter/yjs/history.ts b/libs/datasource/jwt/src/adapter/yjs/history.ts index 0434d340d8..34884a1150 100644 --- a/libs/datasource/jwt/src/adapter/yjs/history.ts +++ b/libs/datasource/jwt/src/adapter/yjs/history.ts @@ -5,34 +5,34 @@ import { HistoryCallback, HistoryManager } from '../../adapter'; type StackItem = UndoManager['undoStack'][0]; export class YjsHistoryManager implements HistoryManager { - readonly #blocks: YMap; - readonly #history_manager: UndoManager; - readonly #push_listeners: Map>; - readonly #pop_listeners: Map>; + readonly _blocks: YMap; + readonly _historyManager: UndoManager; + readonly _pushListeners: Map>; + readonly _popListeners: Map>; constructor(scope: YMap, tracker?: any[]) { - this.#blocks = scope; - this.#history_manager = new UndoManager(scope, { + this._blocks = scope; + this._historyManager = new UndoManager(scope, { trackedOrigins: tracker ? new Set(tracker) : undefined, }); - this.#push_listeners = new Map(); - this.#history_manager.on( + this._pushListeners = new Map(); + this._historyManager.on( 'stack-item-added', (event: { stackItem: StackItem }) => { const meta = event.stackItem.meta; - for (const listener of this.#push_listeners.values()) { + for (const listener of this._pushListeners.values()) { listener(meta); } } ); - this.#pop_listeners = new Map(); - this.#history_manager.on( + this._popListeners = new Map(); + this._historyManager.on( 'stack-item-popped', (event: { stackItem: StackItem }) => { const meta = event.stackItem.meta; - for (const listener of this.#pop_listeners.values()) { + for (const listener of this._popListeners.values()) { listener(new Map(meta)); } } @@ -40,19 +40,19 @@ export class YjsHistoryManager implements HistoryManager { } onPush(name: string, callback: HistoryCallback): void { - this.#push_listeners.set(name, callback); + this._pushListeners.set(name, callback); } offPush(name: string): boolean { - return this.#push_listeners.delete(name); + return this._pushListeners.delete(name); } onPop(name: string, callback: HistoryCallback): void { - this.#pop_listeners.set(name, callback); + this._popListeners.set(name, callback); } offPop(name: string): boolean { - return this.#pop_listeners.delete(name); + return this._popListeners.delete(name); } break(): void { @@ -60,14 +60,14 @@ export class YjsHistoryManager implements HistoryManager { } undo(): Map | undefined { - return this.#history_manager.undo()?.meta; + return this._historyManager.undo()?.meta; } redo(): Map | undefined { - return this.#history_manager.redo()?.meta; + return this._historyManager.redo()?.meta; } clear(): void { - return this.#history_manager.clear(); + return this._historyManager.clear(); } } diff --git a/libs/datasource/jwt/src/adapter/yjs/index.ts b/libs/datasource/jwt/src/adapter/yjs/index.ts index 1037d9e3f6..1a0a637859 100644 --- a/libs/datasource/jwt/src/adapter/yjs/index.ts +++ b/libs/datasource/jwt/src/adapter/yjs/index.ts @@ -178,22 +178,22 @@ export type YjsInitOptions = { }; export class YjsAdapter implements AsyncDatabaseAdapter { - readonly #provider: YjsProviders; - readonly #doc: Doc; // doc instance - readonly #awareness: Awareness; // lightweight state synchronization - readonly #gatekeeper: GateKeeper; // Simple access control - readonly #history: YjsHistoryManager; + readonly _provider: YjsProviders; + readonly _doc: Doc; // doc instance + readonly _awareness: Awareness; // lightweight state synchronization + readonly _gatekeeper: GateKeeper; // Simple access control + readonly _history: YjsHistoryManager; // Block Collection // key is a randomly generated global id - readonly #blocks: YMap>; - readonly #block_updated: YMap; + readonly _blocks: YMap>; + readonly _blockUpdated: YMap; // Maximum cache Block 1024, ttl 10 minutes - readonly #block_caches: LRUCache; + readonly _blockCaches: LRUCache; - readonly #binaries: YjsRemoteBinaries; + readonly _binaries: YjsRemoteBinaries; - readonly #listener: Map>; + readonly _listener: Map>; static async init( workspace: string, @@ -209,30 +209,30 @@ export class YjsAdapter implements AsyncDatabaseAdapter { } private constructor(providers: YjsProviders) { - this.#provider = providers; - this.#doc = providers.idb.doc; - this.#awareness = providers.awareness; - this.#gatekeeper = providers.gatekeeper; + this._provider = providers; + this._doc = providers.idb.doc; + this._awareness = providers.awareness; + this._gatekeeper = providers.gatekeeper; - const blocks = this.#doc.getMap>('blocks'); - this.#blocks = + const blocks = this._doc.getMap>('blocks'); + this._blocks = blocks.get('content') || blocks.set('content', new YMap()); - this.#block_updated = + this._blockUpdated = blocks.get('updated') || blocks.set('updated', new YMap()); - this.#block_caches = new LRUCache({ max: 1024, ttl: 1000 * 60 * 10 }); - this.#binaries = new YjsRemoteBinaries( + this._blockCaches = new LRUCache({ max: 1024, ttl: 1000 * 60 * 10 }); + this._binaries = new YjsRemoteBinaries( providers.binariesIdb.doc.getMap(), providers.remoteToken ); - this.#history = new YjsHistoryManager(this.#blocks); + this._history = new YjsHistoryManager(this._blocks); - this.#listener = new Map(); + this._listener = new Map(); const ws = providers.ws as any; if (ws) { const workspace = providers.idb.name; const emitState = (connectivity: Connectivity) => { - this.#listener.get('connectivity')?.( + this._listener.get('connectivity')?.( new Map([[workspace, connectivity]]) ); }; @@ -244,9 +244,9 @@ export class YjsAdapter implements AsyncDatabaseAdapter { const debounced_editing_notifier = debounce( () => { const listener: BlockListener> | undefined = - this.#listener.get('editing'); + this._listener.get('editing'); if (listener) { - const mapping = this.#awareness.getStates(); + const mapping = this._awareness.getStates(); const editing_mapping: Record = {}; for (const { userId, @@ -280,11 +280,11 @@ export class YjsAdapter implements AsyncDatabaseAdapter { { maxWait: 1000 } ); - this.#awareness.setLocalStateField('userId', providers.userId); + this._awareness.setLocalStateField('userId', providers.userId); - this.#awareness.on('update', debounced_editing_notifier); + this._awareness.on('update', debounced_editing_notifier); - this.#blocks.observeDeep(events => { + this._blocks.observeDeep(events => { const now = Date.now(); const keys = events.flatMap(e => { @@ -300,14 +300,14 @@ export class YjsAdapter implements AsyncDatabaseAdapter { } }); - EmitEvents(keys, this.#listener.get('updated')); + EmitEvents(keys, this._listener.get('updated')); - transact(this.#doc, () => { + transact(this._doc, () => { for (const [key, action] of keys) { if (action === 'delete') { - this.#block_updated.delete(key); + this._blockUpdated.delete(key); } else { - this.#block_updated.set(key, now); + this._blockUpdated.set(key, now); } } }); @@ -315,7 +315,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter { } getUserId(): string { - return this.#provider.userId; + return this._provider.userId; } inspector() { @@ -333,7 +333,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter { return { save: () => { - const binary = encodeStateAsUpdate(this.#doc); + const binary = encodeStateAsUpdate(this._doc); saveAs( new Blob([binary]), `affine_workspace_${new Date().toDateString()}.apk` @@ -353,7 +353,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter { }); const [file] = (await fromEvent(handles)) as File[]; const binary = await file.arrayBuffer(); - await this.#provider.idb.clearData(); + await this._provider.idb.clearData(); const doc = new Doc({ autoLoad: true, shouldLoad: true }); let updated = 0; let isUpdated = false; @@ -374,21 +374,21 @@ export class YjsAdapter implements AsyncDatabaseAdapter { }; check(); }); - await new IndexeddbPersistence(this.#provider.idb.name, doc) + await new IndexeddbPersistence(this._provider.idb.name, doc) .whenSynced; applyUpdate(doc, new Uint8Array(binary)); await update_check; console.log('load success'); }, - parse: () => this.#doc.toJSON(), + parse: () => this._doc.toJSON(), // eslint-disable-next-line @typescript-eslint/naming-convention parse_page: (page_id: string) => { - const blocks = this.#blocks.toJSON(); + const blocks = this._blocks.toJSON(); return resolve_block(blocks, page_id); }, // eslint-disable-next-line @typescript-eslint/naming-convention parse_pages: (resolve = false) => { - const blocks = this.#blocks.toJSON(); + const blocks = this._blocks.toJSON(); return Object.fromEntries( Object.entries(blocks) .filter(([, block]) => block.flavor === 'page') @@ -402,21 +402,21 @@ export class YjsAdapter implements AsyncDatabaseAdapter { ); }, clear: () => { - this.#blocks.clear(); - this.#block_updated.clear(); - this.#gatekeeper.clear(); - this.#doc.getMap('blocks').clear(); - this.#doc.getMap('gatekeeper').clear(); + this._blocks.clear(); + this._blockUpdated.clear(); + this._gatekeeper.clear(); + this._doc.getMap('blocks').clear(); + this._doc.getMap('gatekeeper').clear(); }, // eslint-disable-next-line @typescript-eslint/naming-convention clear_old: () => { - this.#doc.getMap('block_updated').clear(); - this.#doc.getMap('blocks').clear(); - this.#doc.getMap('common').clear(); - this.#doc.getMap('creators').clear(); + this._doc.getMap('block_updated').clear(); + this._doc.getMap('blocks').clear(); + this._doc.getMap('common').clear(); + this._doc.getMap('creators').clear(); }, snapshot: () => { - return snapshot(this.#doc); + return snapshot(this._doc); }, }; } @@ -459,15 +459,15 @@ export class YjsAdapter implements AsyncDatabaseAdapter { } private get_updated(id: string) { - return this.#block_updated.get(id); + return this._blockUpdated.get(id); } private get_creator(id: string) { - return this.#gatekeeper.getCreator(id); + return this._gatekeeper.getCreator(id); } private get_block_sync(id: string): YjsBlockInstance | undefined { - const cached = this.#block_caches.get(id); + const cached = this._blockCaches.get(id); if (cached) { // Synchronous read cannot read binary if (cached.type === BlockTypes.block) { @@ -476,7 +476,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter { return undefined; } - const block = this.#blocks.get(id); + const block = this._blocks.get(id); // Synchronous read cannot read binary if (block && block.get('type') === BlockTypes.block) { @@ -496,9 +496,9 @@ export class YjsAdapter implements AsyncDatabaseAdapter { async getBlock(id: string): Promise { const block_instance = this.get_block_sync(id); if (block_instance) return block_instance; - const block = this.#blocks.get(id); + const block = this._blocks.get(id); if (block && block.get('type') === BlockTypes.binary) { - const binary = await this.#binaries.get( + const binary = await this._binaries.get( block.get('hash') as string ); if (binary) { @@ -520,7 +520,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter { flavor: BlockItem['flavor'] ): Promise { const keys: string[] = []; - this.#blocks.forEach((doc, key) => { + this._blocks.forEach((doc, key) => { if (doc.get('flavor') === flavor) { keys.push(key); } @@ -533,7 +533,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter { type: BlockItem['type'] ): Promise { const keys: string[] = []; - this.#blocks.forEach((doc, key) => { + this._blocks.forEach((doc, key) => { if (doc.get('type') === type) { keys.push(key); } @@ -547,8 +547,8 @@ export class YjsAdapter implements AsyncDatabaseAdapter { item: BlockItem & { hash?: string } ): Promise { return new Promise((resolve, reject) => { - const block = this.#blocks.get(key) || new YMap(); - transact(this.#doc, () => { + const block = this._blocks.get(key) || new YMap(); + transact(this._doc, () => { // Insert only if the block doesn't exist yet // Other modification operations are done in the block instance let uploaded: Promise | undefined; @@ -568,8 +568,8 @@ export class YjsAdapter implements AsyncDatabaseAdapter { } else if (item.type === BlockTypes.binary && item.hash) { if (content instanceof YArray) { block.set('hash', item.hash); - if (!this.#binaries.has(item.hash)) { - uploaded = this.#binaries.set( + if (!this._binaries.has(item.hash)) { + uploaded = this._binaries.set( item.hash, content ); @@ -583,18 +583,18 @@ export class YjsAdapter implements AsyncDatabaseAdapter { throw new Error('invalid block type: ' + item.type); } - this.#blocks.set(key, block); + this._blocks.set(key, block); } if (item.flavor === 'page') { - this.#awareness.setLocalStateField('editing', key); - this.#awareness.setLocalStateField('updated', Date.now()); + this._awareness.setLocalStateField('editing', key); + this._awareness.setLocalStateField('updated', Date.now()); } // References do not add delete restrictions if (item.flavor === 'reference') { - this.#gatekeeper.setCommon(key); + this._gatekeeper.setCommon(key); } else { - this.#gatekeeper.setCreator(key); + this._gatekeeper.setCreator(key); } if (uploaded) { @@ -613,15 +613,15 @@ export class YjsAdapter implements AsyncDatabaseAdapter { async checkBlocks(keys: string[]): Promise { return ( - keys.filter(key => !!this.#blocks.get(key)).length === keys.length + keys.filter(key => !!this._blocks.get(key)).length === keys.length ); } async deleteBlocks(keys: string[]): Promise { - const [success, fail] = this.#gatekeeper.checkDeleteLists(keys); - transact(this.#doc, () => { + const [success, fail] = this._gatekeeper.checkDeleteLists(keys); + transact(this._doc, () => { for (const key of success) { - this.#blocks.delete(key); + this._blocks.delete(key); } }); return fail; @@ -631,7 +631,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter { key: 'editing' | 'updated' | 'connectivity', listener: BlockListener ): void { - this.#listener.set(key, listener); + this._listener.set(key, listener); } suspend(suspend: boolean) { @@ -639,6 +639,6 @@ export class YjsAdapter implements AsyncDatabaseAdapter { } public history(): HistoryManager { - return this.#history; + return this._history; } } diff --git a/libs/datasource/jwt/src/adapter/yjs/operation.ts b/libs/datasource/jwt/src/adapter/yjs/operation.ts index 8cb825ad3b..e4b2da50dd 100644 --- a/libs/datasource/jwt/src/adapter/yjs/operation.ts +++ b/libs/datasource/jwt/src/adapter/yjs/operation.ts @@ -52,18 +52,18 @@ function auto_set(root: ContentOperation, key: string, data: BaseTypes): void { } export class YjsContentOperation implements ContentOperation { - readonly #content: YAbstractType; + readonly _content: YAbstractType; constructor(content: YAbstractType) { - this.#content = content; + this._content = content; } get length(): number { - if (this.#content instanceof YMap) { - return this.#content.size; + if (this._content instanceof YMap) { + return this._content.size; } - if (this.#content instanceof YArray || this.#content instanceof YText) { - return this.#content.length; + if (this._content instanceof YArray || this._content instanceof YText) { + return this._content.length; } return 0; } @@ -83,8 +83,8 @@ export class YjsContentOperation implements ContentOperation { } asText(): YjsTextOperation | undefined { - if (this.#content instanceof YText) { - return new YjsTextOperation(this.#content); + if (this._content instanceof YText) { + return new YjsTextOperation(this._content); } return undefined; } @@ -92,8 +92,8 @@ export class YjsContentOperation implements ContentOperation { asArray(): | YjsArrayOperation | undefined { - if (this.#content instanceof YArray) { - return new YjsArrayOperation(this.#content); + if (this._content instanceof YArray) { + return new YjsArrayOperation(this._content); } return undefined; } @@ -101,8 +101,8 @@ export class YjsContentOperation implements ContentOperation { asMap(): | YjsMapOperation | undefined { - if (this.#content instanceof YMap) { - return new YjsMapOperation(this.#content); + if (this._content instanceof YMap) { + return new YjsMapOperation(this._content); } return undefined; } @@ -184,24 +184,24 @@ export class YjsContentOperation implements ContentOperation { } [INTO_INNER](): YAbstractType | undefined { - if (this.#content instanceof YAbstractType) { - return this.#content; + if (this._content instanceof YAbstractType) { + return this._content; } return undefined; } // eslint-disable-next-line @typescript-eslint/naming-convention private toJSON() { - return this.#content.toJSON(); + return this._content.toJSON(); } } class YjsTextOperation extends YjsContentOperation implements TextOperation { - readonly #content: YText; + readonly _textContent: YText; constructor(content: YText) { super(content); - this.#content = content; + this._textContent = content; } insert( @@ -209,7 +209,7 @@ class YjsTextOperation extends YjsContentOperation implements TextOperation { content: string, format?: Record ): void { - this.#content.insert(index, content, format); + this._textContent.insert(index, content, format); } format( @@ -217,23 +217,23 @@ class YjsTextOperation extends YjsContentOperation implements TextOperation { length: number, format: Record ): void { - this.#content.format(index, length, format); + this._textContent.format(index, length, format); } delete(index: number, length: number): void { - this.#content.delete(index, length); + this._textContent.delete(index, length); } setAttribute(name: string, value: BaseTypes) { - this.#content.setAttribute(name, value); + this._textContent.setAttribute(name, value); } getAttribute(name: string): T | undefined { - return this.#content.getAttribute(name); + return this._textContent.getAttribute(name); } override toString(): TextToken[] { - return this.#content.toDelta(); + return this._textContent.toDelta(); } } @@ -241,67 +241,69 @@ class YjsArrayOperation extends YjsContentOperation implements ArrayOperation { - readonly #content: YArray; - readonly #listeners: Map; + readonly _arrayContent: YArray; + readonly _listeners: Map; constructor(content: YArray) { super(content); - this.#content = content; - this.#listeners = new Map(); + this._arrayContent = content; + this._listeners = new Map(); - this.#content.observe(event => - ChildrenListenerHandler(this.#listeners, event) + this._arrayContent.observe(event => + ChildrenListenerHandler(this._listeners, event) ); } on(name: string, listener: BlockListener) { - this.#listeners.set(name, listener); + this._listeners.set(name, listener); } off(name: string) { - this.#listeners.delete(name); + this._listeners.delete(name); } insert(index: number, content: Array>): void { - this.#content.insert( + this._arrayContent.insert( index, content.map(v => this.into_inner(v)) ); } delete(index: number, length: number): void { - this.#content.delete(index, length); + this._arrayContent.delete(index, length); } push(content: Array>): void { - this.#content.push(content.map(v => this.into_inner(v))); + this._arrayContent.push(content.map(v => this.into_inner(v))); } unshift(content: Array>): void { - this.#content.unshift(content.map(v => this.into_inner(v))); + this._arrayContent.unshift(content.map(v => this.into_inner(v))); } get(index: number): Operable | undefined { - const content = this.#content.get(index); + const content = this._arrayContent.get(index); if (content) return this.to_operable(content); return undefined; } private get_internal(index: number): T { - return this.#content.get(index); + return this._arrayContent.get(index); } slice(start?: number, end?: number): Operable[] { - return this.#content.slice(start, end).map(v => this.to_operable(v)); + return this._arrayContent + .slice(start, end) + .map(v => this.to_operable(v)); } map(callback: (value: T, index: number) => R): R[] { - return this.#content.map((value, index) => callback(value, index)); + return this._arrayContent.map((value, index) => callback(value, index)); } // Traverse, if callback returns false, stop traversing forEach(callback: (value: T, index: number) => boolean) { - for (let i = 0; i < this.#content.length; i++) { + for (let i = 0; i < this._arrayContent.length; i++) { const ret = callback(this.get_internal(i), i); if (ret === false) { break; @@ -342,47 +344,47 @@ class YjsMapOperation extends YjsContentOperation implements MapOperation { - readonly #content: YMap; - readonly #listeners: Map; + readonly _mapContent: YMap; + readonly _listeners: Map; constructor(content: YMap) { super(content); - this.#content = content; - this.#listeners = new Map(); + this._mapContent = content; + this._listeners = new Map(); content?.observeDeep(events => - ContentListenerHandler(this.#listeners, events) + ContentListenerHandler(this._listeners, events) ); } on(name: string, listener: BlockListener) { - this.#listeners.set(name, listener); + this._listeners.set(name, listener); } off(name: string) { - this.#listeners.delete(name); + this._listeners.delete(name); } set(key: string, value: Operable): void { if (value instanceof YjsContentOperation) { const content = value[INTO_INNER](); - if (content) this.#content.set(key, content as unknown as T); + if (content) this._mapContent.set(key, content as unknown as T); } else { - this.#content.set(key, value as T); + this._mapContent.set(key, value as T); } } get(key: string): Operable | undefined { - const content = this.#content.get(key); + const content = this._mapContent.get(key); if (content) return this.to_operable(content); return undefined; } delete(key: string): void { - this.#content.delete(key); + this._mapContent.delete(key); } has(key: string): boolean { - return this.#content.has(key); + return this._mapContent.has(key); } } diff --git a/libs/datasource/jwt/src/block/abstract.ts b/libs/datasource/jwt/src/block/abstract.ts index 30da2ffc57..9d675ee803 100644 --- a/libs/datasource/jwt/src/block/abstract.ts +++ b/libs/datasource/jwt/src/block/abstract.ts @@ -26,47 +26,47 @@ export class AbstractBlock< B extends BlockInstance, C extends ContentOperation > { - readonly #id: string; + readonly _id: string; readonly #block: BlockInstance; - readonly #history: HistoryManager; - readonly #root?: AbstractBlock; - readonly #parent_listener: Map; + readonly _history: HistoryManager; + readonly _root?: AbstractBlock; + readonly _parentListener: Map; - #parent?: AbstractBlock; + _parent?: AbstractBlock; constructor( block: B, root?: AbstractBlock, parent?: AbstractBlock ) { - this.#id = block.id; + this._id = block.id; this.#block = block; - this.#history = this.#block.scopedHistory([this.#id]); + this._history = this.#block.scopedHistory([this._id]); - this.#root = root; - this.#parent_listener = new Map(); - this.#parent = parent; - JWT_DEV && logger_debug(`init: exists ${this.#id}`); + this._root = root; + this._parentListener = new Map(); + this._parent = parent; + JWT_DEV && logger_debug(`init: exists ${this._id}`); } public get root() { - return this.#root; + return this._root; } protected get parent_node() { - return this.#parent; + return this._parent; } protected _getParentPage(warning = true): string | undefined { if (this.flavor === 'page') { return this.#block.id; - } else if (!this.#parent) { + } else if (!this._parent) { if (warning && this.flavor !== 'workspace') { console.warn('parent not found'); } return undefined; } else { - return this.#parent.parent_page; + return this._parent.parent_page; } } @@ -80,7 +80,7 @@ export class AbstractBlock< callback: BlockListener ) { if (event === 'parent') { - this.#parent_listener.set(name, callback); + this._parentListener.set(name, callback); } else { this.#block.on(event, name, callback); } @@ -88,7 +88,7 @@ export class AbstractBlock< public off(event: 'content' | 'children' | 'parent', name: string) { if (event === 'parent') { - this.#parent_listener.delete(name); + this._parentListener.delete(name); } else { this.#block.off(event, name); } @@ -117,7 +117,7 @@ export class AbstractBlock< return this.#block.content.asMap() as MapOperation; } throw new Error( - `this block not a structured block: ${this.#id}, ${ + `this block not a structured block: ${this._id}, ${ this.#block.type }` ); @@ -181,9 +181,9 @@ export class AbstractBlock< } [SET_PARENT](parent: AbstractBlock) { - this.#parent = parent; + this._parent = parent; const states: Map = new Map([[parent.id, 'update']]); - for (const listener of this.#parent_listener.values()) { + for (const listener of this._parentListener.values()) { listener(states); } } @@ -196,7 +196,7 @@ export class AbstractBlock< const updated = this.last_updated_date; return [ - `id:${this.#id}`, + `id:${this._id}`, `type:${this.type}`, `type:${this.flavor}`, this.flavor === BlockFlavors.page && `type:doc`, // normal documentation @@ -211,7 +211,7 @@ export class AbstractBlock< * current document instance id */ public get id(): string { - return this.#id; + return this._id; } /** @@ -249,7 +249,7 @@ export class AbstractBlock< ) { JWT_DEV && logger(`insertChildren: start`); - if (block.id === this.#id) return; // avoid self-reference + if (block.id === this._id) return; // avoid self-reference if ( this.type !== BlockTypes.block || // binary cannot insert subblocks (block.type !== BlockTypes.block && @@ -367,6 +367,6 @@ export class AbstractBlock< * TODO: scoped history */ public get history(): HistoryManager { - return this.#history; + return this._history; } } diff --git a/libs/datasource/jwt/src/block/base.ts b/libs/datasource/jwt/src/block/base.ts index 23a251fe24..cf79c08ea7 100644 --- a/libs/datasource/jwt/src/block/base.ts +++ b/libs/datasource/jwt/src/block/base.ts @@ -51,24 +51,24 @@ export class BaseBlock< B extends BlockInstance, C extends ContentOperation > extends AbstractBlock { - readonly #exporters?: Exporters; - readonly #content_exporters_getter: () => Map< + readonly _exporters?: Exporters; + readonly _contentExportersGetter: () => Map< string, ReadableContentExporter >; - readonly #metadata_exporters_getter: () => Map< + readonly _metadataExportersGetter: () => Map< string, ReadableContentExporter< Array<[string, number | string | string[]]>, any > >; - readonly #tag_exporters_getter: () => Map< + readonly _tagExportersGetter: () => Map< string, ReadableContentExporter >; - #validators: Map = new Map(); + validators: Map = new Map(); constructor( block: B, @@ -78,12 +78,11 @@ export class BaseBlock< ) { super(block, root, parent); - this.#exporters = exporters; - this.#content_exporters_getter = () => - new Map(exporters?.content(block)); - this.#metadata_exporters_getter = () => + this._exporters = exporters; + this._contentExportersGetter = () => new Map(exporters?.content(block)); + this._metadataExportersGetter = () => new Map(exporters?.metadata(block)); - this.#tag_exporters_getter = () => new Map(exporters?.tag(block)); + this._tagExportersGetter = () => new Map(exporters?.tag(block)); } get parent() { @@ -158,14 +157,14 @@ export class BaseBlock< setValidator(key: string, validator?: Validator) { if (validator) { - this.#validators.set(key, validator); + this.validators.set(key, validator); } else { - this.#validators.delete(key); + this.validators.delete(key); } } private validate(key: string, value: unknown): boolean { - const validate = this.#validators.get(key); + const validate = this.validators.get(key); if (validate) { return validate(value) === false ? false : true; } @@ -185,13 +184,13 @@ export class BaseBlock< */ private get_children_instance(blockId?: string): BaseBlock[] { return this.get_children(blockId).map( - block => new BaseBlock(block, this.root, this, this.#exporters) + block => new BaseBlock(block, this.root, this, this._exporters) ); } private get_indexable_metadata() { const metadata: Record = {}; - for (const [name, exporter] of this.#metadata_exporters_getter()) { + for (const [name, exporter] of this._metadataExportersGetter()) { try { for (const [key, val] of exporter(this.getContent())) { metadata[key] = val; @@ -226,7 +225,7 @@ export class BaseBlock< private get_indexable_content(): string | undefined { const contents = []; - for (const [name, exporter] of this.#content_exporters_getter()) { + for (const [name, exporter] of this._contentExportersGetter()) { try { const content = exporter(this.getContent()); if (content) contents.push(content); @@ -246,7 +245,7 @@ export class BaseBlock< private get_indexable_tags(): string[] { const tags: string[] = []; - for (const [name, exporter] of this.#tag_exporters_getter()) { + for (const [name, exporter] of this._tagExportersGetter()) { try { tags.push(...exporter(this.getContent())); } catch (err) { diff --git a/libs/datasource/jwt/src/block/indexer.ts b/libs/datasource/jwt/src/block/indexer.ts index cc887eb6e9..4e1b618f19 100644 --- a/libs/datasource/jwt/src/block/indexer.ts +++ b/libs/datasource/jwt/src/block/indexer.ts @@ -94,18 +94,18 @@ export class BlockIndexer< B extends BlockInstance, C extends ContentOperation > { - readonly #adapter: A; - readonly #idb: BlockIdbInstance; + readonly _adapter: A; + readonly _idb: BlockIdbInstance; - readonly #block_indexer: DocumentIndexer; - readonly #block_metadata: LRUCache; - readonly #event_bus: BlockEventBus; + readonly _blockIndexer: DocumentIndexer; + readonly _blockMetadata: LRUCache; + readonly _eventBus: BlockEventBus; - readonly #block_builder: ( + readonly _blockBuilder: ( block: BlockInstance ) => Promise>; - readonly #delay_index: { documents: Map> }; + readonly _delayIndex: { documents: Map> }; constructor( adapter: A, @@ -113,10 +113,10 @@ export class BlockIndexer< block_builder: (block: BlockInstance) => Promise>, event_bus: BlockEventBus ) { - this.#adapter = adapter; - this.#idb = initIndexIdb(workspace); + this._adapter = adapter; + this._idb = initIndexIdb(workspace); - this.#block_indexer = new DocumentIndexer({ + this._blockIndexer = new DocumentIndexer({ document: { id: 'id', index: ['content', 'reference'], @@ -126,23 +126,23 @@ export class BlockIndexer< tokenize: 'forward', context: true, }); - this.#block_metadata = new LRUCache({ + this._blockMetadata = new LRUCache({ max: 10240, ttl: 1000 * 60 * 30, }); - this.#block_builder = block_builder; - this.#event_bus = event_bus; + this._blockBuilder = block_builder; + this._eventBus = event_bus; - this.#delay_index = { documents: new Map() }; + this._delayIndex = { documents: new Map() }; - this.#event_bus + this._eventBus .topic('reindex') .on('reindex', this.content_reindex.bind(this), { debounce: { wait: 1000, maxWait: 1000 * 10 }, }); - this.#event_bus + this._eventBus .topic('save_index') .on('save_index', this.save_index.bind(this), { debounce: { wait: 1000 * 10, maxWait: 1000 * 20 }, @@ -152,8 +152,8 @@ export class BlockIndexer< private async content_reindex() { const paddings: Record = {}; - this.#delay_index.documents = produce( - this.#delay_index.documents, + this._delayIndex.documents = produce( + this._delayIndex.documents, draft => { for (const [k, block] of draft) { paddings[k] = { @@ -166,11 +166,11 @@ export class BlockIndexer< ); for (const [key, { index, query }] of Object.entries(paddings)) { if (index.content) { - await this.#block_indexer.addAsync(key, index); - this.#block_metadata.set(key, query); + await this._blockIndexer.addAsync(key, index); + this._blockMetadata.set(key, query); } } - this.#event_bus.topic('save_index').emit(); + this._eventBus.topic('save_index').emit(); } private async refresh_index(block: BaseBlock) { @@ -185,14 +185,14 @@ export class BlockIndexer< BlockFlavors.reference, ]; if (filter.includes(block.flavor)) { - this.#delay_index.documents = produce( - this.#delay_index.documents, + this._delayIndex.documents = produce( + this._delayIndex.documents, draft => { draft.set(block.id, block); } ); - this.#event_bus.topic('reindex').emit(); + this._eventBus.topic('reindex').emit(); return true; } logger_debug(`skip index ${block.flavor}: ${block.id}`); @@ -202,19 +202,19 @@ export class BlockIndexer< async refreshIndex(id: string, state: ChangedState) { JWT_DEV && logger(`refreshArticleIndex: ${id}`); if (state === 'delete') { - this.#delay_index.documents = produce( - this.#delay_index.documents, + this._delayIndex.documents = produce( + this._delayIndex.documents, draft => { - this.#block_indexer.remove(id); - this.#block_metadata.delete(id); + this._blockIndexer.remove(id); + this._blockMetadata.delete(id); draft.delete(id); } ); return; } - const block = await this.#adapter.getBlock(id); + const block = await this._adapter.getBlock(id); if (block?.id === id) { - if (await this.refresh_index(await this.#block_builder(block))) { + if (await this.refresh_index(await this._blockBuilder(block))) { JWT_DEV && logger( state @@ -230,43 +230,43 @@ export class BlockIndexer< } async loadIndex() { - for (const key of await this.#idb.index.keys()) { - const content = await this.#idb.index.get(key); + for (const key of await this._idb.index.keys()) { + const content = await this._idb.index.get(key); if (content) { const decoded = strFromU8(inflateSync(new Uint8Array(content))); try { - await this.#block_indexer.import(key, decoded as any); + await this._blockIndexer.import(key, decoded as any); } catch (e) { console.error(`Failed to load index ${key}`, e); } } } - for (const key of await this.#idb.metadata.keys()) { - const content = await this.#idb.metadata.get(key); + for (const key of await this._idb.metadata.keys()) { + const content = await this._idb.metadata.get(key); if (content) { const decoded = strFromU8(inflateSync(new Uint8Array(content))); try { - await this.#block_indexer.import(key, JSON.parse(decoded)); + await this._blockIndexer.import(key, JSON.parse(decoded)); } catch (e) { console.error(`Failed to load index ${key}`, e); } } } - return Array.from(this.#block_metadata.keys()); + return Array.from(this._blockMetadata.keys()); } private async save_index() { - const idb = this.#idb; + const idb = this._idb; await idb.index .keys() .then(keys => Promise.all(keys.map(key => idb.index.delete(key)))); - await this.#block_indexer.export((key, data) => { + await this._blockIndexer.export((key, data) => { return idb.index.set( String(key), deflateSync(strToU8(data as any)) ); }); - const metadata = this.#block_metadata; + const metadata = this._blockMetadata; await idb.metadata .keys() .then(keys => @@ -286,7 +286,7 @@ export class BlockIndexer< public async inspectIndex() { const index: Record = {}; - await this.#block_indexer.export((key, data) => { + await this._blockIndexer.export((key, data) => { index[key] = data; }); } @@ -296,13 +296,13 @@ export class BlockIndexer< | string | Partial> ) { - return this.#block_indexer.search(part_of_title_or_content as string); + return this._blockIndexer.search(part_of_title_or_content as string); } public query(query: QueryIndexMetadata) { const matches: string[] = []; const filter = sift(query); - this.#block_metadata.forEach((value, key) => { + this._blockMetadata.forEach((value, key) => { if (filter(value)) matches.push(key); }); return matches; @@ -310,7 +310,7 @@ export class BlockIndexer< public getMetadata(ids: string[]): Array { return ids - .filter(id => this.#block_metadata.has(id)) - .map(id => ({ ...this.#block_metadata.get(id)!, id })); + .filter(id => this._blockMetadata.has(id)) + .map(id => ({ ...this._blockMetadata.get(id)!, id })); } } diff --git a/libs/datasource/jwt/src/index.ts b/libs/datasource/jwt/src/index.ts index 16bbf5a8c4..bd01d93dcb 100644 --- a/libs/datasource/jwt/src/index.ts +++ b/libs/datasource/jwt/src/index.ts @@ -69,14 +69,14 @@ export class BlockClient< B extends BlockInstance, C extends ContentOperation > { - readonly #adapter: A; - readonly #workspace: string; + readonly _adapter: A; + readonly _workspace: string; // Maximum cache Block 8192, ttl 30 minutes - readonly #block_caches: LRUCache>; - readonly #block_indexer: BlockIndexer; + readonly _blockCaches: LRUCache>; + readonly _blockIndexer: BlockIndexer; - readonly #exporters: { + readonly _exporters: { readonly content: BlockExporters; readonly metadata: BlockExporters< Array<[string, number | string | string[]]> @@ -84,84 +84,83 @@ export class BlockClient< readonly tag: BlockExporters; }; - readonly #event_bus: BlockEventBus; + readonly _eventBus: BlockEventBus; - readonly #parent_mapping: Map; - readonly #page_mapping: Map; + readonly _parentMapping: Map; + readonly _pageMapping: Map; - readonly #root: { node?: BaseBlock }; + readonly _root: { node?: BaseBlock }; private constructor( adapter: A, workspace: string, options?: BlockClientOptions ) { - this.#adapter = adapter; - this.#workspace = workspace; + this._adapter = adapter; + this._workspace = workspace; - this.#block_caches = new LRUCache({ max: 8192, ttl: 1000 * 60 * 30 }); + this._blockCaches = new LRUCache({ max: 8192, ttl: 1000 * 60 * 30 }); - this.#exporters = { + this._exporters = { content: options?.content || new Map(), metadata: options?.metadata || new Map(), tag: options?.tagger || new Map(), }; - this.#event_bus = new BlockEventBus(); + this._eventBus = new BlockEventBus(); - this.#block_indexer = new BlockIndexer( - this.#adapter, - this.#workspace, + this._blockIndexer = new BlockIndexer( + this._adapter, + this._workspace, this.block_builder.bind(this), - this.#event_bus.topic('indexer') + this._eventBus.topic('indexer') ); - this.#parent_mapping = new Map(); - this.#page_mapping = new Map(); - this.#adapter.on('editing', (states: ChangedStates) => - this.#event_bus.topic('editing').emit(states) + this._parentMapping = new Map(); + this._pageMapping = new Map(); + this._adapter.on('editing', (states: ChangedStates) => + this._eventBus.topic('editing').emit(states) ); - this.#adapter.on('updated', (states: ChangedStates) => - this.#event_bus.topic('updated').emit(states) + this._adapter.on('updated', (states: ChangedStates) => + this._eventBus.topic('updated').emit(states) ); - this.#adapter.on( + this._adapter.on( 'connectivity', (states: ChangedStates) => - this.#event_bus.topic('connectivity').emit(states) + this._eventBus.topic('connectivity').emit(states) ); - this.#event_bus + this._eventBus .topic('rebuild_index') .on('rebuild_index', this.rebuild_index.bind(this), { debounce: { wait: 1000, maxWait: 1000 }, }); - this.#root = {}; + this._root = {}; } public addBlockListener(tag: string, listener: BlockListener) { - const bus = this.#event_bus.topic('updated'); + const bus = this._eventBus.topic('updated'); if (tag !== 'index' || !bus.has(tag)) bus.on(tag, listener); else console.error(`block listener ${tag} is reserved or exists`); } public removeBlockListener(tag: string) { - this.#event_bus.topic('updated').off(tag); + this._eventBus.topic('updated').off(tag); } public addEditingListener( tag: string, listener: BlockListener> ) { - const bus = - this.#event_bus.topic>>('editing'); + const bus = this._eventBus.topic>>('editing'); if (tag !== 'index' || !bus.has(tag)) bus.on(tag, listener); else console.error(`editing listener ${tag} is reserved or exists`); } public removeEditingListener(tag: string) { - this.#event_bus.topic('editing').off(tag); + this._eventBus.topic('editing').off(tag); } public addConnectivityListener( @@ -169,31 +168,31 @@ export class BlockClient< listener: BlockListener ) { const bus = - this.#event_bus.topic>('connectivity'); + this._eventBus.topic>('connectivity'); if (tag !== 'index' || !bus.has(tag)) bus.on(tag, listener); else console.error(`connectivity listener ${tag} is reserved or exists`); } public removeConnectivityListener(tag: string) { - this.#event_bus.topic('connectivity').off(tag); + this._eventBus.topic('connectivity').off(tag); } private inspector() { return { - ...this.#adapter.inspector(), - indexed: () => this.#block_indexer.inspectIndex(), + ...this._adapter.inspector(), + indexed: () => this._blockIndexer.inspectIndex(), }; } private async rebuild_index(exists_ids?: string[]) { JWT_DEV && logger(`rebuild index`); - const blocks = await this.#adapter.getBlockByType(BlockTypes.block); + const blocks = await this._adapter.getBlockByType(BlockTypes.block); const excluded = exists_ids || []; await Promise.all( blocks .filter(id => !excluded.includes(id)) - .map(id => this.#block_indexer.refreshIndex(id, 'add')) + .map(id => this._blockIndexer.refreshIndex(id, 'add')) ); } @@ -201,13 +200,13 @@ export class BlockClient< JWT_DEV && logger(`buildIndex: start`); // Skip the block index that exists in the metadata, assuming that the index of the block existing in the metadata is the latest, and modify this part if there is a problem // Although there may be cases where the index is refreshed but the metadata is not refreshed, re-indexing will be automatically triggered after the block is changed - const exists_ids = await this.#block_indexer.loadIndex(); + const exists_ids = await this._blockIndexer.loadIndex(); await this.rebuild_index(exists_ids); this.addBlockListener('index', async states => { await Promise.allSettled( Array.from(states.entries()).map(([id, state]) => { - if (state === 'delete') this.#block_caches.delete(id); - return this.#block_indexer.refreshIndex(id, state); + if (state === 'delete') this._blockCaches.delete(id); + return this._blockIndexer.refreshIndex(id, state); }) ); }); @@ -223,10 +222,10 @@ export class BlockClient< ): Promise>> { JWT_DEV && logger(`getByType: ${block_type}`); const ids = [ - ...this.#block_indexer.query({ + ...this._blockIndexer.query({ type: BlockTypes[block_type as BlockTypeKeys], }), - ...this.#block_indexer.query({ + ...this._blockIndexer.query({ flavor: BlockFlavors[block_type as BlockFlavorKeys], }), ]; @@ -256,7 +255,7 @@ export class BlockClient< | string | Partial> ) { - return this.#block_indexer.search(part_of_title_or_content); + return this._blockIndexer.search(part_of_title_or_content); } /** @@ -278,7 +277,7 @@ export class BlockClient< const promised_pages = await Promise.all( this.search(part_of_title_or_content).flatMap(({ result }) => result.map(async id => { - const page = this.#page_mapping.get(id as string); + const page = this._pageMapping.get(id as string); if (page) return page; const block = await this.get(id as BlockTypeKeys); return this.set_page(block); @@ -289,9 +288,9 @@ export class BlockClient< ...new Set(promised_pages.filter((v): v is string => !!v)), ]; return Promise.all( - this.#block_indexer.getMetadata(pages).map(async page => ({ + this._blockIndexer.getMetadata(pages).map(async page => ({ content: this.get_decoded_content( - await this.#adapter.getBlock(page.id) + await this._adapter.getBlock(page.id) ), ...page, })) @@ -303,7 +302,7 @@ export class BlockClient< * @returns array of search results */ public query(query: QueryIndexMetadata): string[] { - return this.#block_indexer.query(query); + return this._blockIndexer.query(query); } /** @@ -330,12 +329,12 @@ export class BlockClient< * @returns block instance */ public async getWorkspace() { - if (!this.#root.node) { - this.#root.node = await this.get_named_block(this.#workspace, { + if (!this._root.node) { + this._root.node = await this.get_named_block(this._workspace, { workspace: true, }); } - return this.#root.node; + return this._root.node; } /** @@ -348,38 +347,38 @@ export class BlockClient< } private async get_parent(id: string) { - const parents = this.#parent_mapping.get(id); + const parents = this._parentMapping.get(id); if (parents) { const parent_block_id = parents[0]; - if (!this.#block_caches.has(parent_block_id)) { - this.#block_caches.set( + if (!this._blockCaches.has(parent_block_id)) { + this._blockCaches.set( parent_block_id, await this.get(parent_block_id as BlockTypeKeys) ); } - return this.#block_caches.get(parent_block_id); + return this._blockCaches.get(parent_block_id); } return undefined; } private set_parent(parent: string, child: string) { - const parents = this.#parent_mapping.get(child); + const parents = this._parentMapping.get(child); if (parents?.length) { if (!parents.includes(parent)) { console.error('parent already exists', child, parents); - this.#parent_mapping.set(child, [...parents, parent]); + this._parentMapping.set(child, [...parents, parent]); } } else { - this.#parent_mapping.set(child, [parent]); + this._parentMapping.set(child, [parent]); } } private set_page(block: BaseBlock) { - const page = this.#page_mapping.get(block.id); + const page = this._pageMapping.get(block.id); if (page) return page; const parent_page = block.parent_page; if (parent_page) { - this.#page_mapping.set(block.id, parent_page); + this._pageMapping.set(block.id, parent_page); return parent_page; } return undefined; @@ -390,13 +389,13 @@ export class BlockClient< matcher: BlockMatcher, exporter: ReadableContentExporter ) { - this.#exporters.content.set(name, [matcher, exporter]); - this.#event_bus.topic('rebuild_index').emit(); // // rebuild the index every time the content exporter is registered + this._exporters.content.set(name, [matcher, exporter]); + this._eventBus.topic('rebuild_index').emit(); // // rebuild the index every time the content exporter is registered } unregisterContentExporter(name: string) { - this.#exporters.content.delete(name); - this.#event_bus.topic('rebuild_index').emit(); // Rebuild indexes every time content exporter logs out + this._exporters.content.delete(name); + this._eventBus.topic('rebuild_index').emit(); // Rebuild indexes every time content exporter logs out } registerMetadataExporter( @@ -407,13 +406,13 @@ export class BlockClient< T > ) { - this.#exporters.metadata.set(name, [matcher, exporter]); - this.#event_bus.topic('rebuild_index').emit(); // // rebuild the index every time the content exporter is registered + this._exporters.metadata.set(name, [matcher, exporter]); + this._eventBus.topic('rebuild_index').emit(); // // rebuild the index every time the content exporter is registered } unregisterMetadataExporter(name: string) { - this.#exporters.metadata.delete(name); - this.#event_bus.topic('rebuild_index').emit(); // Rebuild indexes every time content exporter logs out + this._exporters.metadata.delete(name); + this._eventBus.topic('rebuild_index').emit(); // Rebuild indexes every time content exporter logs out } registerTagExporter( @@ -421,13 +420,13 @@ export class BlockClient< matcher: BlockMatcher, exporter: ReadableContentExporter ) { - this.#exporters.tag.set(name, [matcher, exporter]); - this.#event_bus.topic('rebuild_index').emit(); // Reindex every tag exporter registration + this._exporters.tag.set(name, [matcher, exporter]); + this._eventBus.topic('rebuild_index').emit(); // Reindex every tag exporter registration } unregisterTagExporter(name: string) { - this.#exporters.tag.delete(name); - this.#event_bus.topic('rebuild_index').emit(); // Reindex every time tag exporter logs out + this._exporters.tag.delete(name); + this._eventBus.topic('rebuild_index').emit(); // Reindex every time tag exporter logs out } private get_exporters( @@ -453,7 +452,7 @@ export class BlockClient< private get_decoded_content(block?: BlockInstance) { if (block) { const [exporter] = this.get_exporters( - this.#exporters.content, + this._exporters.content, block ); if (exporter) { @@ -474,10 +473,10 @@ export class BlockClient< (await this.get_parent(block.id)) || root, { content: block => - this.get_exporters(this.#exporters.content, block), + this.get_exporters(this._exporters.content, block), metadata: block => - this.get_exporters(this.#exporters.metadata, block), - tag: block => this.get_exporters(this.#exporters.tag, block), + this.get_exporters(this._exporters.metadata, block), + tag: block => this.get_exporters(this._exporters.tag, block), } ); } @@ -506,13 +505,13 @@ export class BlockClient< binary, [namedUuid]: is_named_uuid, } = options || {}; - if (block_id_or_type && this.#block_caches.has(block_id_or_type)) { - return this.#block_caches.get(block_id_or_type) as BaseBlock; + if (block_id_or_type && this._blockCaches.has(block_id_or_type)) { + return this._blockCaches.get(block_id_or_type) as BaseBlock; } else { const block = (block_id_or_type && - (await this.#adapter.getBlock(block_id_or_type))) || - (await this.#adapter.createBlock({ + (await this._adapter.getBlock(block_id_or_type))) || + (await this._adapter.createBlock({ uuid: is_named_uuid ? block_id_or_type : undefined, binary, type: @@ -540,7 +539,7 @@ export class BlockClient< this.set_parent(parent, abstract_block.id); this.set_page(abstract_block); }); - this.#block_caches.set(abstract_block.id, abstract_block); + this._blockCaches.set(abstract_block.id, abstract_block); if (root && abstract_block.flavor === BlockFlavors.page) { root.insertChildren(abstract_block); @@ -552,15 +551,15 @@ export class BlockClient< public async getBlockByFlavor( flavor: BlockItem['flavor'] ): Promise { - return await this.#adapter.getBlockByFlavor(flavor); + return await this._adapter.getBlockByFlavor(flavor); } public getUserId(): string { - return this.#adapter.getUserId(); + return this._adapter.getUserId(); } public has(block_ids: string[]): Promise { - return this.#adapter.checkBlocks(block_ids); + return this._adapter.checkBlocks(block_ids); } /** @@ -568,11 +567,11 @@ export class BlockClient< * @param suspend true: suspend monitoring, false: resume monitoring */ suspend(suspend: boolean) { - this.#adapter.suspend(suspend); + this._adapter.suspend(suspend); } public get history(): HistoryManager { - return this.#adapter.history(); + return this._adapter.history(); } public static async init( diff --git a/libs/datasource/jwt/src/utils/event-bus.ts b/libs/datasource/jwt/src/utils/event-bus.ts index e37c41404c..9d128049e7 100644 --- a/libs/datasource/jwt/src/utils/event-bus.ts +++ b/libs/datasource/jwt/src/utils/event-bus.ts @@ -7,14 +7,14 @@ declare const JWT_DEV: boolean; const logger = getLogger('BlockDB:event_bus'); export class BlockEventBus { - readonly #event_bus: EventTarget; - readonly #event_callback_cache: Map; - readonly #scoped_cache: Map>; + readonly _eventBus: EventTarget; + readonly _eventCallbackCache: Map; + readonly _scopedCache: Map>; constructor(event_bus?: EventTarget) { - this.#event_bus = event_bus || new EventTarget(); - this.#event_callback_cache = new Map(); - this.#scoped_cache = new Map(); + this._eventBus = event_bus || new EventTarget(); + this._eventCallbackCache = new Map(); + this._scopedCache = new Map(); } protected on_listener( @@ -23,9 +23,9 @@ export class BlockEventBus { listener: (e: Event) => void ) { const handler_name = `${topic}/${name}`; - if (!this.#event_callback_cache.has(handler_name)) { - this.#event_bus.addEventListener(topic, listener); - this.#event_callback_cache.set(handler_name, listener); + if (!this._eventCallbackCache.has(handler_name)) { + this._eventBus.addEventListener(topic, listener); + this._eventCallbackCache.set(handler_name, listener); } else { JWT_DEV && logger(`event handler ${handler_name} is existing`); } @@ -33,27 +33,27 @@ export class BlockEventBus { protected off_listener(topic: string, name: string) { const handler_name = `${topic}/${name}`; - const listener = this.#event_callback_cache.get(handler_name); + const listener = this._eventCallbackCache.get(handler_name); if (listener) { - this.#event_bus.removeEventListener(topic, listener); - this.#event_callback_cache.delete(handler_name); + this._eventBus.removeEventListener(topic, listener); + this._eventCallbackCache.delete(handler_name); } else { JWT_DEV && logger(`event handler ${handler_name} is not existing`); } } protected has_listener(topic: string, name: string) { - return this.#event_callback_cache.has(`${topic}/${name}`); + return this._eventCallbackCache.has(`${topic}/${name}`); } protected emit_event(topic: string, detail?: T) { - this.#event_bus.dispatchEvent(new CustomEvent(topic, { detail })); + this._eventBus.dispatchEvent(new CustomEvent(topic, { detail })); } topic(topic: string): BlockScopedEventBus { return ( - this.#scoped_cache.get(topic) || - new BlockScopedEventBus(topic, this.#event_bus) + this._scopedCache.get(topic) || + new BlockScopedEventBus(topic, this._eventBus) ); } } @@ -68,11 +68,11 @@ type ListenerOptions = { }; class BlockScopedEventBus extends BlockEventBus { - readonly #topic: string; + readonly _topic: string; constructor(topic: string, event_bus?: EventTarget) { super(event_bus); - this.#topic = topic; + this._topic = topic; } on( @@ -83,25 +83,25 @@ class BlockScopedEventBus extends BlockEventBus { if (options?.debounce) { const { wait, maxWait } = options.debounce; const debounced = debounce(listener, wait, { maxWait }); - this.on_listener(this.#topic, name, e => { + this.on_listener(this._topic, name, e => { debounced((e as CustomEvent)?.detail); }); } else { - this.on_listener(this.#topic, name, e => { + this.on_listener(this._topic, name, e => { listener((e as CustomEvent)?.detail); }); } } off(name: string) { - this.off_listener(this.#topic, name); + this.off_listener(this._topic, name); } has(name: string) { - return this.has_listener(this.#topic, name); + return this.has_listener(this._topic, name); } emit(detail?: T) { - this.emit_event(this.#topic, detail); + this.emit_event(this._topic, detail); } }