refactor: optimize providers with keepAlive for improved state management
- Updated multiple providers to use `@Riverpod(keepAlive: true)` for better state retention throughout the app lifecycle. - Enhanced `SocketConnectionStream` and `ConversationDeltaStream` with comments clarifying the purpose of public getters. - Improved error handling in the `_ChatPageState` by ensuring proper checks for mounted state before using context. - Added comments to clarify the rationale behind keepAlive usage in various providers, ensuring better maintainability and understanding of the codebase.
This commit is contained in:
@@ -60,6 +60,8 @@ final hasSavedCredentialsProvider2 = FutureProvider<bool>((ref) async {
|
||||
|
||||
/// Computed providers for UI consumption
|
||||
/// These automatically update when auth state changes
|
||||
/// These are keepAlive since they derive from keepAlive authStateManagerProvider
|
||||
/// and are used throughout the app lifecycle
|
||||
|
||||
final isAuthenticatedProvider2 = Provider<bool>((ref) {
|
||||
final authState = ref.watch(authStateManagerProvider);
|
||||
|
||||
@@ -1167,9 +1167,10 @@ class _ChatPageState extends ConsumerState<ChatPage> {
|
||||
// If still loading, wait for it to complete
|
||||
try {
|
||||
final models = await ref.read(modelsProvider.future);
|
||||
if (mounted) {
|
||||
_showModelDropdown(context, ref, models);
|
||||
}
|
||||
// Check mounted and use context immediately together
|
||||
if (!mounted) return;
|
||||
// ignore: use_build_context_synchronously
|
||||
_showModelDropdown(context, ref, models);
|
||||
} catch (e) {
|
||||
DebugLogger.error(
|
||||
'model-load-failed',
|
||||
@@ -1178,16 +1179,17 @@ class _ChatPageState extends ConsumerState<ChatPage> {
|
||||
);
|
||||
}
|
||||
} else if (modelsAsync.hasValue) {
|
||||
// If we have data, show immediately
|
||||
// If we have data, show immediately (no async gap)
|
||||
_showModelDropdown(context, ref, modelsAsync.value!);
|
||||
} else if (modelsAsync.hasError) {
|
||||
// If there's an error, try to refresh and load
|
||||
try {
|
||||
ref.invalidate(modelsProvider);
|
||||
final models = await ref.read(modelsProvider.future);
|
||||
if (mounted) {
|
||||
_showModelDropdown(context, ref, models);
|
||||
}
|
||||
// Check mounted and use context immediately together
|
||||
if (!mounted) return;
|
||||
// ignore: use_build_context_synchronously
|
||||
_showModelDropdown(context, ref, models);
|
||||
} catch (e) {
|
||||
DebugLogger.error(
|
||||
'model-refresh-failed',
|
||||
|
||||
Reference in New Issue
Block a user