@@ -23,14 +23,40 @@ class ChannelWindowThreadSummary {
2323 });
2424}
2525
26+ /// A thread-summary snapshot received from the live channel subscription.
27+ ///
28+ /// [createdAt] is the relay event timestamp used to reject delayed snapshots
29+ /// after a fresher recount has already been displayed.
30+ class ChannelWindowLiveThreadSummary {
31+ final ChannelWindowThreadSummary summary;
32+ final int createdAt;
33+
34+ const ChannelWindowLiveThreadSummary ({
35+ required this .summary,
36+ required this .createdAt,
37+ });
38+ }
39+
2640class ChannelWindowRow {
2741 final NostrEvent event;
2842 final ChannelWindowThreadSummary ? thread;
43+ final int ? threadSummaryCreatedAt;
2944
30- const ChannelWindowRow ({required this .event, this .thread});
45+ const ChannelWindowRow ({
46+ required this .event,
47+ this .thread,
48+ this .threadSummaryCreatedAt,
49+ });
3150
32- ChannelWindowRow copyWith ({ChannelWindowThreadSummary ? thread}) =>
33- ChannelWindowRow (event: event, thread: thread ?? this .thread);
51+ ChannelWindowRow copyWith ({
52+ ChannelWindowThreadSummary ? thread,
53+ int ? threadSummaryCreatedAt,
54+ }) => ChannelWindowRow (
55+ event: event,
56+ thread: thread ?? this .thread,
57+ threadSummaryCreatedAt:
58+ threadSummaryCreatedAt ?? this .threadSummaryCreatedAt,
59+ );
3460}
3561
3662class ChannelWindowPage {
@@ -59,7 +85,7 @@ class ChannelWindowStore {
5985 /// Kept beside the pages rather than folded into [ChannelWindowRow.thread]
6086 /// because a root can be in [liveOverlay] instead — a message you just sent
6187 /// has no row yet, and its reply count has to land somewhere.
62- final Map <String , ChannelWindowThreadSummary > liveThreadSummaries;
88+ final Map <String , ChannelWindowLiveThreadSummary > liveThreadSummaries;
6389
6490 const ChannelWindowStore ({
6591 required this .pages,
@@ -96,6 +122,7 @@ ChannelWindowPage parseChannelWindowResponse(
96122 if (rowIndex == null ) continue ;
97123 rows[rowIndex] = rows[rowIndex].copyWith (
98124 thread: parseChannelWindowThreadSummary (event),
125+ threadSummaryCreatedAt: event.createdAt,
99126 );
100127 }
101128
@@ -150,13 +177,28 @@ ChannelWindowStore replaceNewestChannelWindow(
150177 liveAux: current.liveAux
151178 .where ((event) => ! auxIds.contains (event.id))
152179 .toList (),
153- // A refetched row carries its own summary and is authoritative, so drop the
154- // live entry for it. Roots still only in the overlay keep theirs .
155- liveThreadSummaries: _retainLiveSummaries (current, rowIds ),
180+ // A page summary supersedes an equal-or-older live recount. A fresher live
181+ // recount can race the query response, so retain that one on top .
182+ liveThreadSummaries: _retainLiveSummariesAfterPage (current, page.rows ),
156183 );
157184}
158185
159- Map <String , ChannelWindowThreadSummary > _retainLiveSummaries (
186+ Map <String , ChannelWindowLiveThreadSummary > _retainLiveSummariesAfterPage (
187+ ChannelWindowStore current,
188+ List <ChannelWindowRow > rows,
189+ ) {
190+ final rowsById = {for (final row in rows) row.event.id: row};
191+ return {
192+ for (final entry in current.liveThreadSummaries.entries)
193+ if (rowsById[entry.key] == null ||
194+ rowsById[entry.key]! .thread == null ||
195+ entry.value.createdAt >
196+ (rowsById[entry.key]! .threadSummaryCreatedAt ?? - 1 ))
197+ entry.key: entry.value,
198+ };
199+ }
200+
201+ Map <String , ChannelWindowLiveThreadSummary > _retainLiveSummaries (
160202 ChannelWindowStore current,
161203 Set <String > supersededRootIds,
162204) {
@@ -236,11 +278,21 @@ ChannelWindowStore mergeLiveChannelWindowEvent(
236278 } catch (_) {
237279 return current;
238280 }
281+ final existing = current.liveThreadSummaries[rootId];
282+ if (existing != null && existing.createdAt >= event.createdAt) {
283+ return current;
284+ }
239285 return ChannelWindowStore (
240286 pages: current.pages,
241287 liveOverlay: current.liveOverlay,
242288 liveAux: current.liveAux,
243- liveThreadSummaries: {...current.liveThreadSummaries, rootId: summary},
289+ liveThreadSummaries: {
290+ ...current.liveThreadSummaries,
291+ rootId: ChannelWindowLiveThreadSummary (
292+ summary: summary,
293+ createdAt: event.createdAt,
294+ ),
295+ },
244296 );
245297 }
246298
@@ -316,7 +368,8 @@ Map<String, ChannelWindowThreadSummary> channelWindowThreadSummaries(
316368 for (final row in page.rows)
317369 if (row.thread != null ) row.event.id: row.thread! ,
318370 // Live entries last: they are the newer snapshot for any root they cover.
319- ...store.liveThreadSummaries,
371+ for (final entry in store.liveThreadSummaries.entries)
372+ entry.key: entry.value.summary,
320373 };
321374}
322375
0 commit comments