Skip to content

Commit

Permalink
Improve LISSFather system
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-migdal committed Feb 27, 2025
1 parent d12e4c4 commit 2928d5c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
4 changes: 4 additions & 0 deletions V3/src/LISSClasses/LISSChild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export default class LISSChild<T extends Record<string,any> = any> extends LISSP
(this.parentElement as any).onAttach(this);
}

get isAttached() {
return this.father == null;
}

protected onAttach(father: LISSFather) {
this.father = father;
}
Expand Down
53 changes: 30 additions & 23 deletions V3/src/LISSClasses/LISSFather.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,58 @@ function observe( target: LISSFather, callback: (records: MutationRecord[]) => v
// extends LISSSignal (?) Properties merger (?)
export default class LISSFather extends LISSUpdate {

protected LISSChildren = new Array<LISSChild>();
protected LISSChildren: LISSChild[]|null = null;

constructor() {
super();

observe(this, (records: MutationRecord[]) => {

this.updateChildrenList();
this.LISSChildren = null;

for(let i = 0; i < records.length; ++i) {
this.processAddedNodes (records[i].addedNodes);
for(let i = 0; i < records.length; ++i)
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 )
this.onAttach( node );
else
(node as any)[WAITING_UPGRADE] = true;
}
}

protected processRemovedNodes(nodes: NodeList) {

for(let j = 0; j < nodes.length; ++j) {
const node = nodes[j]
if( node instanceof LISSChild )
if( node instanceof LISSChild && node.isAttached )
this.onDetach( node );
else
(node as any)[WAITING_UPGRADE] = false;
}
}

protected updateChildrenList() {
this.LISSChildren = [ ...this.children ].filter( (e) => {
return e instanceof LISSChild
})

// wasn't invalidated
if( this.LISSChildren !== null )
return;

const children = this.children;
this.LISSChildren = new Array(children.length);

let offset = 0;
for(let i = 0; i < children.length; ++i) {

const child = children[i];
if( ! (child instanceof LISSChild) )
continue;

if( ! child.isAttached )
this.onAttach( child );

this.LISSChildren[offset++] = child;
}
}

protected override onInit(): void {}

protected override onUpdate(): void {
this.updateChildrenList();
}

protected onDetach(child: LISSChild) {
Expand Down
10 changes: 10 additions & 0 deletions V3/src/LISSClasses/LISSUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export default class LISSUpdate extends LISSBase {
this.#requestID = requestAnimationFrame( () => {
this.#requestID = null;
this.#updateRequested = false;

if( this.#initCalled === false ) {
this.#initCalled = true;
this.onInit();
}
this.onUpdate();
});
}
Expand All @@ -50,6 +55,11 @@ export default class LISSUpdate extends LISSBase {
this.#scheduleUpdate();
}

#initCalled = false;
protected onInit() {

}

protected onUpdate() {

}
Expand Down

0 comments on commit 2928d5c

Please sign in to comment.