feat(infinite-query): add setInfiniteQueryData support
This commit is contained in:
@@ -268,6 +268,23 @@ abstract class BaseQuery<T extends Object, Outside, Error>
|
|||||||
return isConnectedToInternet(await _connectivity.checkConnectivity());
|
return isConnectedToInternet(await _connectivity.checkConnectivity());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// can be used to update the data manually. Can be useful when used
|
||||||
|
/// together with mutations to perform optimistic updates or manual data
|
||||||
|
/// updates
|
||||||
|
/// For updating particular queries after a mutation using the
|
||||||
|
/// `QueryBowl.refetchQueries` is more appropriate. But this one can be
|
||||||
|
/// used when only 1 query needs get updated
|
||||||
|
///
|
||||||
|
/// Every time a new instance of data should be returned because of
|
||||||
|
/// immutability
|
||||||
|
void setQueryData(QueryUpdateFunction<T> updateFn) async {
|
||||||
|
final newData = await updateFn(data);
|
||||||
|
if (data == newData) return;
|
||||||
|
data = newData;
|
||||||
|
status = QueryStatus.success;
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
/// invalidates the query
|
/// invalidates the query
|
||||||
///
|
///
|
||||||
/// Forcefully makes the query stale & expired which results in a refetch
|
/// Forcefully makes the query stale & expired which results in a refetch
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
// ignore_for_file: invalid_use_of_protected_member
|
||||||
|
|
||||||
import 'package:fl_query/src/infinite_query.dart';
|
import 'package:fl_query/src/infinite_query.dart';
|
||||||
import 'package:fl_query/src/models/infinite_query_job.dart';
|
import 'package:fl_query/src/models/infinite_query_job.dart';
|
||||||
import 'package:fl_query/src/query_bowl.dart';
|
import 'package:fl_query/src/query_bowl.dart';
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
// ignore_for_file: invalid_use_of_protected_member
|
||||||
|
|
||||||
import 'package:fl_query/src/models/mutation_job.dart';
|
import 'package:fl_query/src/models/mutation_job.dart';
|
||||||
import 'package:fl_query/src/mutation.dart';
|
import 'package:fl_query/src/mutation.dart';
|
||||||
import 'package:fl_query/src/query_bowl.dart';
|
import 'package:fl_query/src/query_bowl.dart';
|
||||||
|
|||||||
@@ -97,23 +97,6 @@ class Query<T extends Object, Outside> extends BaseQuery<T, Outside, dynamic> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// can be used to update the data manually. Can be useful when used
|
|
||||||
/// together with mutations to perform optimistic updates or manual data
|
|
||||||
/// updates
|
|
||||||
/// For updating particular queries after a mutation using the
|
|
||||||
/// `QueryBowl.refetchQueries` is more appropriate. But this one can be
|
|
||||||
/// used when only 1 query needs get updated
|
|
||||||
///
|
|
||||||
/// Every time a new instance of data should be returned because of
|
|
||||||
/// immutability
|
|
||||||
void setQueryData(QueryUpdateFunction<T> updateFn) async {
|
|
||||||
final newData = await updateFn(data);
|
|
||||||
if (data == newData) return;
|
|
||||||
data = newData;
|
|
||||||
status = QueryStatus.success;
|
|
||||||
notifyListeners();
|
|
||||||
}
|
|
||||||
|
|
||||||
String get debugLabel => "Query($queryKey)";
|
String get debugLabel => "Query($queryKey)";
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@@ -598,10 +598,24 @@ class QueryBowl extends InheritedWidget {
|
|||||||
/// Mostly used in combination with [onMutate] callback of
|
/// Mostly used in combination with [onMutate] callback of
|
||||||
/// [MutationBuilder] & [useMutation] for optimistic updates
|
/// [MutationBuilder] & [useMutation] for optimistic updates
|
||||||
void setQueryData<T extends Object, Outside>(
|
void setQueryData<T extends Object, Outside>(
|
||||||
String queryKey, QueryUpdateFunction<T> updateCb) {
|
String queryKey,
|
||||||
|
QueryUpdateFunction<T> updateCb,
|
||||||
|
) {
|
||||||
getQuery<T, Outside>(queryKey)?.setQueryData(updateCb);
|
getQuery<T, Outside>(queryKey)?.setQueryData(updateCb);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets [InfiniteQuery]'s data manually
|
||||||
|
///
|
||||||
|
/// Mostly used in combination with [onMutate] callback of
|
||||||
|
/// [MutationBuilder] & [useMutation] for optimistic updates
|
||||||
|
void
|
||||||
|
setInfiniteQueryData<T extends Object, Outside, PageParam extends Object>(
|
||||||
|
String queryKey,
|
||||||
|
QueryUpdateFunction<Map<PageParam, T?>> updateCb,
|
||||||
|
) {
|
||||||
|
getInfiniteQuery<T, Outside, PageParam>(queryKey)?.setQueryData(updateCb);
|
||||||
|
}
|
||||||
|
|
||||||
/// resets all the queries matching the passed List of queryKeys
|
/// resets all the queries matching the passed List of queryKeys
|
||||||
///
|
///
|
||||||
/// If an empty list of [queryKeys] is passed then all of the queries
|
/// If an empty list of [queryKeys] is passed then all of the queries
|
||||||
|
|||||||
Reference in New Issue
Block a user