Fresh installs could sit on 'can't reach the servers' forever: the offers cubit captured the transport once at build time, the shared connection only retried on a connectivity CHANGE, and a silently-filtered relay could stall the pool for minutes. - SocialConnection: retry with backoff after a failed attempt while started and not knowingly offline (injectable schedule for tests) - OffersCubit: follow connection.sessions, re-attach the transport and re-run the last discovery on (re)connect; announce drops via connectionEpoch - market _init: record the wanted area on the cubit even while offline - NostrOfferTransport.discoverPage: sort a copy (channel lists may be unmodifiable)
154 lines
5 KiB
Dart
154 lines
5 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:connectivity_plus/connectivity_plus.dart';
|
|
|
|
import 'social_service.dart';
|
|
import 'social_settings.dart';
|
|
|
|
/// Opens a session against the given relays. Injectable so the connection logic
|
|
/// can be unit-tested without a real relay.
|
|
typedef SessionOpener = Future<SocialSession> Function(List<String> relays);
|
|
|
|
/// ONE shared relay connection per identity, reused by every social feature
|
|
/// (offers, messaging, trust, profile, the inbox listener) instead of each
|
|
/// opening its own `RelayPool`. Fewer sockets, less battery, one place to manage
|
|
/// reconnection.
|
|
///
|
|
/// Lazily connects on first [session] call; reconnects when the network returns
|
|
/// and drops the session when it goes away, announcing each change on [sessions]
|
|
/// so long-lived consumers (the inbox listener) can re-subscribe. Callers must
|
|
/// NOT close the session they get — this owns its lifecycle. Recreated on an
|
|
/// identity switch (the old one is disposed).
|
|
class SocialConnection {
|
|
SocialConnection({
|
|
required SocialService social,
|
|
required SocialSettings settings,
|
|
SessionOpener? open,
|
|
Stream<bool>? online,
|
|
List<Duration>? retrySchedule,
|
|
}) : _settings = settings,
|
|
_open = open ?? social.openSession,
|
|
_online = online,
|
|
_retrySchedule = retrySchedule ?? _defaultRetrySchedule;
|
|
|
|
/// Backoff for self-retries after a failed attempt while started and not
|
|
/// knowingly offline (the fresh-install case: online the whole time, first
|
|
/// connect fails, so no connectivity change ever retriggers a connect).
|
|
static const _defaultRetrySchedule = [
|
|
Duration(seconds: 5),
|
|
Duration(seconds: 15),
|
|
Duration(seconds: 45),
|
|
Duration(seconds: 90),
|
|
];
|
|
|
|
final SocialSettings _settings;
|
|
final SessionOpener _open;
|
|
final Stream<bool>? _online;
|
|
final List<Duration> _retrySchedule;
|
|
|
|
final _sessions = StreamController<SocialSession?>.broadcast();
|
|
SocialSession? _current;
|
|
Future<SocialSession?>? _pending;
|
|
StreamSubscription<bool>? _onlineSub;
|
|
bool _disposed = false;
|
|
bool _started = false;
|
|
bool _knownOffline = false;
|
|
Timer? _retryTimer;
|
|
int _retryIndex = 0;
|
|
|
|
/// Emits the live session on each (re)connect, and null when it drops.
|
|
Stream<SocialSession?> get sessions => _sessions.stream;
|
|
|
|
/// The current shared session, or null if not connected right now.
|
|
SocialSession? get current => _current;
|
|
|
|
/// Begins watching connectivity (reconnect on regain, drop when offline) and
|
|
/// attempts an initial connect. Idempotent-ish; call once at startup.
|
|
void start() {
|
|
_started = true;
|
|
_onlineSub = (_online ?? _connectivityOnline()).listen((isOnline) {
|
|
_knownOffline = !isOnline;
|
|
if (!isOnline) {
|
|
_cancelRetry();
|
|
_drop();
|
|
} else if (_current == null) {
|
|
_retryIndex = 0; // fresh network — start the backoff over
|
|
unawaited(session());
|
|
}
|
|
});
|
|
unawaited(session()); // initial attempt (also connects if already online)
|
|
}
|
|
|
|
/// The shared session, connecting on first use. Returns null when offline or
|
|
/// no relay is reachable; the feature then degrades gracefully.
|
|
Future<SocialSession?> session() {
|
|
if (_current != null) return Future.value(_current);
|
|
return _pending ??= _connect();
|
|
}
|
|
|
|
Future<SocialSession?> _connect() async {
|
|
try {
|
|
final relays = await _settings.relayUrls();
|
|
if (relays.isEmpty) return null;
|
|
final s = await _open(relays);
|
|
if (_disposed) {
|
|
await s.close();
|
|
return null;
|
|
}
|
|
_current = s;
|
|
_retryIndex = 0;
|
|
_cancelRetry();
|
|
_sessions.add(s);
|
|
return s;
|
|
} catch (_) {
|
|
_scheduleRetry(); // unreachable — retry with backoff (see below)
|
|
return null;
|
|
} finally {
|
|
_pending = null;
|
|
}
|
|
}
|
|
|
|
/// After a failed attempt, retries by itself while started and not knowingly
|
|
/// offline — a connectivity change may never come if the device was online
|
|
/// all along. Un-started connections (one-shot [session] callers, tests)
|
|
/// never leave a timer behind.
|
|
void _scheduleRetry() {
|
|
if (!_started || _disposed || _knownOffline || _current != null) return;
|
|
_cancelRetry();
|
|
final i = _retryIndex < _retrySchedule.length
|
|
? _retryIndex
|
|
: _retrySchedule.length - 1;
|
|
_retryIndex++;
|
|
_retryTimer = Timer(_retrySchedule[i], () {
|
|
if (_disposed || _current != null) return;
|
|
unawaited(session());
|
|
});
|
|
}
|
|
|
|
void _cancelRetry() {
|
|
_retryTimer?.cancel();
|
|
_retryTimer = null;
|
|
}
|
|
|
|
void _drop() {
|
|
final s = _current;
|
|
_current = null;
|
|
if (s != null) {
|
|
unawaited(s.close());
|
|
if (!_sessions.isClosed) _sessions.add(null);
|
|
}
|
|
}
|
|
|
|
Future<void> dispose() async {
|
|
_disposed = true;
|
|
_cancelRetry();
|
|
await _onlineSub?.cancel();
|
|
_onlineSub = null;
|
|
_drop();
|
|
if (!_sessions.isClosed) await _sessions.close();
|
|
}
|
|
|
|
static Stream<bool> _connectivityOnline() =>
|
|
Connectivity().onConnectivityChanged.map((results) =>
|
|
results.any((r) => r != ConnectivityResult.none));
|
|
}
|