Browse Source

load publication in order

master
silberengel 7 months ago
parent
commit
b9cd3ba10c
  1. 35
      src/lib/data_structures/publication_tree.ts

35
src/lib/data_structures/publication_tree.ts

@ -788,9 +788,7 @@ export class PublicationTree implements AsyncIterable<NDKEvent | null> {
}); });
} }
return Promise.resolve( return await this.#buildNodeFromEvent(event, address, parentNode);
this.#buildNodeFromEvent(event, address, parentNode),
);
} }
/** /**
@ -851,7 +849,7 @@ export class PublicationTree implements AsyncIterable<NDKEvent | null> {
this.#eventCache.set(address, fetchedEvent); this.#eventCache.set(address, fetchedEvent);
this.#events.set(address, fetchedEvent); this.#events.set(address, fetchedEvent);
return this.#buildNodeFromEvent(fetchedEvent, address, parentNode); return await this.#buildNodeFromEvent(fetchedEvent, address, parentNode);
} }
} catch (error) { } catch (error) {
console.debug( console.debug(
@ -894,11 +892,11 @@ export class PublicationTree implements AsyncIterable<NDKEvent | null> {
* AI-NOTE: 2025-01-24 - Helper method to build a node from an event * AI-NOTE: 2025-01-24 - Helper method to build a node from an event
* This extracts the common logic for building nodes from events * This extracts the common logic for building nodes from events
*/ */
#buildNodeFromEvent( async #buildNodeFromEvent(
event: NDKEvent, event: NDKEvent,
address: string, address: string,
parentNode: PublicationTreeNode, parentNode: PublicationTreeNode,
): PublicationTreeNode { ): Promise<PublicationTreeNode> {
this.#events.set(address, event); this.#events.set(address, event);
const childAddresses = event.tags const childAddresses = event.tags
@ -978,16 +976,21 @@ export class PublicationTree implements AsyncIterable<NDKEvent | null> {
children: [], children: [],
}; };
// Add children asynchronously // Add children in the order they appear in the a-tags to preserve section order
const childPromises = childAddresses.map((address) => // Use sequential processing to ensure order is maintained
this.addEventByAddress(address, event) console.log(`[PublicationTree] Adding ${childAddresses.length} children in order:`, childAddresses);
); for (const address of childAddresses) {
Promise.all(childPromises).catch((error) => { console.log(`[PublicationTree] Adding child: ${address}`);
console.warn( try {
`[PublicationTree] Error adding children for ${address}:`, await this.addEventByAddress(address, event);
error, console.log(`[PublicationTree] Successfully added child: ${address}`);
); } catch (error) {
}); console.warn(
`[PublicationTree] Error adding child ${address} for ${node.address}:`,
error,
);
}
}
this.#nodeResolvedObservers.forEach((observer) => observer(address)); this.#nodeResolvedObservers.forEach((observer) => observer(address));

Loading…
Cancel
Save