refactor(QueryBowl): query bowl logic as a separate class instead of a stateful widget
feat: add QueryCache class for storing, updating, removing all the query/mutation/infiniteQueries refactor(test): update tests according to new query bowl refactor refactor(fl_query_hooks): update the hooks according to new query bowl refactor
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import 'package:fl_query/src/query_bowl.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
abstract class BaseOperation<Data, Error> extends ChangeNotifier {
|
||||
@@ -27,7 +26,7 @@ abstract class BaseOperation<Data, Error> extends ChangeNotifier {
|
||||
/// storage/cache
|
||||
Set<ValueKey<String>> _mounts = {};
|
||||
|
||||
final QueryBowl queryBowl;
|
||||
final queryBowl;
|
||||
|
||||
BaseOperation({
|
||||
required this.cacheTime,
|
||||
|
||||
@@ -254,22 +254,33 @@ abstract class BaseQuery<T extends Object, Outside, Error>
|
||||
bool? refetchOnMount,
|
||||
bool? refetchOnReconnect,
|
||||
}) {
|
||||
bool updated = false;
|
||||
if (this.refetchInterval == null &&
|
||||
refetchInterval != null &&
|
||||
refetchInterval != Duration.zero) {
|
||||
this.refetchInterval = refetchInterval;
|
||||
_refetchIntervalTimer?.cancel();
|
||||
_refetchIntervalTimer = createRefetchTimer();
|
||||
updated = true;
|
||||
}
|
||||
if (this.cacheTime == Duration(minutes: 5) && cacheTime != null)
|
||||
if (this.cacheTime == Duration(minutes: 5) && cacheTime != null) {
|
||||
this.cacheTime = cacheTime;
|
||||
updated = true;
|
||||
}
|
||||
if (this._staleTime == const Duration(milliseconds: 500) &&
|
||||
staleTime != null) this._staleTime = staleTime;
|
||||
if (this.refetchOnMount == null && refetchOnMount != null)
|
||||
staleTime != null) {
|
||||
this._staleTime = staleTime;
|
||||
updated = true;
|
||||
}
|
||||
if (this.refetchOnMount == null && refetchOnMount != null) {
|
||||
this.refetchOnMount = refetchOnMount;
|
||||
if (this.refetchOnReconnect == null && refetchOnReconnect != null)
|
||||
updated = true;
|
||||
}
|
||||
if (this.refetchOnReconnect == null && refetchOnReconnect != null) {
|
||||
this.refetchOnReconnect = refetchOnReconnect;
|
||||
notifyListeners();
|
||||
updated = true;
|
||||
}
|
||||
if (updated) notifyListeners();
|
||||
}
|
||||
|
||||
/// checks if the application is connected to internet in any mean
|
||||
|
||||
@@ -42,11 +42,23 @@ class _InfiniteQueryBuilderState<T extends Object, Outside,
|
||||
void initState() {
|
||||
super.initState();
|
||||
uKey = ValueKey<String>(uuid.v4());
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => init());
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
init();
|
||||
QueryBowl.of(context).onInfiniteQueriesUpdate<T, Outside, PageParam>(
|
||||
(infiniteQuery) {
|
||||
if (infiniteQuery.queryKey != widget.job.queryKey) return;
|
||||
if (mounted)
|
||||
setState(() {
|
||||
this.infiniteQuery = infiniteQuery;
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
void init([T? previousData]) async {
|
||||
final bowl = QueryBowl.of(context);
|
||||
|
||||
infiniteQuery = bowl.addInfiniteQuery<T, Outside, PageParam>(
|
||||
widget.job,
|
||||
externalData: widget.externalData,
|
||||
|
||||
@@ -37,8 +37,6 @@ class MutationBuilder<T extends Object, V> extends StatefulWidget {
|
||||
|
||||
class _MutationBuilderState<T extends Object, V>
|
||||
extends State<MutationBuilder<T, V>> {
|
||||
late QueryBowl queryBowl;
|
||||
|
||||
late ValueKey<String> uKey;
|
||||
|
||||
Mutation<T, V>? mutation;
|
||||
@@ -47,18 +45,32 @@ class _MutationBuilderState<T extends Object, V>
|
||||
void initState() {
|
||||
super.initState();
|
||||
uKey = ValueKey<String>(uuid.v4());
|
||||
WidgetsBinding.instance.addPostFrameCallback(init);
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
init();
|
||||
QueryBowl.of(context).onMutationsUpdate<T, V>(
|
||||
(mutation) {
|
||||
if (mutation.mutationKey != widget.job.mutationKey || !mounted)
|
||||
return;
|
||||
setState(() {
|
||||
this.mutation = mutation;
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
void init([_]) {
|
||||
queryBowl = QueryBowl.of(context);
|
||||
mutation = queryBowl.addMutation<T, V>(
|
||||
widget.job,
|
||||
onData: widget.onData,
|
||||
onError: widget.onError,
|
||||
onMutate: widget.onMutate,
|
||||
key: uKey,
|
||||
);
|
||||
final bowl = QueryBowl.of(context);
|
||||
|
||||
setState(() {
|
||||
mutation = bowl.addMutation<T, V>(
|
||||
widget.job,
|
||||
onData: widget.onData,
|
||||
onError: widget.onError,
|
||||
onMutate: widget.onMutate,
|
||||
key: uKey,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -99,10 +111,7 @@ class _MutationBuilderState<T extends Object, V>
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
queryBowl = QueryBowl.of(context);
|
||||
final latestMutation =
|
||||
queryBowl.getMutation<T, V>(widget.job.mutationKey) ?? mutation;
|
||||
if (latestMutation == null) return Container();
|
||||
return widget.builder(context, latestMutation);
|
||||
if (mutation == null) return Container();
|
||||
return widget.builder(context, mutation!);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,43 +1,35 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:connectivity_plus/connectivity_plus.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/mutation_job.dart';
|
||||
import 'package:fl_query/src/models/query_job.dart';
|
||||
import 'package:fl_query/src/mutation.dart';
|
||||
import 'package:fl_query/src/mutation_builder.dart';
|
||||
import 'package:fl_query/src/query.dart';
|
||||
import 'package:fl_query/fl_query.dart';
|
||||
import 'package:fl_query/src/query_cache.dart';
|
||||
import 'package:fl_query/src/utils.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
/// The widget that holds & provides every [Query] & [Mutation] to your
|
||||
/// The widget that holds every [Query] & [Mutation] to your
|
||||
/// entire Flutter application in anywhere
|
||||
/// This must be used on the above any other widget
|
||||
/// [QueryBowl] provides an imperative way to handle all the query &
|
||||
/// mutation related methods & properties.
|
||||
/// Its responsible or can be used for (not recommended) creating,
|
||||
/// updating & deleting queries & mutations
|
||||
///
|
||||
/// This must be used along with [QueryBowlScope]
|
||||
///
|
||||
/// ```dart
|
||||
/// Widget build(BuildContext context) {
|
||||
/// return QueryBowlScope(
|
||||
/// bowl: QueryBowl(),
|
||||
/// child: MaterialApp(/*...other stuff...*/),
|
||||
/// );
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
class QueryBowlScope extends StatefulWidget {
|
||||
final Widget child;
|
||||
|
||||
class QueryBowl {
|
||||
/// global stale time
|
||||
///
|
||||
/// Makes [Query.data] stale after crossing the duration of provided
|
||||
/// [staleTime]
|
||||
final Duration staleTime;
|
||||
|
||||
/// global cache time
|
||||
///
|
||||
/// Removes inactive queries after provided duration of [cacheTime]
|
||||
final Duration cacheTime;
|
||||
|
||||
// refetching options
|
||||
|
||||
/// refetch query when new query instance mounts
|
||||
@@ -73,10 +65,19 @@ class QueryBowlScope extends StatefulWidget {
|
||||
/// If none is supplied then half of the value of staleTime is used
|
||||
final Duration refetchInterval;
|
||||
|
||||
const QueryBowlScope({
|
||||
required this.child,
|
||||
/// The Cache that holds all the queries, mutations and infinite queries
|
||||
QueryCache cache;
|
||||
|
||||
QueryBowl({
|
||||
/// The Cache that holds all the queries, mutations and infinite
|
||||
/// queries
|
||||
QueryCache? cache,
|
||||
this.staleTime = Duration.zero,
|
||||
this.cacheTime = const Duration(minutes: 5),
|
||||
|
||||
/// global cache time
|
||||
///
|
||||
/// Removes inactive queries after provided duration of [cacheTime]
|
||||
Duration? cacheTime,
|
||||
this.refetchInterval = Duration.zero,
|
||||
this.refetchOnMount = false,
|
||||
this.refetchOnReconnect = true,
|
||||
@@ -84,496 +85,29 @@ class QueryBowlScope extends StatefulWidget {
|
||||
this.refetchOnApplicationResume = true,
|
||||
this.refetchOnWindowFocus = true,
|
||||
this.refetchOnExternalDataChange = false,
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<QueryBowlScope> createState() => _QueryBowlScopeState();
|
||||
}
|
||||
|
||||
class _QueryBowlScopeState extends State<QueryBowlScope> {
|
||||
late Set<InfiniteQuery> infiniteQueries;
|
||||
late Set<Query> queries;
|
||||
late Set<Mutation> mutations;
|
||||
|
||||
StreamSubscription<ConnectivityResult>? _connectionStatusSubscription;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
infiniteQueries = {};
|
||||
queries = {};
|
||||
mutations = {};
|
||||
|
||||
_connectionStatusSubscription = Connectivity()
|
||||
}) : cache = cache ?? QueryCache(cacheTime: cacheTime) {
|
||||
Connectivity()
|
||||
.onConnectivityChanged
|
||||
.listen((ConnectivityResult result) async {
|
||||
if (isConnectedToInternet(result)) {
|
||||
for (final query in queries) {
|
||||
for (final query in this.cache.queries) {
|
||||
if (query.refetchOnReconnect == false || !query.enabled) continue;
|
||||
await query.refetch();
|
||||
await Future.delayed(widget.refetchOnReconnectDelay);
|
||||
await Future.delayed(refetchOnReconnectDelay);
|
||||
}
|
||||
for (final infiniteQuery in infiniteQueries) {
|
||||
for (final infiniteQuery in this.cache.infiniteQueries) {
|
||||
if (infiniteQuery.refetchOnReconnect == false ||
|
||||
!infiniteQuery.enabled) continue;
|
||||
await infiniteQuery.refetchPages();
|
||||
await Future.delayed(widget.refetchOnReconnectDelay);
|
||||
await Future.delayed(refetchOnReconnectDelay);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_disposeUpdateListeners();
|
||||
_connectionStatusSubscription?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _listenToUpdates() {
|
||||
for (final infiniteQuery in infiniteQueries) {
|
||||
infiniteQuery.addListener(() => updateInfiniteQueries(infiniteQuery));
|
||||
}
|
||||
for (final query in queries) {
|
||||
query.addListener(() => updateQueries(query));
|
||||
}
|
||||
for (final mutation in mutations) {
|
||||
mutation.addListener(() => updateMutations(mutation));
|
||||
}
|
||||
}
|
||||
|
||||
void _disposeUpdateListeners() {
|
||||
for (final infiniteQuery in infiniteQueries) {
|
||||
infiniteQuery.removeListener(() => updateInfiniteQueries(infiniteQuery));
|
||||
}
|
||||
for (final query in queries) {
|
||||
query.removeListener(() => updateQueries(query));
|
||||
}
|
||||
for (final mutation in mutations) {
|
||||
mutation.removeListener(() => updateMutations(mutation));
|
||||
}
|
||||
}
|
||||
|
||||
void updateQueries(Query query) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
// checking & not including inactive queries
|
||||
// basically garbage collecting queries
|
||||
setState(() {
|
||||
queries = Set.from(
|
||||
query.isInactive
|
||||
? queries.where((el) => el.queryKey != query.queryKey)
|
||||
: queries,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void updateInfiniteQueries(InfiniteQuery infiniteQuery) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
// checking & not including inactive queries
|
||||
// basically garbage collecting queries
|
||||
setState(() {
|
||||
infiniteQueries = Set.from(
|
||||
infiniteQuery.isInactive
|
||||
? infiniteQueries
|
||||
.where((el) => el.queryKey != infiniteQuery.queryKey)
|
||||
: infiniteQueries,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void updateMutations(Mutation mutation) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
setState(() {
|
||||
// checking & not including inactive mutations
|
||||
// basically garbage collecting mutations
|
||||
mutations = Set.from(
|
||||
mutation.isInactive
|
||||
? mutations.where(
|
||||
(el) => el.mutationKey != mutation.mutationKey,
|
||||
)
|
||||
: mutations,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void addInfiniteQuery<T extends Object, Outside, PageParam extends Object>(
|
||||
InfiniteQuery<T, Outside, PageParam> infiniteQuery) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
setState(() {
|
||||
infiniteQueries = Set.from({...infiniteQueries, infiniteQuery});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void addQuery<T extends Object, Outside>(Query<T, Outside> query) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
setState(() {
|
||||
queries = Set.from({...queries, query});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void addMutation<T extends Object, V>(Mutation<T, V> mutation) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
setState(() {
|
||||
mutations = Set.from({...mutations, mutation});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
int removeQueries(List<String> queryKeys) {
|
||||
int count = 0;
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
setState(() {
|
||||
mutations = Set.from(
|
||||
queries.whereNot((query) {
|
||||
final isAboutToRip = queryKeys.contains(query.queryKey);
|
||||
if (isAboutToRip) count++;
|
||||
return isAboutToRip;
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
return count;
|
||||
}
|
||||
|
||||
void clear() {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
setState(() {
|
||||
queries = Set<Query>();
|
||||
mutations = Set<Mutation>();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
_disposeUpdateListeners();
|
||||
_listenToUpdates();
|
||||
return QueryBowl(
|
||||
addInfiniteQuery: addInfiniteQuery,
|
||||
addQuery: addQuery,
|
||||
addMutation: addMutation,
|
||||
removeQueries: removeQueries,
|
||||
clear: clear,
|
||||
infiniteQueries: infiniteQueries,
|
||||
queries: queries,
|
||||
mutations: mutations,
|
||||
staleTime: widget.staleTime,
|
||||
cacheTime: widget.cacheTime,
|
||||
refetchInterval: widget.refetchInterval,
|
||||
refetchOnMount: widget.refetchOnMount,
|
||||
refetchOnReconnect: widget.refetchOnReconnect,
|
||||
refetchOnExternalDataChange: widget.refetchOnExternalDataChange,
|
||||
child: widget.child,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// QueryBowl provides an imperative way to handle all the query &
|
||||
/// mutation related methods & properties.
|
||||
///
|
||||
/// Its responsible or can be used for (not recommended) creating,
|
||||
/// updating & deleting queries & mutations
|
||||
class QueryBowl extends InheritedWidget {
|
||||
final Set<InfiniteQuery> _infiniteQueries;
|
||||
final Set<Query> _queries;
|
||||
final Set<Mutation> _mutations;
|
||||
final Duration staleTime;
|
||||
final Duration cacheTime;
|
||||
|
||||
final Duration? refetchInterval;
|
||||
final bool refetchOnMount;
|
||||
final bool refetchOnReconnect;
|
||||
final bool refetchOnExternalDataChange;
|
||||
|
||||
final void Function<T extends Object, Outside, PageParam extends Object>(
|
||||
InfiniteQuery<T, Outside, PageParam> infiniteQuery) _addInfiniteQuery;
|
||||
|
||||
final void Function<T extends Object, Outside>(Query<T, Outside> query)
|
||||
_addQuery;
|
||||
|
||||
final void Function<T extends Object, V>(Mutation<T, V> mutation)
|
||||
_addMutation;
|
||||
|
||||
final int Function(List<String>) removeQueries;
|
||||
|
||||
final void Function() clear;
|
||||
|
||||
const QueryBowl({
|
||||
required Widget child,
|
||||
required final void Function<T extends Object, Outside,
|
||||
PageParam extends Object>(
|
||||
InfiniteQuery<T, Outside, PageParam> infiniteQuery)
|
||||
addInfiniteQuery,
|
||||
required final void Function<T extends Object, Outside>(
|
||||
Query<T, Outside> query)
|
||||
addQuery,
|
||||
required final void Function<T extends Object, V>(Mutation<T, V> mutation)
|
||||
addMutation,
|
||||
required final Set<InfiniteQuery> infiniteQueries,
|
||||
required final Set<Query> queries,
|
||||
required final Set<Mutation> mutations,
|
||||
required this.staleTime,
|
||||
required this.cacheTime,
|
||||
required this.removeQueries,
|
||||
required this.clear,
|
||||
required this.refetchOnMount,
|
||||
required this.refetchOnReconnect,
|
||||
required this.refetchOnExternalDataChange,
|
||||
this.refetchInterval,
|
||||
Key? key,
|
||||
}) : _addQuery = addQuery,
|
||||
_queries = queries,
|
||||
_mutations = mutations,
|
||||
_addMutation = addMutation,
|
||||
_addInfiniteQuery = addInfiniteQuery,
|
||||
_infiniteQueries = infiniteQueries,
|
||||
super(child: child, key: key);
|
||||
|
||||
Query<T, Outside> _createQueryWithDefaults<T extends Object, Outside>(
|
||||
QueryJob<T, Outside> options,
|
||||
Outside externalData, [
|
||||
T? previousData,
|
||||
]) {
|
||||
final query = Query<T, Outside>.fromOptions(
|
||||
options,
|
||||
externalData: externalData,
|
||||
previousData: previousData,
|
||||
queryBowl: this,
|
||||
);
|
||||
query.updateDefaultOptions(
|
||||
cacheTime: cacheTime,
|
||||
staleTime: staleTime,
|
||||
refetchInterval: refetchInterval,
|
||||
refetchOnMount: refetchOnMount,
|
||||
refetchOnReconnect: refetchOnReconnect,
|
||||
);
|
||||
return query;
|
||||
}
|
||||
|
||||
InfiniteQuery<T, Outside, PageParam> _createInfiniteQueryWithDefaults<
|
||||
T extends Object, Outside, PageParam extends Object>(
|
||||
InfiniteQueryJob<T, Outside, PageParam> options,
|
||||
Outside externalData,
|
||||
) {
|
||||
final infiniteQuery = InfiniteQuery<T, Outside, PageParam>.fromOptions(
|
||||
options,
|
||||
externalData: externalData,
|
||||
queryBowl: this,
|
||||
);
|
||||
infiniteQuery.updateDefaultOptions(
|
||||
cacheTime: cacheTime,
|
||||
staleTime: staleTime,
|
||||
refetchInterval: refetchInterval,
|
||||
refetchOnMount: refetchOnMount,
|
||||
refetchOnReconnect: refetchOnReconnect,
|
||||
);
|
||||
return infiniteQuery;
|
||||
}
|
||||
|
||||
Future<T?> prefetchQuery<T extends Object, Outside>(
|
||||
QueryJob<T, Outside> options, {
|
||||
required Outside externalData,
|
||||
}) async {
|
||||
final prevQuery =
|
||||
_queries.firstWhereOrNull((q) => q.queryKey == options.queryKey);
|
||||
if (prevQuery != null && prevQuery is Query<T, Outside>)
|
||||
return prevQuery.data;
|
||||
|
||||
final query = _createQueryWithDefaults<T, Outside>(options, externalData);
|
||||
_addQuery<T, Outside>(query);
|
||||
return await query.fetch();
|
||||
}
|
||||
|
||||
/// !⚠️**Warning** only for internal library usage
|
||||
@protected
|
||||
Future<T?> fetchQuery<T extends Object, Outside>(
|
||||
QueryJob<T, Outside> options, {
|
||||
required Outside externalData,
|
||||
final QueryListener<T>? onData,
|
||||
final QueryListener<dynamic>? onError,
|
||||
required ValueKey<String> key,
|
||||
}) async {
|
||||
final prevQuery =
|
||||
_queries.firstWhereOrNull((q) => q.queryKey == options.queryKey);
|
||||
if (prevQuery is Query<T, Outside>) {
|
||||
// run the query if its still not called or if externalData has
|
||||
// changed
|
||||
final hasExternalDataChanged = prevQuery.prevUsedExternalData != null &&
|
||||
externalData != null &&
|
||||
!isShallowEqual(
|
||||
prevQuery.prevUsedExternalData!,
|
||||
externalData,
|
||||
);
|
||||
prevQuery.mount(key);
|
||||
if (onData != null) prevQuery.addDataListener(onData);
|
||||
if (onError != null) prevQuery.addErrorListener(onError);
|
||||
if (!prevQuery.hasData || hasExternalDataChanged) {
|
||||
if (hasExternalDataChanged) prevQuery.setExternalData(externalData);
|
||||
|
||||
return await prevQuery.refetch();
|
||||
}
|
||||
// mounting the widget that is using the query in the prevQuery
|
||||
return prevQuery.data;
|
||||
}
|
||||
|
||||
final query = _createQueryWithDefaults<T, Outside>(options, externalData);
|
||||
query.mount(key);
|
||||
_addQuery<T, Outside>(query);
|
||||
return await query.fetch();
|
||||
}
|
||||
|
||||
@protected
|
||||
InfiniteQuery<T, Outside, PageParam>
|
||||
addInfiniteQuery<T extends Object, Outside, PageParam extends Object>(
|
||||
InfiniteQueryJob<T, Outside, PageParam> infiniteQueryJob, {
|
||||
required Outside externalData,
|
||||
required ValueKey<String> key,
|
||||
final InfiniteQueryListeners<T, PageParam>? onData,
|
||||
final InfiniteQueryListeners<dynamic, PageParam>? onError,
|
||||
}) {
|
||||
final prevInfiniteQuery = _infiniteQueries.firstWhereOrNull(
|
||||
(q) => q.queryKey == infiniteQueryJob.queryKey,
|
||||
);
|
||||
if (prevInfiniteQuery is InfiniteQuery<T, Outside, PageParam>) {
|
||||
// run the query if its still not called or if externalData has
|
||||
// changed
|
||||
if (prevInfiniteQuery.prevUsedExternalData != null &&
|
||||
externalData != null &&
|
||||
!isShallowEqual(
|
||||
prevInfiniteQuery.prevUsedExternalData!,
|
||||
externalData,
|
||||
)) {
|
||||
prevInfiniteQuery.setExternalData(externalData);
|
||||
}
|
||||
prevInfiniteQuery.mount(key);
|
||||
if (onData != null) prevInfiniteQuery.addDataListener(onData);
|
||||
if (onError != null) prevInfiniteQuery.addErrorListener(onError);
|
||||
// mounting the widget that is using the query in the prevQuery
|
||||
return prevInfiniteQuery;
|
||||
}
|
||||
|
||||
final infiniteQuery =
|
||||
_createInfiniteQueryWithDefaults<T, Outside, PageParam>(
|
||||
infiniteQueryJob,
|
||||
externalData,
|
||||
);
|
||||
if (onData != null) infiniteQuery.addDataListener(onData);
|
||||
if (onError != null) infiniteQuery.addErrorListener(onError);
|
||||
infiniteQuery.mount(key);
|
||||
_addInfiniteQuery<T, Outside, PageParam>(infiniteQuery);
|
||||
return infiniteQuery;
|
||||
}
|
||||
|
||||
/// !⚠️**Warning** only for internal library usage
|
||||
@protected
|
||||
Query<T, Outside> addQuery<T extends Object, Outside>(
|
||||
QueryJob<T, Outside> queryJob, {
|
||||
required Outside externalData,
|
||||
required ValueKey<String> key,
|
||||
final QueryListener<T>? onData,
|
||||
final QueryListener<dynamic>? onError,
|
||||
final T? previousData,
|
||||
}) {
|
||||
final prevQuery =
|
||||
_queries.firstWhereOrNull((q) => q.queryKey == queryJob.queryKey);
|
||||
if (prevQuery is Query<T, Outside>) {
|
||||
// run the query if its still not called or if externalData has
|
||||
// changed
|
||||
if (prevQuery.prevUsedExternalData != null &&
|
||||
externalData != null &&
|
||||
!isShallowEqual(
|
||||
prevQuery.prevUsedExternalData!,
|
||||
externalData,
|
||||
)) {
|
||||
prevQuery.setExternalData(externalData);
|
||||
}
|
||||
prevQuery.mount(key);
|
||||
if (onData != null) prevQuery.addDataListener(onData);
|
||||
if (onError != null) prevQuery.addErrorListener(onError);
|
||||
// mounting the widget that is using the query in the prevQuery
|
||||
return prevQuery;
|
||||
}
|
||||
final query = _createQueryWithDefaults<T, Outside>(
|
||||
queryJob,
|
||||
externalData,
|
||||
previousData,
|
||||
);
|
||||
if (onData != null) query.addDataListener(onData);
|
||||
if (onError != null) query.addErrorListener(onError);
|
||||
query.mount(key);
|
||||
_addQuery<T, Outside>(query);
|
||||
return query;
|
||||
}
|
||||
|
||||
/// !⚠️**Warning** only for internal library usage
|
||||
@protected
|
||||
Mutation<T, V> addMutation<T extends Object, V>(
|
||||
MutationJob<T, V> mutationJob, {
|
||||
final MutationListener<T, V>? onData,
|
||||
final MutationListener<dynamic, V>? onError,
|
||||
final MutationListenerReturnable<V, dynamic>? onMutate,
|
||||
required ValueKey<String> key,
|
||||
}) {
|
||||
final prevMutation = _mutations.firstWhereOrNull(
|
||||
(prevMutation) => prevMutation.mutationKey == mutationJob.mutationKey);
|
||||
if (prevMutation != null && prevMutation is Mutation<T, V>) {
|
||||
if (onData != null) prevMutation.addDataListener(onData);
|
||||
if (onError != null) prevMutation.addErrorListener(onError);
|
||||
if (onMutate != null) prevMutation.addMutateListener(onMutate);
|
||||
prevMutation.mount(key);
|
||||
return prevMutation;
|
||||
} else {
|
||||
final mutation = Mutation<T, V>.fromOptions(
|
||||
mutationJob,
|
||||
queryBowl: this,
|
||||
);
|
||||
if (onData != null) mutation.addDataListener(onData);
|
||||
if (onError != null) mutation.addErrorListener(onError);
|
||||
if (onMutate != null) mutation.addMutateListener(onMutate);
|
||||
mutation.updateDefaultOptions(cacheTime: cacheTime);
|
||||
mutation.mount(key);
|
||||
_addMutation(mutation);
|
||||
return mutation;
|
||||
}
|
||||
}
|
||||
|
||||
InfiniteQuery<T, Outside, PageParam>?
|
||||
getInfiniteQuery<T extends Object, Outside, PageParam extends Object>(
|
||||
String queryKey,
|
||||
) {
|
||||
return _infiniteQueries.firstWhereOrNull((infiniteQuery) {
|
||||
return infiniteQuery.queryKey == queryKey &&
|
||||
infiniteQuery is InfiniteQuery<T, Outside, PageParam>;
|
||||
})?.cast<InfiniteQuery<T, Outside, PageParam>>();
|
||||
}
|
||||
|
||||
/// Get a query by providing queryKey only
|
||||
///
|
||||
/// Useful for optimistic update or single query refetch
|
||||
Query<T, Outside>? getQuery<T extends Object, Outside>(String queryKey) {
|
||||
return _queries.firstWhereOrNull((query) {
|
||||
return query.queryKey == queryKey && query is Query<T, Outside>;
|
||||
})?.cast<Query<T, Outside>>();
|
||||
}
|
||||
|
||||
/// Get a mutation by providing mutationKey only
|
||||
///
|
||||
/// Useful for mutation resets
|
||||
Mutation<T, V>? getMutation<T extends Object, V>(String mutationKey) {
|
||||
return _mutations.firstWhereOrNull((mutation) {
|
||||
return mutation.mutationKey == mutationKey && mutation is Mutation<T, V>;
|
||||
})?.cast<Mutation<T, V>>();
|
||||
}
|
||||
|
||||
/// Returns the number of query is currently fetching or refetching
|
||||
int get isFetching {
|
||||
return _queries.fold<int>(
|
||||
return cache.queries.fold<int>(
|
||||
0,
|
||||
(acc, query) {
|
||||
if (query.isLoading || query.isRefetching) acc++;
|
||||
@@ -584,7 +118,7 @@ class QueryBowl extends InheritedWidget {
|
||||
|
||||
/// Provides the number of mutations that are running at the moment
|
||||
int get isMutating {
|
||||
return _mutations.fold<int>(
|
||||
return cache.mutations.fold<int>(
|
||||
0,
|
||||
(acc, mutation) {
|
||||
if (mutation.isLoading) acc++;
|
||||
@@ -593,6 +127,66 @@ class QueryBowl extends InheritedWidget {
|
||||
);
|
||||
}
|
||||
|
||||
void onQueriesUpdate<T extends Object, Outside>(
|
||||
void Function(Query<T, Outside> query) listener,
|
||||
) {
|
||||
cache.on((event, changes) {
|
||||
if (event == CacheEvent.query && changes is Query<T, Outside>) {
|
||||
listener(changes);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void onMutationsUpdate<T extends Object, Outside>(
|
||||
void Function(Mutation<T, Outside> mutation) listener,
|
||||
) {
|
||||
cache.on((event, changes) {
|
||||
if (event == CacheEvent.mutation && changes is Mutation<T, Outside>) {
|
||||
listener(changes);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void onInfiniteQueriesUpdate<T extends Object, Outside,
|
||||
PageParam extends Object>(
|
||||
void Function(InfiniteQuery<T, Outside, PageParam> infiniteQuery) listener,
|
||||
) {
|
||||
cache.on((event, changes) {
|
||||
if (event == CacheEvent.infiniteQuery &&
|
||||
changes is InfiniteQuery<T, Outside, PageParam>) {
|
||||
listener(changes);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
InfiniteQuery<T, Outside, PageParam>?
|
||||
getInfiniteQuery<T extends Object, Outside, PageParam extends Object>(
|
||||
String queryKey,
|
||||
) {
|
||||
return cache.infiniteQueries.firstWhereOrNull((infiniteQuery) {
|
||||
return infiniteQuery.queryKey == queryKey &&
|
||||
infiniteQuery is InfiniteQuery<T, Outside, PageParam>;
|
||||
})?.cast<InfiniteQuery<T, Outside, PageParam>>();
|
||||
}
|
||||
|
||||
/// Get a query by providing queryKey only
|
||||
///
|
||||
/// Useful for optimistic update or single query refetch
|
||||
Query<T, Outside>? getQuery<T extends Object, Outside>(String queryKey) {
|
||||
return cache.queries.firstWhereOrNull((query) {
|
||||
return query.queryKey == queryKey && query is Query<T, Outside>;
|
||||
})?.cast<Query<T, Outside>>();
|
||||
}
|
||||
|
||||
/// Get a mutation by providing mutationKey only
|
||||
///
|
||||
/// Useful for mutation resets
|
||||
Mutation<T, V>? getMutation<T extends Object, V>(String mutationKey) {
|
||||
return cache.mutations.firstWhereOrNull((mutation) {
|
||||
return mutation.mutationKey == mutationKey && mutation is Mutation<T, V>;
|
||||
})?.cast<Mutation<T, V>>();
|
||||
}
|
||||
|
||||
/// Sets [Query]'s data manually
|
||||
///
|
||||
/// Mostly used in combination with [onMutate] callback of
|
||||
@@ -621,7 +215,7 @@ class QueryBowl extends InheritedWidget {
|
||||
/// If an empty list of [queryKeys] is passed then all of the queries
|
||||
/// will be reset
|
||||
void resetQueries(List<String> queryKeys) {
|
||||
for (final query in _queries) {
|
||||
for (final query in cache.queries) {
|
||||
if (queryKeys.isNotEmpty && !queryKeys.contains(query.queryKey)) continue;
|
||||
query.reset();
|
||||
}
|
||||
@@ -632,7 +226,7 @@ class QueryBowl extends InheritedWidget {
|
||||
/// If an empty list of [queryKeys] is passed then all of the queries
|
||||
/// will be invalidated
|
||||
void invalidateQueries(List<String> queryKeys) {
|
||||
for (final query in _queries) {
|
||||
for (final query in cache.queries) {
|
||||
if (queryKeys.isNotEmpty && queryKeys.contains(query.queryKey)) continue;
|
||||
query.invalidate();
|
||||
}
|
||||
@@ -643,22 +237,252 @@ class QueryBowl extends InheritedWidget {
|
||||
/// If an empty list of [queryKeys] is passed then all of the queries
|
||||
/// will be refetched
|
||||
Future<void> refetchQueries(List<String> queryKeys) async {
|
||||
for (final query in _queries) {
|
||||
for (final query in cache.queries) {
|
||||
if (queryKeys.isNotEmpty && queryKeys.contains(query.queryKey)) continue;
|
||||
await query.refetch();
|
||||
}
|
||||
}
|
||||
|
||||
static QueryBowl of(BuildContext context) =>
|
||||
context.dependOnInheritedWidgetOfExactType<QueryBowl>()!;
|
||||
/// Removes all the queries matching the passed List of queryKeys
|
||||
/// from the [QueryCache]
|
||||
int removeQueries(List<String> queryKeys) {
|
||||
int count = 0;
|
||||
for (final query in cache.queries) {
|
||||
if (queryKeys.isEmpty || !queryKeys.contains(query.queryKey)) continue;
|
||||
cache.removeQuery(query);
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/// !⚠️**Warning** only for internal library usage
|
||||
@override
|
||||
@protected
|
||||
bool updateShouldNotify(QueryBowl oldWidget) {
|
||||
return oldWidget.staleTime != staleTime ||
|
||||
oldWidget._queries != _queries ||
|
||||
oldWidget._mutations != _mutations ||
|
||||
oldWidget._infiniteQueries != _infiniteQueries;
|
||||
Query<T, Outside> _createQueryWithDefaults<T extends Object, Outside>(
|
||||
QueryJob<T, Outside> options,
|
||||
Outside externalData, [
|
||||
T? previousData,
|
||||
]) {
|
||||
final query = Query<T, Outside>.fromOptions(
|
||||
options,
|
||||
externalData: externalData,
|
||||
previousData: previousData,
|
||||
queryBowl: this as dynamic,
|
||||
);
|
||||
query.updateDefaultOptions(
|
||||
cacheTime: cache.cacheTime,
|
||||
staleTime: staleTime,
|
||||
refetchInterval: refetchInterval,
|
||||
refetchOnMount: refetchOnMount,
|
||||
refetchOnReconnect: refetchOnReconnect,
|
||||
);
|
||||
return query;
|
||||
}
|
||||
|
||||
InfiniteQuery<T, Outside, PageParam> _createInfiniteQueryWithDefaults<
|
||||
T extends Object, Outside, PageParam extends Object>(
|
||||
InfiniteQueryJob<T, Outside, PageParam> options,
|
||||
Outside externalData,
|
||||
) {
|
||||
final infiniteQuery = InfiniteQuery<T, Outside, PageParam>.fromOptions(
|
||||
options,
|
||||
externalData: externalData,
|
||||
queryBowl: this as dynamic,
|
||||
);
|
||||
infiniteQuery.updateDefaultOptions(
|
||||
cacheTime: cache.cacheTime,
|
||||
staleTime: staleTime,
|
||||
refetchInterval: refetchInterval,
|
||||
refetchOnMount: refetchOnMount,
|
||||
refetchOnReconnect: refetchOnReconnect,
|
||||
);
|
||||
return infiniteQuery;
|
||||
}
|
||||
|
||||
/// Creates/Updates a [Query] with the provided [QueryJob] and it's
|
||||
/// [externalData] and listeners and mounts the [QueryBuilder] or
|
||||
/// [useQuery] for the Query
|
||||
Query<T, Outside> addQuery<T extends Object, Outside>(
|
||||
QueryJob<T, Outside> queryJob, {
|
||||
required Outside externalData,
|
||||
required ValueKey<String> key,
|
||||
final QueryListener<T>? onData,
|
||||
final QueryListener<dynamic>? onError,
|
||||
final T? previousData,
|
||||
}) {
|
||||
final prevQuery = getQuery<T, Outside>(queryJob.queryKey);
|
||||
if (prevQuery != null) {
|
||||
// run the query if its still not called or if externalData has
|
||||
// changed
|
||||
if (prevQuery.prevUsedExternalData != null &&
|
||||
externalData != null &&
|
||||
!isShallowEqual(
|
||||
prevQuery.prevUsedExternalData!,
|
||||
externalData,
|
||||
)) {
|
||||
prevQuery.setExternalData(externalData);
|
||||
}
|
||||
prevQuery.mount(key);
|
||||
if (onData != null) prevQuery.addDataListener(onData);
|
||||
if (onError != null) prevQuery.addErrorListener(onError);
|
||||
// mounting the widget that is using the query in the prevQuery
|
||||
return prevQuery;
|
||||
}
|
||||
final query = _createQueryWithDefaults<T, Outside>(
|
||||
queryJob,
|
||||
externalData,
|
||||
previousData,
|
||||
);
|
||||
if (onData != null) query.addDataListener(onData);
|
||||
if (onError != null) query.addErrorListener(onError);
|
||||
query.mount(key);
|
||||
cache.addQuery(query);
|
||||
return query;
|
||||
}
|
||||
|
||||
/// Creates/Updates a [InfiniteQuery] with the provided
|
||||
/// [InfiniteQueryJob] and it's [externalData] and listeners and mounts
|
||||
/// the [InfiniteQueryBuilder] or [useInfiniteQuery] for the it
|
||||
InfiniteQuery<T, Outside, PageParam>
|
||||
addInfiniteQuery<T extends Object, Outside, PageParam extends Object>(
|
||||
InfiniteQueryJob<T, Outside, PageParam> infiniteQueryJob, {
|
||||
required Outside externalData,
|
||||
required ValueKey<String> key,
|
||||
final InfiniteQueryListeners<T, PageParam>? onData,
|
||||
final InfiniteQueryListeners<dynamic, PageParam>? onError,
|
||||
}) {
|
||||
final prevInfiniteQuery =
|
||||
getInfiniteQuery<T, Outside, PageParam>(infiniteQueryJob.queryKey);
|
||||
if (prevInfiniteQuery != null) {
|
||||
// run the query if its still not called or if externalData has
|
||||
// changed
|
||||
if (prevInfiniteQuery.prevUsedExternalData != null &&
|
||||
externalData != null &&
|
||||
!isShallowEqual(
|
||||
prevInfiniteQuery.prevUsedExternalData!,
|
||||
externalData,
|
||||
)) {
|
||||
prevInfiniteQuery.setExternalData(externalData);
|
||||
}
|
||||
prevInfiniteQuery.mount(key);
|
||||
if (onData != null) prevInfiniteQuery.addDataListener(onData);
|
||||
if (onError != null) prevInfiniteQuery.addErrorListener(onError);
|
||||
// mounting the widget that is using the query in the prevQuery
|
||||
return prevInfiniteQuery;
|
||||
}
|
||||
|
||||
final infiniteQuery =
|
||||
_createInfiniteQueryWithDefaults<T, Outside, PageParam>(
|
||||
infiniteQueryJob,
|
||||
externalData,
|
||||
);
|
||||
if (onData != null) infiniteQuery.addDataListener(onData);
|
||||
if (onError != null) infiniteQuery.addErrorListener(onError);
|
||||
infiniteQuery.mount(key);
|
||||
cache.addInfiniteQuery(infiniteQuery);
|
||||
return infiniteQuery;
|
||||
}
|
||||
|
||||
/// Creates/Updates a [Mutation] with the provided
|
||||
/// [MutationJob] and it's listeners. Mounts
|
||||
/// the [MutationBuilder] or [useMutation] for the it
|
||||
Mutation<T, V> addMutation<T extends Object, V>(
|
||||
MutationJob<T, V> mutationJob, {
|
||||
final MutationListener<T, V>? onData,
|
||||
final MutationListener<dynamic, V>? onError,
|
||||
final MutationListenerReturnable<V, dynamic>? onMutate,
|
||||
required ValueKey<String> key,
|
||||
}) {
|
||||
final prevMutation = getMutation<T, V>(mutationJob.mutationKey);
|
||||
if (prevMutation != null) {
|
||||
if (onData != null) prevMutation.addDataListener(onData);
|
||||
if (onError != null) prevMutation.addErrorListener(onError);
|
||||
if (onMutate != null) prevMutation.addMutateListener(onMutate);
|
||||
prevMutation.mount(key);
|
||||
return prevMutation;
|
||||
} else {
|
||||
final mutation = Mutation<T, V>.fromOptions(
|
||||
mutationJob,
|
||||
queryBowl: this as dynamic,
|
||||
);
|
||||
if (onData != null) mutation.addDataListener(onData);
|
||||
if (onError != null) mutation.addErrorListener(onError);
|
||||
if (onMutate != null) mutation.addMutateListener(onMutate);
|
||||
mutation.updateDefaultOptions(cacheTime: cache.cacheTime);
|
||||
mutation.mount(key);
|
||||
cache.addMutation(mutation);
|
||||
return mutation;
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates/Updates a [Query] with the provided [QueryJob] and it's
|
||||
/// [externalData] and listeners and mounts the [QueryBuilder] or
|
||||
/// [useQuery] for the Query
|
||||
///
|
||||
/// It also fetches/refetches the [Query] strategically/based on changes
|
||||
Future<T?> fetchQuery<T extends Object, Outside>(
|
||||
QueryJob<T, Outside> options, {
|
||||
required Outside externalData,
|
||||
final QueryListener<T>? onData,
|
||||
final QueryListener<dynamic>? onError,
|
||||
required ValueKey<String> key,
|
||||
}) async {
|
||||
final prevQuery = getQuery<T, Outside>(options.queryKey);
|
||||
if (prevQuery != null) {
|
||||
// run the query if its still not called or if externalData has
|
||||
// changed
|
||||
final hasExternalDataChanged = prevQuery.prevUsedExternalData != null &&
|
||||
externalData != null &&
|
||||
!isShallowEqual(
|
||||
prevQuery.prevUsedExternalData!,
|
||||
externalData,
|
||||
);
|
||||
prevQuery.mount(key);
|
||||
if (onData != null) prevQuery.addDataListener(onData);
|
||||
if (onError != null) prevQuery.addErrorListener(onError);
|
||||
if (!prevQuery.hasData || hasExternalDataChanged) {
|
||||
if (hasExternalDataChanged) prevQuery.setExternalData(externalData);
|
||||
|
||||
return await prevQuery.refetch();
|
||||
}
|
||||
// mounting the widget that is using the query in the prevQuery
|
||||
return prevQuery.data;
|
||||
}
|
||||
|
||||
final query = _createQueryWithDefaults<T, Outside>(
|
||||
options,
|
||||
externalData,
|
||||
);
|
||||
|
||||
query.mount(key);
|
||||
cache.addQuery(query);
|
||||
return await query.fetch();
|
||||
}
|
||||
|
||||
/// Finds the closest instance of [QueryBowl] for the provided
|
||||
/// [BuildContext]
|
||||
static QueryBowl of(BuildContext context) {
|
||||
return context.dependOnInheritedWidgetOfExactType<QueryBowlScope>()!.bowl;
|
||||
}
|
||||
}
|
||||
|
||||
/// A simple [InheritedWidget] that does the job of injecting [QueryBowl]
|
||||
/// into context
|
||||
///
|
||||
/// ```dart
|
||||
/// Widget build(BuildContext context) {
|
||||
/// return QueryBowlScope(
|
||||
/// bowl: QueryBowl(),
|
||||
/// child: MaterialApp(/*...other stuff...*/),
|
||||
/// );
|
||||
/// }
|
||||
/// ```
|
||||
class QueryBowlScope extends InheritedWidget {
|
||||
final QueryBowl bowl;
|
||||
|
||||
QueryBowlScope({
|
||||
required this.bowl,
|
||||
required Widget child,
|
||||
Key? key,
|
||||
}) : super(key: key, child: child);
|
||||
|
||||
@override
|
||||
bool updateShouldNotify(covariant oldWidget) => false;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
// ignore_for_file: invalid_use_of_protected_member
|
||||
|
||||
import 'package:fl_query/src/models/query_job.dart';
|
||||
import 'package:fl_query/src/query.dart';
|
||||
import 'package:fl_query/src/query_bowl.dart';
|
||||
import 'package:fl_query/src/utils.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
|
||||
class QueryBuilder<T extends Object, Outside> extends StatefulWidget {
|
||||
final Function(BuildContext context, Query<T, Outside> query) builder;
|
||||
final Widget Function(BuildContext context, Query<T, Outside> query) builder;
|
||||
final QueryJob<T, Outside> job;
|
||||
final Outside externalData;
|
||||
|
||||
@@ -28,21 +26,34 @@ class QueryBuilder<T extends Object, Outside> extends StatefulWidget {
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<QueryBuilder<T, Outside>> createState() =>
|
||||
_QueryBuilderState<T, Outside> createState() =>
|
||||
_QueryBuilderState<T, Outside>();
|
||||
}
|
||||
|
||||
class _QueryBuilderState<T extends Object, Outside>
|
||||
extends State<QueryBuilder<T, Outside>> {
|
||||
late QueryBowl queryBowl;
|
||||
late final ValueKey<String> uKey;
|
||||
Query<T, Outside>? query;
|
||||
late final ValueKey<String> uKey;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
uKey = ValueKey<String>(uuid.v4());
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => init());
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
init();
|
||||
|
||||
QueryBowl.of(context).onQueriesUpdate<T, Outside>(
|
||||
(query) {
|
||||
if (query.queryKey != widget.job.queryKey) return;
|
||||
if (mounted)
|
||||
setState(() {
|
||||
this.query = query;
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
void init([T? previousData]) async {
|
||||
@@ -55,6 +66,7 @@ class _QueryBuilderState<T extends Object, Outside>
|
||||
onData: widget.onData,
|
||||
onError: widget.onError,
|
||||
);
|
||||
|
||||
final hasExternalDataChanged = query!.externalData != null &&
|
||||
query!.prevUsedExternalData != null &&
|
||||
!isShallowEqual(
|
||||
@@ -71,6 +83,7 @@ class _QueryBuilderState<T extends Object, Outside>
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant oldWidget) {
|
||||
final bowl = QueryBowl.of(context);
|
||||
final hasOnErrorChanged =
|
||||
oldWidget.onError != widget.onError && oldWidget.onError != null;
|
||||
final hasOnDataChanged =
|
||||
@@ -94,8 +107,8 @@ class _QueryBuilderState<T extends Object, Outside>
|
||||
widget.externalData != null &&
|
||||
!isShallowEqual(oldWidget.externalData!, widget.externalData!)) {
|
||||
if (widget.job.refetchOnExternalDataChange ??
|
||||
queryBowl.refetchOnExternalDataChange) {
|
||||
QueryBowl.of(context).fetchQuery(
|
||||
bowl.refetchOnExternalDataChange) {
|
||||
bowl.fetchQuery(
|
||||
widget.job,
|
||||
externalData: widget.externalData,
|
||||
onData: widget.onData,
|
||||
@@ -103,7 +116,7 @@ class _QueryBuilderState<T extends Object, Outside>
|
||||
key: uKey,
|
||||
);
|
||||
} else {
|
||||
QueryBowl.of(context)
|
||||
bowl
|
||||
.getQuery(widget.job.queryKey)
|
||||
?.setExternalData(widget.externalData);
|
||||
}
|
||||
@@ -136,10 +149,7 @@ class _QueryBuilderState<T extends Object, Outside>
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
queryBowl = QueryBowl.of(context);
|
||||
final latestQuery =
|
||||
queryBowl.getQuery<T, Outside>(widget.job.queryKey) ?? query;
|
||||
if (latestQuery == null) return Container();
|
||||
return widget.builder(context, latestQuery);
|
||||
if (query == null) return Container();
|
||||
return widget.builder(context, query!);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:fl_query/fl_query.dart';
|
||||
import 'package:fl_query/src/base_operation.dart';
|
||||
|
||||
typedef ReadonlySet<E> = UnmodifiableSetView<E>;
|
||||
|
||||
enum CacheEvent {
|
||||
clearCache,
|
||||
query,
|
||||
infiniteQuery,
|
||||
mutation,
|
||||
}
|
||||
|
||||
typedef CacheUpdateListener<T> = void Function(CacheEvent event, T? changes);
|
||||
|
||||
class QueryCache {
|
||||
/// Removes inactive queries after provided duration of [cacheTime]
|
||||
final Duration cacheTime;
|
||||
|
||||
QueryCache({Duration? cacheTime})
|
||||
: cacheTime = cacheTime ?? const Duration(minutes: 5) {}
|
||||
|
||||
final Set<Query> _queries = {};
|
||||
final Set<InfiniteQuery> _infiniteQueries = {};
|
||||
final Set<Mutation> _mutations = {};
|
||||
|
||||
final Set<CacheUpdateListener<BaseOperation>> _listeners = {};
|
||||
|
||||
ReadonlySet<Query> get queries => UnmodifiableSetView(_queries);
|
||||
ReadonlySet<InfiniteQuery> get infiniteQueries =>
|
||||
UnmodifiableSetView(_infiniteQueries);
|
||||
ReadonlySet<Mutation> get mutations => UnmodifiableSetView(_mutations);
|
||||
|
||||
ReadonlySet<CacheUpdateListener<BaseOperation>> get listeners =>
|
||||
UnmodifiableSetView(_listeners);
|
||||
|
||||
_notifyListeners<T extends Object>(CacheEvent event, changes) {
|
||||
_listeners.forEach((listener) => listener(event, changes));
|
||||
}
|
||||
|
||||
_listenToQueryChanges(Query query) {
|
||||
query.addListener(() {
|
||||
if (query.isInactive) {
|
||||
_queries.removeWhere((el) => el.queryKey != query.queryKey);
|
||||
_notifyListeners(CacheEvent.query, null);
|
||||
} else {
|
||||
_notifyListeners(CacheEvent.query, query);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
_listenToInfiniteQueryChanges(InfiniteQuery infiniteQuery) {
|
||||
infiniteQuery.addListener(() {
|
||||
if (infiniteQuery.isInactive) {
|
||||
_infiniteQueries
|
||||
.removeWhere((el) => el.queryKey != infiniteQuery.queryKey);
|
||||
_notifyListeners(CacheEvent.infiniteQuery, null);
|
||||
} else {
|
||||
_notifyListeners(CacheEvent.infiniteQuery, infiniteQuery);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
_listenToMutationChanges(Mutation mutation) {
|
||||
mutation.addListener(() {
|
||||
if (mutation.isInactive) {
|
||||
_mutations.removeWhere(
|
||||
(el) => el.mutationKey != mutation.mutationKey,
|
||||
);
|
||||
_notifyListeners(CacheEvent.mutation, null);
|
||||
} else {
|
||||
_notifyListeners(CacheEvent.mutation, mutation);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void addQuery(Query query) {
|
||||
_queries.add(query);
|
||||
_listenToQueryChanges(query);
|
||||
_notifyListeners(CacheEvent.query, query);
|
||||
}
|
||||
|
||||
void addInfiniteQuery(InfiniteQuery infiniteQuery) {
|
||||
_infiniteQueries.add(infiniteQuery);
|
||||
_listenToInfiniteQueryChanges(infiniteQuery);
|
||||
_notifyListeners(CacheEvent.infiniteQuery, infiniteQuery);
|
||||
}
|
||||
|
||||
void addMutation(Mutation mutation) {
|
||||
_mutations.add(mutation);
|
||||
_listenToMutationChanges(mutation);
|
||||
_notifyListeners(CacheEvent.mutation, mutation);
|
||||
}
|
||||
|
||||
void removeQuery(Query query) {
|
||||
_queries.remove(query);
|
||||
_notifyListeners(CacheEvent.query, null);
|
||||
}
|
||||
|
||||
void removeInfiniteQuery(InfiniteQuery infiniteQuery) {
|
||||
_infiniteQueries.remove(infiniteQuery);
|
||||
_notifyListeners(CacheEvent.infiniteQuery, null);
|
||||
}
|
||||
|
||||
void removeMutation(Mutation mutation) {
|
||||
_mutations.remove(mutation);
|
||||
_notifyListeners(CacheEvent.mutation, null);
|
||||
}
|
||||
|
||||
void clearCache() {
|
||||
_infiniteQueries.clear();
|
||||
_queries.clear();
|
||||
_mutations.clear();
|
||||
_notifyListeners(CacheEvent.clearCache, null);
|
||||
}
|
||||
|
||||
void on(CacheUpdateListener<BaseOperation?> listener) {
|
||||
_listeners.add(listener);
|
||||
}
|
||||
|
||||
void off(CacheUpdateListener<BaseOperation?> listener) {
|
||||
_listeners.remove(listener);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user