dart format
This commit is contained in:
+36
-27
@@ -49,7 +49,10 @@ abstract class RequestCache<T extends ApiResponse?> {
|
||||
|
||||
Future<void> start(String document) async {
|
||||
try {
|
||||
final tableData = await Localstore.instance.collection(collection).doc(document).get();
|
||||
final tableData = await Localstore.instance
|
||||
.collection(collection)
|
||||
.doc(document)
|
||||
.get();
|
||||
if (tableData != null) {
|
||||
final cached = onLocalData(tableData['json'] as String);
|
||||
onUpdate?.call(cached);
|
||||
@@ -57,7 +60,8 @@ abstract class RequestCache<T extends ApiResponse?> {
|
||||
}
|
||||
|
||||
final lastUpdate = (tableData?['lastupdate'] as num?) ?? 0;
|
||||
if (DateTime.now().millisecondsSinceEpoch - (maxCacheTime * 1000) < lastUpdate) {
|
||||
if (DateTime.now().millisecondsSinceEpoch - (maxCacheTime * 1000) <
|
||||
lastUpdate) {
|
||||
if (renew == null || !renew!) return;
|
||||
}
|
||||
|
||||
@@ -65,10 +69,12 @@ abstract class RequestCache<T extends ApiResponse?> {
|
||||
final newValue = await onLoad();
|
||||
onUpdate?.call(newValue);
|
||||
onNetworkData?.call(newValue);
|
||||
unawaited(Localstore.instance.collection(collection).doc(document).set({
|
||||
'json': jsonEncode(newValue),
|
||||
'lastupdate': DateTime.now().millisecondsSinceEpoch,
|
||||
}));
|
||||
unawaited(
|
||||
Localstore.instance.collection(collection).doc(document).set({
|
||||
'json': jsonEncode(newValue),
|
||||
'lastupdate': DateTime.now().millisecondsSinceEpoch,
|
||||
}),
|
||||
);
|
||||
} on Exception catch (e) {
|
||||
onError(e);
|
||||
}
|
||||
@@ -79,7 +85,6 @@ abstract class RequestCache<T extends ApiResponse?> {
|
||||
|
||||
T onLocalData(String json);
|
||||
Future<T> onLoad();
|
||||
|
||||
}
|
||||
|
||||
/// Concrete [RequestCache] that takes the two overrides as constructor
|
||||
@@ -97,22 +102,23 @@ class SimpleCache<T extends ApiResponse?> extends RequestCache<T> {
|
||||
void Function(T)? onNetworkData,
|
||||
void Function(Exception)? onError,
|
||||
bool? renew,
|
||||
}) : _loader = loader,
|
||||
_fromJson = fromJson,
|
||||
super(
|
||||
cacheTime,
|
||||
onUpdate,
|
||||
onError: onError ?? RequestCache.ignore,
|
||||
renew: renew,
|
||||
onCacheData: onCacheData,
|
||||
onNetworkData: onNetworkData,
|
||||
);
|
||||
}) : _loader = loader,
|
||||
_fromJson = fromJson,
|
||||
super(
|
||||
cacheTime,
|
||||
onUpdate,
|
||||
onError: onError ?? RequestCache.ignore,
|
||||
renew: renew,
|
||||
onCacheData: onCacheData,
|
||||
onNetworkData: onNetworkData,
|
||||
);
|
||||
|
||||
@override
|
||||
Future<T> onLoad() => _loader();
|
||||
|
||||
@override
|
||||
T onLocalData(String json) => _fromJson(jsonDecode(json) as Map<String, dynamic>);
|
||||
T onLocalData(String json) =>
|
||||
_fromJson(jsonDecode(json) as Map<String, dynamic>);
|
||||
}
|
||||
|
||||
/// Captures the latest cache payload (cached or network) and rethrows the
|
||||
@@ -120,24 +126,27 @@ class SimpleCache<T extends ApiResponse?> extends RequestCache<T> {
|
||||
/// `latest`/`capturedError`/`await ready` boilerplate that DataProviders
|
||||
/// otherwise repeat per endpoint.
|
||||
Future<T> resolveFromCache<T extends ApiResponse?>(
|
||||
RequestCache<T> Function(void Function(T) onUpdate, void Function(Exception) onError) build, {
|
||||
RequestCache<T> Function(
|
||||
void Function(T) onUpdate,
|
||||
void Function(Exception) onError,
|
||||
)
|
||||
build, {
|
||||
void Function(Object)? onError,
|
||||
String? operationName,
|
||||
}) async {
|
||||
T? latest;
|
||||
Object? capturedError;
|
||||
final cache = build(
|
||||
(data) => latest = data,
|
||||
(e) {
|
||||
capturedError = e;
|
||||
onError?.call(e);
|
||||
},
|
||||
);
|
||||
final cache = build((data) => latest = data, (e) {
|
||||
capturedError = e;
|
||||
onError?.call(e);
|
||||
});
|
||||
await cache.ready;
|
||||
if (latest != null) return latest as T;
|
||||
final err = capturedError;
|
||||
if (err != null) throw err;
|
||||
throw ParseException(
|
||||
technicalDetails: operationName != null ? 'No data and no error from $operationName' : null,
|
||||
technicalDetails: operationName != null
|
||||
? 'No data and no error from $operationName'
|
||||
: null,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user