@ -288,32 +288,22 @@ export class PublicationTree implements AsyncIterable<NDKEvent | null> {
@@ -288,32 +288,22 @@ export class PublicationTree implements AsyncIterable<NDKEvent | null> {
await this . # cursor . tryMoveTo ( this . # bookmark ) ;
}
// Based on Raymond Chen's tree traversal algorithm.
// https://devblogs.microsoft.com/oldnewthing/20200106-00/?p=103300
do {
if ( await this . # cursor . tryMoveToFirstChild ( ) ) {
continue ;
}
if ( await this . # cursor . tryMoveToNextSibling ( ) ) {
continue ;
}
if ( this . # cursor . tryMoveToParent ( ) ) {
continue ;
}
const isRoot = this . # cursor . target ? . address === this . # root . address ;
if ( isRoot && this . # cursor . target ? . type === PublicationTreeNodeType . Branch ) {
return { done : true , value : null } ;
}
while ( await this . # cursor . tryMoveToFirstChild ( ) ) {
continue ;
}
if ( isRoot && this . # cursor . target ? . type === PublicationTreeNodeType . Leaf ) {
return { done : tru e, value : this. # events . get ( this . # cursor . target ! . address ) ! } ;
const event = await this . getEvent ( this . # cursor . target ! . address ) ;
return { done : false , value : event } ;
}
} while ( this . # cursor . target ? . type !== PublicationTreeNodeType . Leaf ) ;
this . # cursor . tryMoveToParent ( ) ;
} while ( this . # cursor . tryMoveToParent ( ) ) ;
const event = await this . getEvent ( this . # cursor . target ! . address ) ;
return { done : false , value : event } ;
// If we get to this point, we're at the root node (can't move up any more).
return { done : true , value : null } ;
}
// #endregion