Skip to content

Commit 5ff7541

Browse files
browclaude
andcommitted
mobile: stop typing prune ticks from rebuilding channel pages
The typing prune timer published a new list every second while anyone was typing, and the channel and thread pages watched the whole list, so each tick rebuilt the page and its message list. Publish only when the visible entries change, and have pages select a value-equal view that excludes the current user's own echoed indicator. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Tom Brow <[email protected]>
1 parent e922dc0 commit 5ff7541

4 files changed

Lines changed: 93 additions & 16 deletions

File tree

‎mobile/lib/features/channels/channel_detail_page.dart‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -365,14 +365,16 @@ class _ResolvedChannelDetailPage extends HookConsumerWidget {
365365
// Me flattens replies into this timeline, so include their typing signals.
366366
// Ordinary channels keep thread activity inside the thread view.
367367
final typingEntries = ref
368-
.watch(channelTypingProvider(channel.id))
369-
.where((e) => personalChat || e.threadHeadId == null)
370-
.where(
371-
(e) =>
372-
currentPubkey == null ||
373-
e.pubkey.toLowerCase() != currentPubkey.toLowerCase(),
368+
.watch(
369+
channelTypingProvider(channel.id).select(
370+
(all) => VisibleTyping(
371+
all,
372+
selfPubkey: currentPubkey,
373+
include: (e) => personalChat || e.threadHeadId == null,
374+
),
375+
),
374376
)
375-
.toList();
377+
.entries;
376378
final baseChannel = listedChannel ?? channel;
377379
final resolvedChannel =
378380
detailsAsync.whenData(baseChannel.mergeDetails).value ?? baseChannel;

‎mobile/lib/features/channels/channel_typing_provider.dart‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,43 @@ class TypingEntry {
1717
this.threadHeadId,
1818
required this.expiresAtMs,
1919
});
20+
21+
@override
22+
bool operator ==(Object other) =>
23+
other is TypingEntry &&
24+
other.pubkey == pubkey &&
25+
other.threadHeadId == threadHeadId &&
26+
other.expiresAtMs == expiresAtMs;
27+
28+
@override
29+
int get hashCode => Object.hash(pubkey, threadHeadId, expiresAtMs);
30+
}
31+
32+
/// The [TypingEntry]s other than [selfPubkey]'s that [include] keeps,
33+
/// compared by value, so a widget that selects them rebuilds only when
34+
/// they change rather than on every update to the channel's list.
35+
@immutable
36+
class VisibleTyping {
37+
final List<TypingEntry> entries;
38+
39+
VisibleTyping(
40+
List<TypingEntry> all, {
41+
required String? selfPubkey,
42+
required bool Function(TypingEntry entry) include,
43+
}) : entries = List.unmodifiable([
44+
for (final entry in all)
45+
if ((selfPubkey == null ||
46+
entry.pubkey.toLowerCase() != selfPubkey.toLowerCase()) &&
47+
include(entry))
48+
entry,
49+
]);
50+
51+
@override
52+
bool operator ==(Object other) =>
53+
other is VisibleTyping && listEquals(other.entries, entries);
54+
55+
@override
56+
int get hashCode => Object.hashAll(entries);
2057
}
2158

2259
/// Tracks who is currently typing in a specific channel.
@@ -195,7 +232,7 @@ class ChannelTypingNotifier extends Notifier<List<TypingEntry>> {
195232
void _prune() {
196233
final now = DateTime.now().millisecondsSinceEpoch;
197234
_activity.removeWhere((_, value) => value.retainUntil <= now);
198-
state = [
235+
final next = [
199236
for (final entry in _activity.entries)
200237
if (entry.value.visibleUntil > now)
201238
TypingEntry(
@@ -204,6 +241,9 @@ class ChannelTypingNotifier extends Notifier<List<TypingEntry>> {
204241
expiresAtMs: entry.value.visibleUntil,
205242
),
206243
];
244+
// The timer ticks every second while anyone is active. Publishing an
245+
// unchanged list would rebuild every watcher, including channel pages.
246+
if (!listEquals(next, state)) state = next;
207247
if (_activity.isEmpty) {
208248
_pruneTimer?.cancel();
209249
_pruneTimer = null;

‎mobile/lib/features/channels/thread_detail_page.dart‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -784,15 +784,17 @@ class ThreadDetailPage extends HookConsumerWidget {
784784
);
785785

786786
// Thread-scoped typing indicators (exclude self).
787-
final allTyping = ref.watch(channelTypingProvider(channelId));
788-
final threadTyping = allTyping
789-
.where((e) => e.threadHeadId == threadHead.id)
790-
.where(
791-
(e) =>
792-
currentPubkey == null ||
793-
e.pubkey.toLowerCase() != currentPubkey.toLowerCase(),
787+
final threadTyping = ref
788+
.watch(
789+
channelTypingProvider(channelId).select(
790+
(all) => VisibleTyping(
791+
all,
792+
selfPubkey: currentPubkey,
793+
include: (e) => e.threadHeadId == threadHead.id,
794+
),
795+
),
794796
)
795-
.toList();
797+
.entries;
796798

797799
// The root of the entire thread chain. If the current thread head is
798800
// itself a root message its rootId is null, so fall back to its own id.

‎mobile/test/features/channels/channel_typing_provider_test.dart‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:buzz/shared/relay/relay.dart';
33
import 'package:fake_async/fake_async.dart';
44
import 'package:flutter_test/flutter_test.dart';
55
import 'package:hooks_riverpod/hooks_riverpod.dart';
6+
67
import '../../helpers/thread_ids.dart';
78

89
class _Relay extends RelaySessionNotifier {
@@ -214,4 +215,36 @@ void main() {
214215
expect(container.read(provider), hasLength(2));
215216
},
216217
);
218+
test(
219+
'prune ticks publish only when the visible entries change',
220+
() => fakeAsync((async) {
221+
final relay = _Relay();
222+
final container = ProviderContainer(
223+
overrides: [relaySessionProvider.overrideWith(() => relay)],
224+
);
225+
final provider = channelTypingProvider('chat');
226+
var notifications = 0;
227+
container.listen(provider, (_, _) => notifications++);
228+
_settle(container, async);
229+
relay.receive(
230+
NostrEvent(
231+
id: 'typing',
232+
pubkey: 'agent',
233+
kind: 20002,
234+
createdAt: DateTime.now().millisecondsSinceEpoch ~/ 1000,
235+
content: '',
236+
sig: '',
237+
tags: [
238+
['h', 'chat'],
239+
],
240+
),
241+
);
242+
expect(container.read(provider), hasLength(1));
243+
expect(notifications, 1);
244+
async.elapse(const Duration(milliseconds: 5500));
245+
expect(container.read(provider), hasLength(1));
246+
expect(notifications, 1);
247+
container.dispose();
248+
}),
249+
);
217250
}

0 commit comments

Comments
 (0)