From eb2ad57c5a113ac1a128ff5b3816d9b64bdd511c Mon Sep 17 00:00:00 2001 From: DarkSky Date: Mon, 8 Aug 2022 18:39:30 +0800 Subject: [PATCH] feat: block remove event --- libs/datasource/jwt/src/block/abstract.ts | 32 +++++++++++++++++------ 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/libs/datasource/jwt/src/block/abstract.ts b/libs/datasource/jwt/src/block/abstract.ts index 29ee655dc2..0225aa0897 100644 --- a/libs/datasource/jwt/src/block/abstract.ts +++ b/libs/datasource/jwt/src/block/abstract.ts @@ -19,8 +19,8 @@ declare const JWT_DEV: boolean; const logger = getLogger('BlockDB:block'); const logger_debug = getLogger('debug:BlockDB:block'); -const GET_BLOCK = Symbol('GET_BLOCK'); -const SET_PARENT = Symbol('SET_PARENT'); +const _GET_BLOCK = Symbol('GET_BLOCK'); +const _SET_PARENT = Symbol('SET_PARENT'); export class AbstractBlock< B extends BlockInstance, @@ -47,6 +47,13 @@ export class AbstractBlock< this._parentListener = new Map(); this._parent = parent; JWT_DEV && logger_debug(`init: exists ${this._id}`); + if (parent) { + parent.addChildrenListener(this._id, states => { + if (states.get(this._id) === 'delete') { + this._emitParent(parent._id, 'delete'); + } + }); + } } public get root() { @@ -176,18 +183,27 @@ export class AbstractBlock< return this.#block.creator; } - [GET_BLOCK]() { + [_GET_BLOCK]() { return this.#block; } - [SET_PARENT](parent: AbstractBlock) { - this._parent = parent; - const states: Map = new Map([[parent.id, 'update']]); + private _emitParent( + parentId: string, + type: 'update' | 'delete' = 'update' + ) { + const states: Map = new Map([ + [parentId, type], + ]); for (const listener of this._parentListener.values()) { listener(states); } } + [_SET_PARENT](parent: AbstractBlock) { + this._parent = parent; + this._emitParent(parent.id); + } + /** * Get document index tags */ @@ -258,8 +274,8 @@ export class AbstractBlock< throw new Error('insertChildren: binary not allow insert children'); } - this.#block.insertChildren(block[GET_BLOCK](), position); - block[SET_PARENT](this); + this.#block.insertChildren(block[_GET_BLOCK](), position); + block[_SET_PARENT](this); } public hasChildren(id: string): boolean {