refactor(mobile): move relay event delivery into a part file

Keeps relay_session.dart under the file size limit with the new reports.

Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
Signed-off-by: Tom Brow <tomb@block.xyz>
This commit is contained in:
Tom Brow
2026-11-18 13:00:38 -08:00
parent 45159d98d0
commit 3226b8e0b5
2 changed files with 64 additions and 59 deletions

View File

@@ -34,6 +34,7 @@ export 'relay_session_types.dart';
part 'relay_session_auth.dart';
part 'relay_session_background.dart';
part 'relay_session_delivery.dart';
part 'relay_session_test_hooks.dart';
part 'relay_session_subscriptions.dart';
part 'relay_session_snapshot.dart';
@@ -999,15 +1000,6 @@ class RelaySessionNotifier extends Notifier<SessionState> {
});
}
void _reportInvalidRequest(String message) {
if (!isInvalidRequestRejection(message)) return;
_diagnostics.reportError(
const RelayInvalidRequest(),
StackTrace.current,
operation: DiagnosticOperation.relayRejection,
);
}
void _handleOk(List<dynamic> data) {
if (data.length < 3) return;
final eventId = data[1] as String;
@@ -1056,56 +1048,6 @@ class RelaySessionNotifier extends Notifier<SessionState> {
}
}
void _scheduleFlush() {
_flushTimer ??= Timer(
const Duration(milliseconds: _eventBatchMs),
_flushEventBuffer,
);
}
void _flushBufferedEventsNow() {
_flushTimer?.cancel();
_flushTimer = null;
_flushEventBuffer();
}
void _flushEventBuffer() {
_flushTimer = null;
if (_eventBuffer.isEmpty) return;
final batch = List<_BufferedEvent>.from(_eventBuffer);
_eventBuffer.clear();
for (final buffered in batch) {
final sub = _liveSubscriptions[buffered.subId];
if (sub == null) continue;
// Deduplicate per subscription. The same relay event can legitimately
// match multiple live subscriptions, e.g. the channel list unread listener
// and the open channel message listener.
final deliveryKey = '${buffered.subId}:${buffered.event.id}';
if (_recentDeliveryKeys.contains(deliveryKey)) continue;
// Cap the dedup set to prevent unbounded memory growth.
if (_recentDeliveryKeys.length >= _maxRecentDeliveryKeys) {
_recentDeliveryKeys.clear();
}
_recentDeliveryKeys.add(deliveryKey);
// A subscriber that throws must not cost the rest of the batch: its
// delivery keys are already recorded, so lost events never come back.
try {
sub.onEvent(buffered.event);
} catch (error, stackTrace) {
_diagnostics.reportError(
error,
stackTrace,
operation: DiagnosticOperation.eventSubscriber,
);
}
}
}
String _nextSubId(String prefix) {
_subIdCounter++;
return '$prefix-$_subIdCounter';

View File

@@ -0,0 +1,63 @@
part of 'relay_session.dart';
extension _RelaySessionDelivery on RelaySessionNotifier {
void _scheduleFlush() {
_flushTimer ??= Timer(
const Duration(milliseconds: RelaySessionNotifier._eventBatchMs),
_flushEventBuffer,
);
}
void _flushBufferedEventsNow() {
_flushTimer?.cancel();
_flushTimer = null;
_flushEventBuffer();
}
void _flushEventBuffer() {
_flushTimer = null;
if (_eventBuffer.isEmpty) return;
final batch = List<_BufferedEvent>.from(_eventBuffer);
_eventBuffer.clear();
for (final buffered in batch) {
final sub = _liveSubscriptions[buffered.subId];
if (sub == null) continue;
// Deduplicate per subscription. The same relay event can legitimately
// match multiple live subscriptions, e.g. the channel list unread listener
// and the open channel message listener.
final deliveryKey = '${buffered.subId}:${buffered.event.id}';
if (_recentDeliveryKeys.contains(deliveryKey)) continue;
// Cap the dedup set to prevent unbounded memory growth.
if (_recentDeliveryKeys.length >=
RelaySessionNotifier._maxRecentDeliveryKeys) {
_recentDeliveryKeys.clear();
}
_recentDeliveryKeys.add(deliveryKey);
// A subscriber that throws must not cost the rest of the batch: its
// delivery keys are already recorded, so lost events never come back.
try {
sub.onEvent(buffered.event);
} catch (error, stackTrace) {
_diagnostics.reportError(
error,
stackTrace,
operation: DiagnosticOperation.eventSubscriber,
);
}
}
}
void _reportInvalidRequest(String message) {
if (!isInvalidRequestRejection(message)) return;
_diagnostics.reportError(
const RelayInvalidRequest(),
StackTrace.current,
operation: DiagnosticOperation.relayRejection,
);
}
}