Skip to content

Commit 3764a2e

Browse files
browsquare-tombstudio
authored
perf(mobile): ask typing subscriptions only for the typing window (#9454)
Every channel that shows a typing indicator (the open channel, and each Solo row on the home list) subscribes for typing and message events with `limit: 10` and no `since`, so each subscribe replays that channel's last 10 stored messages. The handler then ignores every one of them that is older than the 8 second typing window, so the replay only costs bytes. On an iPhone 12 mini signed in to Builderlab, seven Solo rows made this about 66 KB of kind 9 messages on each cold start, and again on each resubscribe. The subscription now asks only for events inside the typing window (`since` = now minus 8 s), which are the only ones the handler can act on. ### Validation New test: the typing subscription asks for history starting 8 s before it subscribes. It fails without this change. Full mobile test suite and `flutter analyze` pass. On the iPhone 12 mini (profile build with this change, signed in to Builderlab), stored and live events on the typing subscriptions in the first minute or more after a cold start: | | Subscriptions | Kind 9 messages | Typing events | | --- | --- | --- | --- | | Before (60 s) | 7 | 74 (66.6 KB) | 26 (15.6 KB) | | After (80 s) | 8 | 2 (1.5 KB) | 29 (17.8 KB) | Typing events are mostly live ones from agents working in Solo chats, so they stay about the same. The automated run covered a cold start to the home list and its Solo rows. ### Regressions - A device clock running more than 8 s fast can make the relay skip a message that would have ended an indicator. That indicator still clears when its 8 s window ends, and the handler already drops events it considers older than the window, so the same skew loses the same events today. Signed-off-by: Tom Brow <[email protected]> Co-authored-by: Tom Brow <[email protected]> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
1 parent 11ea8c9 commit 3764a2e

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ class ChannelTypingNotifier extends Notifier<List<TypingEntry>> {
187187
tags: {
188188
'#h': [channelId],
189189
},
190+
// Typing is ephemeral, and stored messages only matter while they
191+
// can still end a visible indicator (see [_handleTypingEvent]).
192+
since: DateTime.now().millisecondsSinceEpoch ~/ 1000 - _ttlMs ~/ 1000,
190193
limit: 10,
191194
),
192195
(event) {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import '../../helpers/thread_ids.dart';
99
class _Relay extends RelaySessionNotifier {
1010
late void Function(NostrEvent) receive;
1111
int calls = 0;
12+
NostrFilter? filter;
1213
bool reject = false;
1314
void Function(String)? close;
1415
int active = 0;
@@ -24,6 +25,7 @@ class _Relay extends RelaySessionNotifier {
2425
}) async {
2526
expect(filter.kinds, containsAll([20002, 9, 40002, 40008]));
2627
calls++;
28+
this.filter = filter;
2729
close = onClosed;
2830
if (reject) throw StateError('rejected');
2931
receive = onEvent;
@@ -43,6 +45,21 @@ void _settle(ProviderContainer container, FakeAsync async) {
4345
}
4446

4547
void main() {
48+
test('asks only for stored history inside the typing window', () async {
49+
final relay = _Relay();
50+
final container = ProviderContainer(
51+
overrides: [relaySessionProvider.overrideWith(() => relay)],
52+
);
53+
addTearDown(container.dispose);
54+
final before = DateTime.now().millisecondsSinceEpoch ~/ 1000;
55+
container.listen(channelTypingProvider('chat'), (_, _) {});
56+
await Future<void>.delayed(Duration.zero);
57+
final after = DateTime.now().millisecondsSinceEpoch ~/ 1000;
58+
// Older messages can no longer end an indicator, so replaying them on
59+
// every subscribe only downloads them.
60+
expect(relay.filter!.since, inInclusiveRange(before - 8, after - 8));
61+
});
62+
4663
test(
4764
'initial rejection retries with a bound and no uncaught error',
4865
() => fakeAsync((async) {

0 commit comments

Comments
 (0)