···265265 metadata.nextItemDepth = nextItem?.depth
266266267267 /*
268268- * We can now officially calculate `isLastSibling` and `isLastChild`
269269- * based on the actual data that we've seen.
268268+ * Item is the last "sibling" if we know for sure we're out of
269269+ * replies on the parent (even though this item itself may have its
270270+ * own reply branches).
270271 */
271271- metadata.isLastSibling =
272272+ const isLastSiblingByCounts =
272273 metadata.replyIndex ===
273273- metadata.parentMetadata.repliesSeenCounter - 1
274274+ metadata.parentMetadata.repliesIndexCounter - 1
275275+276276+ /*
277277+ * Item can also be the last "sibling" if we know we don't have a
278278+ * next item, OR if that next item's depth is less than this item's
279279+ * depth (meaning it's a sibling of the parent, not a child of this
280280+ * item).
281281+ */
282282+ const isImplicitlyLastSibling =
283283+ metadata.nextItemDepth === undefined ||
284284+ metadata.nextItemDepth < metadata.depth
285285+286286+ /*
287287+ * Ok now we can set the last sibling state.
288288+ */
289289+ metadata.isLastSibling =
290290+ isLastSiblingByCounts || isImplicitlyLastSibling
291291+292292+ /*
293293+ * Item is the last "child" in a branch if there is no next item,
294294+ * or if the next item's depth is less than this item's depth (a
295295+ * sibling of the parent) or equal to this item's depth (a sibling
296296+ * of this item)
297297+ */
274298 metadata.isLastChild =
275299 metadata.nextItemDepth === undefined ||
276300 metadata.nextItemDepth <= metadata.depth