diff --git a/lib/controllers/event.dart b/lib/controllers/event.dart index 51b791f..a41d6b6 100644 --- a/lib/controllers/event.dart +++ b/lib/controllers/event.dart @@ -18,26 +18,30 @@ class EventController(final GetEventRequest request) if (event != null) return event; + final link = ref.keepAlive(); final fetchedEvent = await ref .read(ClientController.provider.notifier) .getEvent(request) - .onError((_, _) => null); + .onError((_, _) => null) + .whenComplete(link.close); if (fetchedEvent != null) { - ref - .read(RoomsController.provider.notifier) - .update( - .new({ - request.roomId: .new( - events: .new({fetchedEvent.rowId: fetchedEvent}), - ), - }), - ); - - return fetchedEvent; + // Storing the event invalidates this provider, as it watches the room. + // If nothing is listening, Riverpod won't rebuild it, and this build's + // result would be lost, so store it after returning. + final rooms = ref.read(RoomsController.provider.notifier); + Future.microtask( + () => rooms.update( + .new({ + request.roomId: .new( + events: .new({fetchedEvent.rowId: fetchedEvent}), + ), + }), + ), + ); } - return null; + return fetchedEvent; } static final provider = AsyncNotifierProvider.family diff --git a/lib/helpers/hooks/chat_scroll.dart b/lib/helpers/hooks/chat_scroll.dart index 7845576..dabea1b 100644 --- a/lib/helpers/hooks/chat_scroll.dart +++ b/lib/helpers/hooks/chat_scroll.dart @@ -127,10 +127,15 @@ final class ChatScroll({ final isAtBottom = position.extentBefore <= 0; if (isAtBottom != atBottom.value) atBottom.value = isAtBottom; + // contextualEvent updates immediately when jumping, but + // controllerData is only for the historical timeline once it loads + final isHistorical = + contextualEvent.value != null && + controllerData.value?.historicalData != null; + if (position.extentAfter <= loadThreshold) { await paginate(.backward); - } else if (contextualEvent.value != null && - position.extentBefore <= loadThreshold) { + } else if (isHistorical && position.extentBefore <= loadThreshold) { await paginate(.forward); } else if (position.extentBefore <= 0) { await markRead(); @@ -139,7 +144,7 @@ final class ChatScroll({ if (controllerData.isLoading == false && position.extentBefore <= 0 && controllerData.value?.hasMoreForward == false && - contextualEvent.value != null) { + isHistorical) { anchorId.value = null; contextualEvent.value = null; }