Skip to content

Commit f216dd6

Browse files
square-tombstudio
andcommitted
fix(mobile): retire directory-only loads on a scope switch
A directory-only search load kept its ownership marker after a community or identity switch retired it. On returning to the first community, the list refresh treated the abandoned loading status as owned by a live load and left it in place, so search never loaded the directory again. A scope switch now retires directory-only loads too: it advances the directory generation and clears the marker. Tests cover returning both while the old request is in flight and after it ended. Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz> Signed-off-by: Tom Brow <[email protected]>
1 parent ba0d0a5 commit f216dd6

3 files changed

Lines changed: 118 additions & 2 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,16 @@ class _ChannelRefreshCoordinator {
459459
/// wire cannot be written into the new scope.
460460
void retireInFlight() => _generation++;
461461

462+
/// Retires every in-flight request, directory-only loads included, for a
463+
/// relay or identity change. A directory-only load survives ordinary list
464+
/// refreshes, but not a scope change: its loading status must not look
465+
/// owned when the user returns to the old scope.
466+
void retireScope() {
467+
_generation++;
468+
_directoryGeneration++;
469+
_directoryOnlyGeneration = null;
470+
}
471+
462472
/// Starts a fenced refresh without issuing the directory query.
463473
///
464474
/// Used by callers that must carry the scope across later awaits even when

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ class ChannelsNotifier extends AsyncNotifier<List<Channel>> {
228228
_confirmedJoins.clear();
229229
// Retire any in-flight directory request: its response describes the
230230
// previous relay or identity and must not reach this scope's state.
231-
_refreshCoordinator.retireInFlight();
231+
_refreshCoordinator.retireScope();
232232
}
233233
final snapshot =
234234
(_hasLoaded ? state.value : null) ??

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

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1963,6 +1963,107 @@ void main() {
19631963
isNot(communityB),
19641964
);
19651965
});
1966+
1967+
test(
1968+
'returning to a community during a discarded load loads again',
1969+
() async {
1970+
final session = _FakeRelaySession(
1971+
memberships: [_membership(_channelA, myPk)],
1972+
metadata: [
1973+
_meta(id: _channelA, name: 'general'),
1974+
_meta(id: _channelB, name: 'community-a-open'),
1975+
],
1976+
);
1977+
final container = await loaded(session);
1978+
final communityA = container.read(relayConfigProvider).baseUrl;
1979+
1980+
session.pauseNextDirectoryQuery();
1981+
final directory = container
1982+
.read(channelsProvider.notifier)
1983+
.ensureDirectoryLoaded(maxAge: Duration.zero);
1984+
await session.nextDirectoryQueryStarted;
1985+
1986+
container
1987+
.read(relayConfigProvider.notifier)
1988+
.update(baseUrl: 'https://community-b.example');
1989+
await container.read(channelsProvider.future);
1990+
container
1991+
.read(relayConfigProvider.notifier)
1992+
.update(baseUrl: communityA);
1993+
await container.read(channelsProvider.future);
1994+
await _settle();
1995+
final directoryQueries = session.directoryQueryFilters.length;
1996+
await container
1997+
.read(channelsProvider.notifier)
1998+
.ensureDirectoryLoaded(maxAge: Duration.zero);
1999+
2000+
expect(
2001+
session.directoryQueryFilters.length,
2002+
greaterThan(directoryQueries),
2003+
);
2004+
expect(
2005+
container.read(channelDirectoryLoadStatusProvider).status,
2006+
ChannelDirectoryLoadStatus.loaded,
2007+
);
2008+
2009+
// The old request ending later leaves the newer load's status alone.
2010+
session.resumePausedDirectoryQuery();
2011+
await directory;
2012+
await _settle();
2013+
expect(
2014+
container.read(channelDirectoryLoadStatusProvider).status,
2015+
ChannelDirectoryLoadStatus.loaded,
2016+
);
2017+
},
2018+
);
2019+
2020+
test(
2021+
'returning to a community after a discarded load loads again',
2022+
() async {
2023+
final session = _FakeRelaySession(
2024+
memberships: [_membership(_channelA, myPk)],
2025+
metadata: [
2026+
_meta(id: _channelA, name: 'general'),
2027+
_meta(id: _channelB, name: 'community-a-open'),
2028+
],
2029+
);
2030+
final container = await loaded(session);
2031+
final communityA = container.read(relayConfigProvider).baseUrl;
2032+
2033+
session.pauseNextDirectoryQuery();
2034+
final directory = container
2035+
.read(channelsProvider.notifier)
2036+
.ensureDirectoryLoaded(maxAge: Duration.zero);
2037+
await session.nextDirectoryQueryStarted;
2038+
2039+
container
2040+
.read(relayConfigProvider.notifier)
2041+
.update(baseUrl: 'https://community-b.example');
2042+
await container.read(channelsProvider.future);
2043+
session.resumePausedDirectoryQuery();
2044+
await directory;
2045+
await _settle();
2046+
2047+
container
2048+
.read(relayConfigProvider.notifier)
2049+
.update(baseUrl: communityA);
2050+
await container.read(channelsProvider.future);
2051+
await _settle();
2052+
final directoryQueries = session.directoryQueryFilters.length;
2053+
await container
2054+
.read(channelsProvider.notifier)
2055+
.ensureDirectoryLoaded(maxAge: Duration.zero);
2056+
2057+
expect(
2058+
session.directoryQueryFilters.length,
2059+
greaterThan(directoryQueries),
2060+
);
2061+
expect(
2062+
container.read(channelDirectoryLoadStatusProvider).status,
2063+
ChannelDirectoryLoadStatus.loaded,
2064+
);
2065+
},
2066+
);
19662067
});
19672068

19682069
test('membership refresh does not refetch a loaded directory', () async {
@@ -3490,6 +3591,7 @@ class _FakeRelaySession extends RelaySessionNotifier {
34903591
Completer<void>? _pausedSubscribe;
34913592
Completer<void>? _subscribeStarted;
34923593
Completer<void>? _pausedDirectory;
3594+
bool _pauseClaimed = false;
34933595
Completer<void>? _directoryStarted;
34943596
Completer<void>? _pausedHiddenDm;
34953597
Completer<void>? _hiddenDmStarted;
@@ -3812,11 +3914,15 @@ class _FakeRelaySession extends RelaySessionNotifier {
38123914
// active when the response is released.
38133915
final directorySnapshot = List.of(metadata);
38143916
final paused = _pausedDirectory;
3815-
if (paused != null) {
3917+
// Only the first query after the pause waits; a later one meanwhile
3918+
// (a new scope's load) answers at once.
3919+
if (paused != null && !_pauseClaimed) {
3920+
_pauseClaimed = true;
38163921
_directoryStarted!.complete();
38173922
await paused.future;
38183923
_pausedDirectory = null;
38193924
_directoryStarted = null;
3925+
_pauseClaimed = false;
38203926
}
38213927
if (directoryFailures > 0) {
38223928
directoryFailures--;

0 commit comments

Comments
 (0)