fix dispose issue on read

This commit is contained in:
Fabian Freund
2025-07-27 15:35:06 +02:00
parent 0364cbe7ff
commit ebf0500f29
3 changed files with 8 additions and 4 deletions
+3 -1
View File
@@ -23,7 +23,7 @@ import 'package:riverpod/riverpod.dart';
extension CacheForExtension on Ref { extension CacheForExtension on Ref {
/// Keeps the provider alive for [duration]. /// Keeps the provider alive for [duration].
void cacheFor(Duration duration) { KeepAliveLink cacheFor(Duration duration) {
// Immediately prevent the state from getting destroyed. // Immediately prevent the state from getting destroyed.
final link = keepAlive(); final link = keepAlive();
// After duration has elapsed, we re-enable automatic disposal. // After duration has elapsed, we re-enable automatic disposal.
@@ -32,5 +32,7 @@ extension CacheForExtension on Ref {
// Optional: when the provider is recomputed (such as with ref.watch), // Optional: when the provider is recomputed (such as with ref.watch),
// we cancel the pending timer. // we cancel the pending timer.
onDispose(timer.cancel); onDispose(timer.cancel);
return link;
} }
} }
@@ -81,6 +81,8 @@ Future<WebPageInfo> pageInfo(
Uri url, { Uri url, {
required bool isImageRequest, required bool isImageRequest,
}) async { }) async {
final link = ref.cacheFor(const Duration(minutes: 2));
final tabId = ref.read(selectedTabProvider); final tabId = ref.read(selectedTabProvider);
int? proxyPort; int? proxyPort;
@@ -112,8 +114,8 @@ Future<WebPageInfo> pageInfo(
proxyPort: proxyPort, proxyPort: proxyPort,
); );
if (result.isSuccess) { if (!result.isSuccess) {
ref.cacheFor(const Duration(minutes: 2)); link.close();
} }
return result.value; return result.value;
@@ -6,7 +6,7 @@ part of 'website_title.dart';
// RiverpodGenerator // RiverpodGenerator
// ************************************************************************** // **************************************************************************
String _$pageInfoHash() => r'46eec6236555d37b5bbb4d8d887c5ee6ee0336e3'; String _$pageInfoHash() => r'53380fbf9c67d494944330641cefbde9455cdb60';
/// Copied from Dart SDK /// Copied from Dart SDK
class _SystemHash { class _SystemHash {