From d69cac4554e8a1bd4e90ef871178c6cf529d184f Mon Sep 17 00:00:00 2001 From: jld3103 Date: Sun, 2 Oct 2022 15:51:11 +0200 Subject: [PATCH] neon: Fix loading data from cache --- packages/neon/lib/src/blocs/apps.dart | 2 +- packages/neon/lib/src/blocs/capabilities.dart | 2 +- packages/neon/lib/src/blocs/user_details.dart | 2 +- packages/neon/lib/src/blocs/user_status.dart | 2 +- .../neon/lib/src/utils/request_manager.dart | 19 +++++-------------- 5 files changed, 9 insertions(+), 18 deletions(-) diff --git a/packages/neon/lib/src/blocs/apps.dart b/packages/neon/lib/src/blocs/apps.dart index 817ab936..e80b8e61 100644 --- a/packages/neon/lib/src/blocs/apps.dart +++ b/packages/neon/lib/src/blocs/apps.dart @@ -106,7 +106,7 @@ class AppsBloc extends $AppsBloc { 'apps-apps', () async => _account.client.core.getNavigationApps(), (final response) => response.ocs!.data!, - preloadCache: true, + previousData: _appsSubject.valueOrNull?.data, ) .listen(_appsSubject.add); } diff --git a/packages/neon/lib/src/blocs/capabilities.dart b/packages/neon/lib/src/blocs/capabilities.dart index e65ff7bc..bbde05d9 100644 --- a/packages/neon/lib/src/blocs/capabilities.dart +++ b/packages/neon/lib/src/blocs/capabilities.dart @@ -37,7 +37,7 @@ class CapabilitiesBloc extends $CapabilitiesBloc { 'capabilities', () async => _client.core.getCapabilities(), (final response) => response.ocs!.data!, - preloadCache: true, + previousData: _capabilitiesSubject.valueOrNull?.data, ) .listen(_capabilitiesSubject.add); } diff --git a/packages/neon/lib/src/blocs/user_details.dart b/packages/neon/lib/src/blocs/user_details.dart index 19f08c1b..7551e66c 100644 --- a/packages/neon/lib/src/blocs/user_details.dart +++ b/packages/neon/lib/src/blocs/user_details.dart @@ -34,7 +34,7 @@ class UserDetailsBloc extends $UserDetailsBloc { 'user-details', () async => _client.provisioningApi.getCurrentUser(), (final response) => response.ocs!.data!, - preloadCache: true, + previousData: _userDetailsSubject.valueOrNull?.data, ) .listen(_userDetailsSubject.add); } diff --git a/packages/neon/lib/src/blocs/user_status.dart b/packages/neon/lib/src/blocs/user_status.dart index 378b5794..07ac1ca1 100644 --- a/packages/neon/lib/src/blocs/user_status.dart +++ b/packages/neon/lib/src/blocs/user_status.dart @@ -45,7 +45,7 @@ class UserStatusBloc extends $UserStatusBloc { 'user-status', () async => _account.client.userStatus.getStatus(), (final response) => response.ocs?.data?.userStatus, - preloadCache: true, + previousData: _userStatusSubject.valueOrNull?.data, ) .listen(_userStatusSubject.add); } diff --git a/packages/neon/lib/src/utils/request_manager.dart b/packages/neon/lib/src/utils/request_manager.dart index 7cb392cd..71e94257 100644 --- a/packages/neon/lib/src/utils/request_manager.dart +++ b/packages/neon/lib/src/utils/request_manager.dart @@ -33,10 +33,8 @@ class RequestManager { final String k, final Future Function() call, final T Function(R) unwrap, { - final bool preloadCache = false, - final bool preferCache = false, + required final T? previousData, final bool disableTimeout = false, - final T? previousData, }) async* { yield* _wrap( clientID, @@ -45,8 +43,6 @@ class RequestManager { (final d) => deserialize(json.decode(d)), call, unwrap: unwrap, - preloadCache: preloadCache, - preferCache: preferCache, previousData: previousData, disableTimeout: disableTimeout, ); @@ -56,7 +52,6 @@ class RequestManager { final String clientID, final String k, final Future Function() call, { - final bool preloadCache = false, final bool preferCache = false, final bool disableTimeout = false, final Uint8List? previousData, @@ -67,7 +62,6 @@ class RequestManager { (final s) => base64.encode(s), (final d) => base64.decode(d), call, - preloadCache: preloadCache, preferCache: preferCache, previousData: previousData, disableTimeout: disableTimeout, @@ -77,7 +71,6 @@ class RequestManager { final String clientID, final String k, final Future Function() call, { - final bool preloadCache = false, final bool preferCache = false, final bool disableTimeout = false, final String? previousData, @@ -88,7 +81,6 @@ class RequestManager { (final s) => s, (final d) => d, call, - preloadCache: preloadCache, preferCache: preferCache, previousData: previousData, disableTimeout: disableTimeout, @@ -99,7 +91,6 @@ class RequestManager { final String Function(R) serialize, final R Function(String) deserialize, final Future Function() call, { - final bool preloadCache = false, final bool preferCache = false, final bool disableTimeout = false, T Function(R)? unwrap, @@ -117,14 +108,14 @@ class RequestManager { _print('[Request]: $k'); - if ((preferCache || preloadCache) && cache != null && await cache!.has(key)) { + if (cache != null && await cache!.has(key)) { _print('[Cache]: $k'); final s = unwrap(deserialize((await cache!.get(key))!)); - if (preloadCache) { - yield ResultCached(s, loading: true); - } else { + if (preferCache) { yield Result.success(s); return; + } else { + yield ResultCached(s, loading: true); } }