From 6ac1f618f022dc12123d17babce6f3c29ff1f8a4 Mon Sep 17 00:00:00 2001 From: DarkSky Date: Wed, 24 Aug 2022 17:37:16 +0800 Subject: [PATCH] feat: cascading events --- libs/datasource/jwt/src/block/abstract.ts | 49 +++++++++++++++++------ libs/datasource/jwt/src/block/indexer.ts | 8 ++-- libs/datasource/jwt/src/index.ts | 2 +- 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/libs/datasource/jwt/src/block/abstract.ts b/libs/datasource/jwt/src/block/abstract.ts index 55249514b3..d2f117c62b 100644 --- a/libs/datasource/jwt/src/block/abstract.ts +++ b/libs/datasource/jwt/src/block/abstract.ts @@ -21,6 +21,7 @@ const logger_debug = getLogger('debug:BlockDB:block'); const _GET_BLOCK = Symbol('GET_BLOCK'); const _SET_PARENT = Symbol('SET_PARENT'); +const _EMIT_EVENT = Symbol('EMIT_EVENT'); export class AbstractBlock< B extends BlockInstance, @@ -30,7 +31,12 @@ export class AbstractBlock< private readonly _block: BlockInstance; private readonly _history: HistoryManager; private readonly _root: AbstractBlock | undefined; - private readonly _parentListener: Map; + private readonly _listeners: { + cascade: Map; + children: Map; + content: Map; + parent: Map; + }; private _parent: AbstractBlock | undefined; private _changeParent?: () => void; @@ -45,7 +51,19 @@ export class AbstractBlock< this._history = this._block.scopedHistory([this._id]); this._root = root; - this._parentListener = new Map(); + this._listeners = { + cascade: new Map(), + children: new Map(), + content: new Map(), + parent: new Map(), + }; + + this._block.on('content', 'internal', states => + this[_EMIT_EVENT]('content', states) + ); + this._block.on('children', 'internal', states => + this[_EMIT_EVENT]('children', states) + ); JWT_DEV && logger_debug(`init: exists ${this._id}`); if (parent) { @@ -83,19 +101,11 @@ export class AbstractBlock< name: string, callback: BlockListener ) { - if (event === 'parent') { - this._parentListener.set(name, callback); - } else { - this._block.on(event, name, callback); - } + this._listeners[event]?.set(name, callback); } public off(event: 'content' | 'children' | 'parent', name: string) { - if (event === 'parent') { - this._parentListener.delete(name); - } else { - this._block.off(event, name); - } + this._listeners[event]?.delete(name); } public addChildrenListener(name: string, listener: BlockListener) { @@ -189,11 +199,24 @@ export class AbstractBlock< const states: Map = new Map([ [parentId, type], ]); - for (const listener of this._parentListener.values()) { + for (const listener of this._listeners.parent.values()) { listener(states); } } + [_EMIT_EVENT]( + event: 'cascade' | 'content' | 'children', + states: Parameters[0] + ) { + const listeners = this._listeners[event]; + if (listeners) { + for (const listener of listeners.values()) { + listener(states); + } + } + this._parent?.[_EMIT_EVENT]('cascade', states); + } + private _refreshParent(parent: AbstractBlock) { this._changeParent?.(); parent.addChildrenListener(this._id, states => { diff --git a/libs/datasource/jwt/src/block/indexer.ts b/libs/datasource/jwt/src/block/indexer.ts index ed57465dbc..cd430ce6d6 100644 --- a/libs/datasource/jwt/src/block/indexer.ts +++ b/libs/datasource/jwt/src/block/indexer.ts @@ -123,8 +123,8 @@ export class BlockIndexer< constructor( adapter: A, workspace: string, - block_builder: (block: BlockInstance) => Promise>, - event_bus: BlockEventBus + blockBuilder: (block: BlockInstance) => Promise>, + eventBus: BlockEventBus ) { this._adapter = adapter; this._idb = initIndexIdb(workspace); @@ -144,8 +144,8 @@ export class BlockIndexer< ttl: 1000 * 60 * 30, }); - this._blockBuilder = block_builder; - this._eventBus = event_bus; + this._blockBuilder = blockBuilder; + this._eventBus = eventBus; this._delayIndex = { documents: new Map() }; diff --git a/libs/datasource/jwt/src/index.ts b/libs/datasource/jwt/src/index.ts index e08bdc1e16..e8f3483e23 100644 --- a/libs/datasource/jwt/src/index.ts +++ b/libs/datasource/jwt/src/index.ts @@ -679,7 +679,7 @@ export type { ReadableContentExporter as BlockContentExporter, } from './block'; export { BlockTypes, BucketBackend as BlockBackend } from './types'; -export type { BlockTypeKeys } from './types'; +export type { BlockFlavorKeys, BlockTypeKeys } from './types'; export { isBlock } from './utils'; export type { ArrayOperation,