Browse Source

Merge pull request #74 from jld3103/fix/cache-loading

neon: Fix loading data from cache
pull/76/head
jld3103 2 years ago committed by GitHub
parent
commit
e63b24a80b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      packages/neon/lib/src/blocs/apps.dart
  2. 2
      packages/neon/lib/src/blocs/capabilities.dart
  3. 2
      packages/neon/lib/src/blocs/user_details.dart
  4. 2
      packages/neon/lib/src/blocs/user_status.dart
  5. 19
      packages/neon/lib/src/utils/request_manager.dart

2
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);
}

2
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);
}

2
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);
}

2
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);
}

19
packages/neon/lib/src/utils/request_manager.dart

@ -33,10 +33,8 @@ class RequestManager {
final String k,
final Future<R> 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<T, R>(
clientID,
@ -45,8 +43,6 @@ class RequestManager {
(final d) => deserialize<R>(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<Uint8List> 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<String> 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<R> 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);
}
}

Loading…
Cancel
Save