feat: add query and infinite query disk caching support
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
import 'package:fl_query/fl_query.dart';
|
||||
import 'package:fl_query/src/base_operation.dart';
|
||||
import 'package:fl_query/src/mixins/autocast.dart';
|
||||
import 'package:fl_query/src/query.dart';
|
||||
import 'package:fl_query/src/utils.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
abstract class BaseQuery<T extends Object, Outside, Error>
|
||||
extends BaseOperation<T, Error> with AutoCast {
|
||||
@@ -79,6 +80,9 @@ abstract class BaseQuery<T extends Object, Outside, Error>
|
||||
if (refetchInterval != null && refetchInterval != Duration.zero) {
|
||||
_refetchIntervalTimer = createRefetchTimer();
|
||||
}
|
||||
if (canCacheToDisk) {
|
||||
loadFromDisk();
|
||||
}
|
||||
}
|
||||
// all getters & setters
|
||||
|
||||
@@ -88,6 +92,33 @@ abstract class BaseQuery<T extends Object, Outside, Error>
|
||||
@protected
|
||||
Timer createRefetchTimer();
|
||||
|
||||
@protected
|
||||
Future<void> loadFromDisk() async {
|
||||
final box = await Hive.lazyBox<String>(kFlQueryBoxKey);
|
||||
final rawData = await box.get(queryKey);
|
||||
if (rawData == null) return;
|
||||
data = deserialize(rawData);
|
||||
if (!isLoading && !isRefetching) {
|
||||
status = QueryStatus.cached;
|
||||
}
|
||||
updatedAt = DateTime.now();
|
||||
await notifyDataListeners();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> saveToDisk({bool delete = false}) async {
|
||||
if (!canCacheToDisk) return;
|
||||
if (!hasData) {
|
||||
if (delete) {
|
||||
final box = await Hive.lazyBox<String>(kFlQueryBoxKey);
|
||||
await box.delete(queryKey);
|
||||
}
|
||||
return;
|
||||
}
|
||||
final box = await Hive.lazyBox<String>(kFlQueryBoxKey);
|
||||
await box.put(queryKey, serialize(data!)!);
|
||||
}
|
||||
|
||||
/// Calls the task function & doesn't check if there's already
|
||||
/// cached data available
|
||||
@protected
|
||||
@@ -95,6 +126,7 @@ abstract class BaseQuery<T extends Object, Outside, Error>
|
||||
try {
|
||||
retryAttempts = 0;
|
||||
await setData();
|
||||
await saveToDisk();
|
||||
_prevUsedExternalData = _externalData;
|
||||
updatedAt = DateTime.now();
|
||||
status = QueryStatus.success;
|
||||
@@ -112,6 +144,7 @@ abstract class BaseQuery<T extends Object, Outside, Error>
|
||||
await Future.delayed(retryDelay);
|
||||
try {
|
||||
await setData();
|
||||
await saveToDisk();
|
||||
_prevUsedExternalData = _externalData;
|
||||
status = QueryStatus.success;
|
||||
await notifyDataListeners();
|
||||
@@ -159,6 +192,15 @@ abstract class BaseQuery<T extends Object, Outside, Error>
|
||||
Future<T?> fetch() async {
|
||||
if (!enabled) return null;
|
||||
|
||||
if (isCachedData) {
|
||||
status = QueryStatus.loading;
|
||||
await execute().then((_) {
|
||||
fetched = true;
|
||||
});
|
||||
notifyListeners();
|
||||
return data;
|
||||
}
|
||||
|
||||
/// if isLoading/isRefetching is true that means its already fetching/
|
||||
/// refetching. So [_execute] again can create a race condition
|
||||
if (isLoading || isRefetching || (hasData && !isPreviousData)) return data;
|
||||
@@ -195,6 +237,15 @@ abstract class BaseQuery<T extends Object, Outside, Error>
|
||||
return await execute().then((_) => data);
|
||||
}
|
||||
|
||||
@protected
|
||||
String? serialize(T data);
|
||||
|
||||
@protected
|
||||
T? deserialize(String rawData);
|
||||
|
||||
@protected
|
||||
bool get canCacheToDisk;
|
||||
|
||||
@protected
|
||||
FutureOr<void> setData();
|
||||
@protected
|
||||
@@ -229,6 +280,7 @@ abstract class BaseQuery<T extends Object, Outside, Error>
|
||||
void reset() {
|
||||
refetchCount = 0;
|
||||
data = _previousData ?? _initialData;
|
||||
saveToDisk(delete: true);
|
||||
error = null;
|
||||
fetched = false;
|
||||
status = QueryStatus.idle;
|
||||
@@ -238,6 +290,12 @@ abstract class BaseQuery<T extends Object, Outside, Error>
|
||||
mounts.clear();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() async {
|
||||
await Hive.openLazyBox(kFlQueryBoxKey).then((box) => box.delete(queryKey));
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
/// Update configurations of the query
|
||||
/// after already creating the Query instance
|
||||
///
|
||||
@@ -314,6 +372,7 @@ abstract class BaseQuery<T extends Object, Outside, Error>
|
||||
final newData = await updateFn(data);
|
||||
if (data == newData) return;
|
||||
data = newData;
|
||||
await saveToDisk();
|
||||
status = QueryStatus.success;
|
||||
notifyListeners();
|
||||
}
|
||||
@@ -347,6 +406,7 @@ abstract class BaseQuery<T extends Object, Outside, Error>
|
||||
bool get isLoading => status == QueryStatus.loading;
|
||||
bool get isRefetching => status == QueryStatus.refetching;
|
||||
bool get isSuccess => status == QueryStatus.success;
|
||||
bool get isCachedData => status == QueryStatus.cached;
|
||||
bool get isPreviousData {
|
||||
return _previousData != null ? _previousData == data : false;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
import 'package:fl_query/fl_query.dart';
|
||||
import 'package:fl_query/src/base_query.dart';
|
||||
import 'package:fl_query/src/models/infinite_query_job.dart';
|
||||
import 'package:fl_query/src/query.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:queue/queue.dart';
|
||||
|
||||
@@ -46,6 +46,12 @@ class InfiniteQuery<T extends Object, Outside, PageParam extends Object>
|
||||
final Set<InfiniteQueryListeners<dynamic, PageParam>> onErrorListeners =
|
||||
Set();
|
||||
|
||||
final SerializeFunction<T>? _serializePage;
|
||||
final DeserializeFunction<T>? _deserializePage;
|
||||
|
||||
final SerializeFunction<PageParam>? serializePageParam;
|
||||
final DeserializeFunction<PageParam>? deserializePageParam;
|
||||
|
||||
InfiniteQuery({
|
||||
required super.queryKey,
|
||||
required this.task,
|
||||
@@ -63,13 +69,21 @@ class InfiniteQuery<T extends Object, Outside, PageParam extends Object>
|
||||
super.previousData,
|
||||
super.connectivity,
|
||||
super.refetchOnApplicationResume,
|
||||
DeserializeFunction<T>? deserialize,
|
||||
SerializeFunction<T>? serialize,
|
||||
InfiniteQueryListeners<T, PageParam>? super.onData,
|
||||
InfiniteQueryListeners<dynamic, PageParam>? super.onError,
|
||||
required T? initialPage,
|
||||
this.getNextPageParam,
|
||||
this.getPreviousPageParam,
|
||||
this.serializePageParam,
|
||||
this.deserializePageParam,
|
||||
}) : _currentParam = initialParam,
|
||||
super(initialData: {initialParam: initialPage});
|
||||
_serializePage = serialize,
|
||||
_deserializePage = deserialize,
|
||||
super(
|
||||
initialData: {initialParam: initialPage},
|
||||
);
|
||||
|
||||
InfiniteQuery.fromOptions(
|
||||
InfiniteQueryJob<T, Outside, PageParam> options, {
|
||||
@@ -80,6 +94,10 @@ class InfiniteQuery<T extends Object, Outside, PageParam extends Object>
|
||||
_currentParam = options.initialParam,
|
||||
getNextPageParam = options.getNextPageParam,
|
||||
getPreviousPageParam = options.getPreviousPageParam,
|
||||
_serializePage = options.serialize,
|
||||
_deserializePage = options.deserialize,
|
||||
serializePageParam = options.serializePageParam,
|
||||
deserializePageParam = options.deserializePageParam,
|
||||
super(
|
||||
cacheTime: options.cacheTime ?? const Duration(minutes: 5),
|
||||
retries: options.retries ?? 3,
|
||||
@@ -273,4 +291,37 @@ class InfiniteQuery<T extends Object, Outside, PageParam extends Object>
|
||||
|
||||
@override
|
||||
bool get hasData => data?[_currentParam] != null;
|
||||
|
||||
@override
|
||||
bool get canCacheToDisk =>
|
||||
_serializePage != null &&
|
||||
_deserializePage != null &&
|
||||
serializePageParam != null &&
|
||||
deserializePageParam != null;
|
||||
|
||||
@override
|
||||
deserialize(String rawData) {
|
||||
if (deserializePageParam == null || _deserializePage == null) return null;
|
||||
return Map.from(jsonDecode(rawData)).cast<String, String>().map(
|
||||
(key, value) {
|
||||
return MapEntry(deserializePageParam!(key), _deserializePage!(value));
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
serialize(data) {
|
||||
if (serializePageParam == null || _serializePage == null) return null;
|
||||
|
||||
data.removeWhere((key, value) => value == null);
|
||||
|
||||
return jsonEncode(data.map(
|
||||
(key, value) {
|
||||
return MapEntry(
|
||||
serializePageParam!(key),
|
||||
_serializePage!(value!),
|
||||
);
|
||||
},
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
import 'package:fl_query/src/infinite_query.dart';
|
||||
import 'package:fl_query/src/models/query_job.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
class InfiniteQueryJob<T extends Object, Outside, PageParam extends Object> {
|
||||
// all params
|
||||
String _queryKey;
|
||||
InfiniteQueryTaskFunction<T, Outside, PageParam> task;
|
||||
SerializeFunction<T>? serialize;
|
||||
DeserializeFunction<T>? deserialize;
|
||||
SerializeFunction<PageParam>? serializePageParam;
|
||||
DeserializeFunction<PageParam>? deserializePageParam;
|
||||
final int? retries;
|
||||
final Duration? retryDelay;
|
||||
T? initialPage;
|
||||
@@ -51,7 +56,23 @@ class InfiniteQueryJob<T extends Object, Outside, PageParam extends Object> {
|
||||
this.refetchOnApplicationResume,
|
||||
this.refetchOnWindowFocus,
|
||||
this.connectivity,
|
||||
}) : _queryKey = queryKey;
|
||||
this.deserialize,
|
||||
this.serialize,
|
||||
this.serializePageParam,
|
||||
this.deserializePageParam,
|
||||
}) : assert(
|
||||
serialize == null &&
|
||||
deserialize == null &&
|
||||
serializePageParam == null &&
|
||||
deserializePageParam == null ||
|
||||
(serialize != null &&
|
||||
deserialize != null &&
|
||||
serializePageParam != null &&
|
||||
deserializePageParam != null &&
|
||||
enabled != false),
|
||||
"All or none of the serialize, deserialize, serializePageParam & deserializePageParam function must be provided. And `enabled` must be true if all of them are provided.",
|
||||
),
|
||||
_queryKey = queryKey;
|
||||
|
||||
String get queryKey => _queryKey;
|
||||
|
||||
@@ -79,6 +100,10 @@ class InfiniteQueryJob<T extends Object, Outside, PageParam extends Object> {
|
||||
bool? refetchOnApplicationResume,
|
||||
bool? refetchOnWindowFocus,
|
||||
Connectivity? connectivity,
|
||||
SerializeFunction<T>? serialize,
|
||||
DeserializeFunction<T>? deserialize,
|
||||
SerializeFunction<PageParam>? serializePageParam,
|
||||
DeserializeFunction<PageParam>? deserializePageParam,
|
||||
}) {
|
||||
return (String queryKey) {
|
||||
if (preQueryKey != null) queryKey = "$preQueryKey#$queryKey";
|
||||
@@ -101,6 +126,10 @@ class InfiniteQueryJob<T extends Object, Outside, PageParam extends Object> {
|
||||
refetchOnWindowFocus: refetchOnWindowFocus,
|
||||
connectivity: connectivity,
|
||||
initialParam: initialParam,
|
||||
serialize: serialize,
|
||||
deserialize: deserialize,
|
||||
serializePageParam: serializePageParam,
|
||||
deserializePageParam: deserializePageParam,
|
||||
);
|
||||
query.isDynamic = true;
|
||||
return query;
|
||||
|
||||
@@ -2,10 +2,15 @@ import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
import 'package:fl_query/src/query.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
typedef SerializeFunction<T extends Object> = String Function(T data);
|
||||
typedef DeserializeFunction<T extends Object> = T Function(String raw);
|
||||
|
||||
class QueryJob<T extends Object, Outside> {
|
||||
// all params
|
||||
String _queryKey;
|
||||
QueryTaskFunction<T, Outside> task;
|
||||
final SerializeFunction<T>? serialize;
|
||||
final DeserializeFunction<T>? deserialize;
|
||||
final int? retries;
|
||||
final Duration? retryDelay;
|
||||
T? initialData;
|
||||
@@ -47,7 +52,14 @@ class QueryJob<T extends Object, Outside> {
|
||||
this.keepPreviousData,
|
||||
this.refetchOnApplicationResume,
|
||||
this.refetchOnWindowFocus,
|
||||
}) : _queryKey = queryKey;
|
||||
this.deserialize,
|
||||
this.serialize,
|
||||
}) : assert(
|
||||
serialize == null && deserialize == null ||
|
||||
(serialize != null && deserialize != null && enabled != false),
|
||||
"Both or none of the serialize and deserialize function must be provided and enabled must be true if you want to use disk caching",
|
||||
),
|
||||
_queryKey = queryKey;
|
||||
|
||||
String get queryKey => _queryKey;
|
||||
|
||||
@@ -73,6 +85,8 @@ class QueryJob<T extends Object, Outside> {
|
||||
bool? refetchOnWindowFocus,
|
||||
Connectivity? connectivity,
|
||||
bool? keepPreviousData,
|
||||
SerializeFunction<T>? serialize,
|
||||
DeserializeFunction<T>? deserialize,
|
||||
}) {
|
||||
return (String queryKey) {
|
||||
if (preQueryKey != null) queryKey = "$preQueryKey#$queryKey";
|
||||
@@ -93,6 +107,8 @@ class QueryJob<T extends Object, Outside> {
|
||||
keepPreviousData: keepPreviousData,
|
||||
refetchOnApplicationResume: refetchOnApplicationResume,
|
||||
refetchOnWindowFocus: refetchOnWindowFocus,
|
||||
serialize: serialize,
|
||||
deserialize: deserialize,
|
||||
);
|
||||
query.isDynamic = true;
|
||||
return query;
|
||||
|
||||
@@ -22,7 +22,10 @@ enum QueryStatus {
|
||||
idle,
|
||||
|
||||
/// when the query is refetching (rerunning)
|
||||
refetching;
|
||||
refetching,
|
||||
|
||||
/// when the query data is loaded from cache
|
||||
cached,
|
||||
}
|
||||
|
||||
typedef QueryTaskFunction<T extends Object, Outside> = FutureOr<T> Function(
|
||||
@@ -42,6 +45,9 @@ class Query<T extends Object, Outside> extends BaseQuery<T, Outside, dynamic> {
|
||||
final Set<QueryListener<T>> onDataListeners = Set();
|
||||
final Set<QueryListener<dynamic>> onErrorListeners = Set();
|
||||
|
||||
final SerializeFunction<T>? _serialize;
|
||||
final DeserializeFunction<T>? _deserialize;
|
||||
|
||||
Query({
|
||||
required super.queryKey,
|
||||
required this.task,
|
||||
@@ -59,9 +65,12 @@ class Query<T extends Object, Outside> extends BaseQuery<T, Outside, dynamic> {
|
||||
super.connectivity,
|
||||
super.initialData,
|
||||
super.refetchOnApplicationResume,
|
||||
SerializeFunction<T>? serialize,
|
||||
DeserializeFunction<T>? deserialize,
|
||||
QueryListener<T>? super.onData,
|
||||
QueryListener<dynamic>? super.onError,
|
||||
});
|
||||
}) : _deserialize = deserialize,
|
||||
_serialize = serialize;
|
||||
|
||||
Query.fromOptions(
|
||||
QueryJob<T, Outside> options, {
|
||||
@@ -70,6 +79,8 @@ class Query<T extends Object, Outside> extends BaseQuery<T, Outside, dynamic> {
|
||||
QueryListener<T>? onData,
|
||||
QueryListener<dynamic>? onError,
|
||||
}) : task = options.task,
|
||||
_deserialize = options.deserialize,
|
||||
_serialize = options.serialize,
|
||||
super(
|
||||
cacheTime: options.cacheTime ?? const Duration(minutes: 5),
|
||||
retries: options.retries ?? 3,
|
||||
@@ -133,4 +144,19 @@ class Query<T extends Object, Outside> extends BaseQuery<T, Outside, dynamic> {
|
||||
void setError(e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
@override
|
||||
deserialize(rawData) {
|
||||
if (_deserialize == null) return null;
|
||||
return _deserialize!(rawData);
|
||||
}
|
||||
|
||||
@override
|
||||
serialize(data) {
|
||||
if (_serialize == null) return null;
|
||||
return _serialize!(data);
|
||||
}
|
||||
|
||||
@override
|
||||
bool get canCacheToDisk => _serialize != null && _deserialize != null;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import 'package:fl_query/src/utils.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
|
||||
/// The widget that holds every [Query] & [Mutation] to your
|
||||
/// entire Flutter application in anywhere
|
||||
|
||||
@@ -40,7 +40,13 @@ class QueryCache {
|
||||
_listenToQueryChanges(Query query) {
|
||||
query.addListener(() {
|
||||
if (query.isInactive) {
|
||||
_queries.removeWhere((el) => el.queryKey != query.queryKey);
|
||||
_queries.removeWhere((el) {
|
||||
if (el.queryKey != query.queryKey) {
|
||||
el.dispose();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
_notifyListeners(CacheEvent.query, null);
|
||||
} else {
|
||||
_notifyListeners(CacheEvent.query, query);
|
||||
@@ -51,8 +57,13 @@ class QueryCache {
|
||||
_listenToInfiniteQueryChanges(InfiniteQuery infiniteQuery) {
|
||||
infiniteQuery.addListener(() {
|
||||
if (infiniteQuery.isInactive) {
|
||||
_infiniteQueries
|
||||
.removeWhere((el) => el.queryKey != infiniteQuery.queryKey);
|
||||
_infiniteQueries.removeWhere((el) {
|
||||
if (el.queryKey != infiniteQuery.queryKey) {
|
||||
el.dispose();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
_notifyListeners(CacheEvent.infiniteQuery, null);
|
||||
} else {
|
||||
_notifyListeners(CacheEvent.infiniteQuery, infiniteQuery);
|
||||
@@ -64,7 +75,13 @@ class QueryCache {
|
||||
mutation.addListener(() {
|
||||
if (mutation.isInactive) {
|
||||
_mutations.removeWhere(
|
||||
(el) => el.mutationKey != mutation.mutationKey,
|
||||
(el) {
|
||||
if (el.mutationKey != mutation.mutationKey) {
|
||||
el.dispose();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
);
|
||||
_notifyListeners(CacheEvent.mutation, null);
|
||||
} else {
|
||||
@@ -92,21 +109,27 @@ class QueryCache {
|
||||
}
|
||||
|
||||
void removeQuery(Query query) {
|
||||
query.dispose();
|
||||
_queries.remove(query);
|
||||
_notifyListeners(CacheEvent.query, null);
|
||||
}
|
||||
|
||||
void removeInfiniteQuery(InfiniteQuery infiniteQuery) {
|
||||
infiniteQuery.dispose();
|
||||
_infiniteQueries.remove(infiniteQuery);
|
||||
_notifyListeners(CacheEvent.infiniteQuery, null);
|
||||
}
|
||||
|
||||
void removeMutation(Mutation mutation) {
|
||||
mutation.dispose();
|
||||
_mutations.remove(mutation);
|
||||
_notifyListeners(CacheEvent.mutation, null);
|
||||
}
|
||||
|
||||
void clearCache() {
|
||||
_infiniteQueries.forEach((el) => el.dispose());
|
||||
_queries.forEach((el) => el.dispose());
|
||||
_mutations.forEach((el) => el.dispose());
|
||||
_infiniteQueries.clear();
|
||||
_queries.clear();
|
||||
_mutations.clear();
|
||||
|
||||
Reference in New Issue
Block a user