From d12e4c4b3eb07486a7ab98b8b58cc192a5e43984 Mon Sep 17 00:00:00 2001 From: denmigda Date: Wed, 26 Feb 2025 19:23:52 +0100 Subject: [PATCH] keep track of children in LISSFather --- V3/src/LISSClasses/LISSFather.ts | 61 +++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/V3/src/LISSClasses/LISSFather.ts b/V3/src/LISSClasses/LISSFather.ts index d275822..467a6b7 100644 --- a/V3/src/LISSClasses/LISSFather.ts +++ b/V3/src/LISSClasses/LISSFather.ts @@ -3,37 +3,58 @@ import LISSUpdate from "./LISSUpdate"; export const WAITING_UPGRADE = Symbol(); -const obsChildren = new MutationObserver( (records: MutationRecord[]) => { - - for(let i = 0; i < records.length; ++i) { - const {addedNodes, removedNodes} = records[i]; - const target: LISSFather = records[i].target as any; - - for(let j = 0; j < addedNodes.length; ++j) { - const node = addedNodes[j] +function observe( target: LISSFather, callback: (records: MutationRecord[]) => void ) { + new MutationObserver(callback).observe(target, {childList: true}); +} + +// extends LISSSignal (?) Properties merger (?) +export default class LISSFather extends LISSUpdate { + + protected LISSChildren = new Array(); + + constructor() { + super(); + + observe(this, (records: MutationRecord[]) => { + + this.updateChildrenList(); + + for(let i = 0; i < records.length; ++i) { + this.processAddedNodes (records[i].addedNodes); + this.processRemovedNodes(records[i].removedNodes); + } + }); + + this.updateChildrenList(); + this.processAddedNodes(this.childNodes); + } + + protected processAddedNodes(nodes: NodeList) { + + for(let j = 0; j < nodes.length; ++j) { + const node = nodes[j] if( node instanceof LISSChild ) - (target as any).onAttach( node ); + this.onAttach( node ); else (node as any)[WAITING_UPGRADE] = true; } + } + + protected processRemovedNodes(nodes: NodeList) { - for(let j = 0; j < removedNodes.length; ++j) { - const node = removedNodes[j] + for(let j = 0; j < nodes.length; ++j) { + const node = nodes[j] if( node instanceof LISSChild ) - (target as any).onDetach( node ); + this.onDetach( node ); else (node as any)[WAITING_UPGRADE] = false; } } -}); - -// extends LISSSignal (?) Properties merger (?) -export default class LISSFather extends LISSUpdate { - - constructor() { - super(); - obsChildren.observe(this, {childList: true}); + protected updateChildrenList() { + this.LISSChildren = [ ...this.children ].filter( (e) => { + return e instanceof LISSChild + }) } protected onDetach(child: LISSChild) {