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:
Kingkor Roy Tirtho
2022-09-22 22:03:28 +06:00
parent f435f101c4
commit 2b835505a3
22 changed files with 804 additions and 733 deletions
@@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"connectivity_plus","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus-2.3.6+1/","native_build":true,"dependencies":[]}],"android":[{"name":"connectivity_plus","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus-2.3.6+1/","native_build":true,"dependencies":[]}],"macos":[{"name":"connectivity_plus_macos","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_macos-1.2.4/","native_build":true,"dependencies":[]}],"linux":[{"name":"connectivity_plus_linux","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_linux-1.3.1/","native_build":false,"dependencies":[]}],"windows":[{"name":"connectivity_plus_windows","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_windows-1.2.2/","native_build":true,"dependencies":[]}],"web":[{"name":"connectivity_plus_web","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_web-1.2.3/","dependencies":[]}]},"dependencyGraph":[{"name":"connectivity_plus","dependencies":["connectivity_plus_linux","connectivity_plus_macos","connectivity_plus_web","connectivity_plus_windows"]},{"name":"connectivity_plus_linux","dependencies":[]},{"name":"connectivity_plus_macos","dependencies":[]},{"name":"connectivity_plus_web","dependencies":[]},{"name":"connectivity_plus_windows","dependencies":[]}],"date_created":"2022-09-14 11:35:43.874010","version":"3.3.0"}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"connectivity_plus","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus-2.3.6+1/","native_build":true,"dependencies":[]}],"android":[{"name":"connectivity_plus","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus-2.3.6+1/","native_build":true,"dependencies":[]}],"macos":[{"name":"connectivity_plus_macos","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_macos-1.2.4/","native_build":true,"dependencies":[]}],"linux":[{"name":"connectivity_plus_linux","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_linux-1.3.1/","native_build":false,"dependencies":[]}],"windows":[{"name":"connectivity_plus_windows","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_windows-1.2.2/","native_build":true,"dependencies":[]}],"web":[{"name":"connectivity_plus_web","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_web-1.2.3/","dependencies":[]}]},"dependencyGraph":[{"name":"connectivity_plus","dependencies":["connectivity_plus_linux","connectivity_plus_macos","connectivity_plus_web","connectivity_plus_windows"]},{"name":"connectivity_plus_linux","dependencies":[]},{"name":"connectivity_plus_macos","dependencies":[]},{"name":"connectivity_plus_web","dependencies":[]},{"name":"connectivity_plus_windows","dependencies":[]}],"date_created":"2022-09-22 21:58:17.052046","version":"3.3.0"}
@@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"connectivity_plus","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus-2.3.6/","native_build":true,"dependencies":[]}],"android":[{"name":"connectivity_plus","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus-2.3.6/","native_build":true,"dependencies":[]}],"macos":[{"name":"connectivity_plus_macos","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_macos-1.2.4/","native_build":true,"dependencies":[]}],"linux":[{"name":"connectivity_plus_linux","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_linux-1.3.1/","native_build":false,"dependencies":[]}],"windows":[{"name":"connectivity_plus_windows","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_windows-1.2.2/","native_build":true,"dependencies":[]}],"web":[{"name":"connectivity_plus_web","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_web-1.2.3/","dependencies":[]}]},"dependencyGraph":[{"name":"connectivity_plus","dependencies":["connectivity_plus_linux","connectivity_plus_macos","connectivity_plus_web","connectivity_plus_windows"]},{"name":"connectivity_plus_linux","dependencies":[]},{"name":"connectivity_plus_macos","dependencies":[]},{"name":"connectivity_plus_web","dependencies":[]},{"name":"connectivity_plus_windows","dependencies":[]}],"date_created":"2022-09-19 10:39:45.071010","version":"3.3.0"}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"connectivity_plus","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus-2.3.6/","native_build":true,"dependencies":[]}],"android":[{"name":"connectivity_plus","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus-2.3.6/","native_build":true,"dependencies":[]}],"macos":[{"name":"connectivity_plus_macos","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_macos-1.2.4/","native_build":true,"dependencies":[]}],"linux":[{"name":"connectivity_plus_linux","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_linux-1.3.1/","native_build":false,"dependencies":[]}],"windows":[{"name":"connectivity_plus_windows","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_windows-1.2.2/","native_build":true,"dependencies":[]}],"web":[{"name":"connectivity_plus_web","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_web-1.2.3/","dependencies":[]}]},"dependencyGraph":[{"name":"connectivity_plus","dependencies":["connectivity_plus_linux","connectivity_plus_macos","connectivity_plus_web","connectivity_plus_windows"]},{"name":"connectivity_plus_linux","dependencies":[]},{"name":"connectivity_plus_macos","dependencies":[]},{"name":"connectivity_plus_web","dependencies":[]},{"name":"connectivity_plus_windows","dependencies":[]}],"date_created":"2022-09-22 20:53:14.912483","version":"3.3.0"}
@@ -3,6 +3,8 @@ import 'dart:math';
import 'package:fl_query/fl_query.dart';
import 'package:flutter/material.dart';
final bowl = QueryBowl();
final successJob = QueryJob<String, void>(
queryKey: "greetings-example",
task: (queryKey, _) => Future.delayed(
+4 -1
View File
@@ -1,15 +1,17 @@
import 'package:fl_query_example/components/basic_infinite_query.dart';
import 'package:fl_query_example/components/basic_mutation.dart';
import 'package:fl_query/fl_query.dart';
import 'package:fl_query_example/components/basic_query.dart';
import 'package:fl_query_example/components/lazy_query.dart';
import 'package:fl_query_example/components/mutation_variable_key.dart';
import 'package:fl_query_example/components/query_external_data.dart';
import 'package:fl_query_example/components/query_previous_data.dart';
import 'package:fl_query_example/components/query_variable_key.dart';
import 'package:fl_query/fl_query.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
void main() {
debugRepaintRainbowEnabled = true;
runApp(const MyApp());
}
@@ -19,6 +21,7 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return QueryBowlScope(
bowl: QueryBowl(),
child: MaterialApp(
// showPerformanceOverlay: true,
title: 'Flutter Demo',
@@ -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,
+16 -5
View File
@@ -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,
+25 -16
View File
@@ -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!);
}
}
+335 -511
View File
@@ -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;
}
+26 -16
View File
@@ -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!);
}
}
+124
View File
@@ -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);
}
}
+193 -170
View File
@@ -1,18 +1,14 @@
// Mocks generated by Mockito 5.2.0 from annotations
// Mocks generated by Mockito 5.3.0 from annotations
// in fl_query/test/query_test.dart.
// Do not manually edit this file.
import 'dart:async' as _i7;
// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i4;
import 'package:connectivity_plus/connectivity_plus.dart' as _i11;
import 'package:fl_query/src/models/mutation_job.dart' as _i9;
import 'package:fl_query/src/models/query_job.dart' as _i8;
import 'package:fl_query/src/mutation.dart' as _i4;
import 'package:fl_query/src/query.dart' as _i3;
import 'package:fl_query/src/query_bowl.dart' as _i6;
import 'package:flutter/foundation.dart' as _i5;
import 'package:flutter/rendering.dart' as _i10;
import 'package:flutter/widgets.dart' as _i2;
import 'package:connectivity_plus/connectivity_plus.dart' as _i6;
import 'package:fl_query/fl_query.dart' as _i3;
import 'package:fl_query/src/query_cache.dart' as _i2;
import 'package:flutter/cupertino.dart' as _i5;
import 'package:mockito/mockito.dart' as _i1;
// ignore_for_file: type=lint
@@ -24,71 +20,92 @@ import 'package:mockito/mockito.dart' as _i1;
// ignore_for_file: prefer_const_constructors
// ignore_for_file: unnecessary_parenthesis
// ignore_for_file: camel_case_types
// ignore_for_file: subtype_of_sealed_class
class _FakeDuration_0 extends _i1.Fake implements Duration {}
class _FakeWidget_1 extends _i1.Fake implements _i2.Widget {
@override
String toString({_i2.DiagnosticLevel? minLevel = _i2.DiagnosticLevel.info}) =>
super.toString();
class _FakeDuration_0 extends _i1.SmartFake implements Duration {
_FakeDuration_0(Object parent, Invocation parentInvocation)
: super(parent, parentInvocation);
}
class _FakeQuery_2<T extends Object, Outside> extends _i1.Fake
implements _i3.Query<T, Outside> {}
class _FakeMutation_3<T extends Object, V> extends _i1.Fake
implements _i4.Mutation<T, V> {}
class _FakeInheritedElement_4 extends _i1.Fake implements _i2.InheritedElement {
@override
String toString({_i2.DiagnosticLevel? minLevel = _i2.DiagnosticLevel.info}) =>
super.toString();
class _FakeQueryCache_1 extends _i1.SmartFake implements _i2.QueryCache {
_FakeQueryCache_1(Object parent, Invocation parentInvocation)
: super(parent, parentInvocation);
}
class _FakeDiagnosticsNode_5 extends _i1.Fake implements _i2.DiagnosticsNode {
@override
String toString(
{_i5.TextTreeConfiguration? parentConfiguration,
_i2.DiagnosticLevel? minLevel = _i2.DiagnosticLevel.info}) =>
super.toString();
class _FakeQuery_2<T extends Object, Outside> extends _i1.SmartFake
implements _i3.Query<T, Outside> {
_FakeQuery_2(Object parent, Invocation parentInvocation)
: super(parent, parentInvocation);
}
class _FakeObject_6 extends _i1.Fake implements Object {}
class _FakeInfiniteQuery_3<T extends Object, Outside, PageParam extends Object>
extends _i1.SmartFake implements _i3.InfiniteQuery<T, Outside, PageParam> {
_FakeInfiniteQuery_3(Object parent, Invocation parentInvocation)
: super(parent, parentInvocation);
}
class _FakeMutation_4<T extends Object, V> extends _i1.SmartFake
implements _i3.Mutation<T, V> {
_FakeMutation_4(Object parent, Invocation parentInvocation)
: super(parent, parentInvocation);
}
class _FakeObject_5 extends _i1.SmartFake implements Object {
_FakeObject_5(Object parent, Invocation parentInvocation)
: super(parent, parentInvocation);
}
/// A class which mocks [QueryBowl].
///
/// See the documentation for Mockito's code generation for more information.
class MockQueryBowl extends _i1.Mock implements _i6.QueryBowl {
class MockQueryBowl extends _i1.Mock implements _i3.QueryBowl {
MockQueryBowl() {
_i1.throwOnMissingStub(this);
}
@override
Duration get staleTime => (super.noSuchMethod(Invocation.getter(#staleTime),
returnValue: _FakeDuration_0()) as Duration);
@override
Duration get cacheTime => (super.noSuchMethod(Invocation.getter(#cacheTime),
returnValue: _FakeDuration_0()) as Duration);
returnValue: _FakeDuration_0(this, Invocation.getter(#staleTime)))
as Duration);
@override
bool get refetchOnMount => (super
.noSuchMethod(Invocation.getter(#refetchOnMount), returnValue: false)
as bool);
@override
bool get refetchOnWindowFocus =>
(super.noSuchMethod(Invocation.getter(#refetchOnWindowFocus),
returnValue: false) as bool);
@override
bool get refetchOnApplicationResume =>
(super.noSuchMethod(Invocation.getter(#refetchOnApplicationResume),
returnValue: false) as bool);
@override
bool get refetchOnReconnect =>
(super.noSuchMethod(Invocation.getter(#refetchOnReconnect),
returnValue: false) as bool);
@override
Duration get refetchOnReconnectDelay =>
(super.noSuchMethod(Invocation.getter(#refetchOnReconnectDelay),
returnValue: _FakeDuration_0(
this, Invocation.getter(#refetchOnReconnectDelay))) as Duration);
@override
bool get refetchOnExternalDataChange =>
(super.noSuchMethod(Invocation.getter(#refetchOnExternalDataChange),
returnValue: false) as bool);
@override
int Function(List<String>) get removeQueries =>
(super.noSuchMethod(Invocation.getter(#removeQueries),
returnValue: (List<String> __p0) => 0) as int Function(List<String>));
Duration get refetchInterval =>
(super.noSuchMethod(Invocation.getter(#refetchInterval),
returnValue:
_FakeDuration_0(this, Invocation.getter(#refetchInterval)))
as Duration);
@override
void Function() get clear =>
(super.noSuchMethod(Invocation.getter(#clear), returnValue: () {}) as void
Function());
_i2.QueryCache get cache => (super.noSuchMethod(Invocation.getter(#cache),
returnValue: _FakeQueryCache_1(this, Invocation.getter(#cache)))
as _i2.QueryCache);
@override
set cache(_i2.QueryCache? _cache) =>
super.noSuchMethod(Invocation.setter(#cache, _cache),
returnValueForMissingStub: null);
@override
int get isFetching =>
(super.noSuchMethod(Invocation.getter(#isFetching), returnValue: 0)
@@ -98,38 +115,73 @@ class MockQueryBowl extends _i1.Mock implements _i6.QueryBowl {
(super.noSuchMethod(Invocation.getter(#isMutating), returnValue: 0)
as int);
@override
_i2.Widget get child => (super.noSuchMethod(Invocation.getter(#child),
returnValue: _FakeWidget_1()) as _i2.Widget);
void onQueriesUpdate<T extends Object, Outside>(
void Function(_i3.Query<T, Outside>)? listener) =>
super.noSuchMethod(Invocation.method(#onQueriesUpdate, [listener]),
returnValueForMissingStub: null);
@override
_i7.Future<T?> prefetchQuery<T extends Object, Outside>(
_i8.QueryJob<T, Outside>? options,
{Outside? externalData}) =>
(super.noSuchMethod(
Invocation.method(
#prefetchQuery, [options], {#externalData: externalData}),
returnValue: Future<T?>.value()) as _i7.Future<T?>);
void onMutationsUpdate<T extends Object, Outside>(
void Function(_i3.Mutation<T, Outside>)? listener) =>
super.noSuchMethod(Invocation.method(#onMutationsUpdate, [listener]),
returnValueForMissingStub: null);
@override
_i7.Future<T?> fetchQuery<T extends Object, Outside>(
_i8.QueryJob<T, Outside>? options,
{Outside? externalData,
_i3.QueryListener<T>? onData,
_i3.QueryListener<dynamic>? onError,
_i2.ValueKey<String>? key}) =>
(super.noSuchMethod(
Invocation.method(#fetchQuery, [
options
], {
#externalData: externalData,
#onData: onData,
#onError: onError,
#key: key
}),
returnValue: Future<T?>.value()) as _i7.Future<T?>);
void onInfiniteQueriesUpdate<T extends Object, Outside,
PageParam extends Object>(
void Function(_i3.InfiniteQuery<T, Outside, PageParam>)? listener) =>
super.noSuchMethod(
Invocation.method(#onInfiniteQueriesUpdate, [listener]),
returnValueForMissingStub: null);
@override
_i3.InfiniteQuery<T, Outside, PageParam>?
getInfiniteQuery<T extends Object, Outside, PageParam extends Object>(
String? queryKey) =>
(super.noSuchMethod(Invocation.method(#getInfiniteQuery, [queryKey]))
as _i3.InfiniteQuery<T, Outside, PageParam>?);
@override
_i3.Query<T, Outside>? getQuery<T extends Object, Outside>(
String? queryKey) =>
(super.noSuchMethod(Invocation.method(#getQuery, [queryKey]))
as _i3.Query<T, Outside>?);
@override
_i3.Mutation<T, V>? getMutation<T extends Object, V>(String? mutationKey) =>
(super.noSuchMethod(Invocation.method(#getMutation, [mutationKey]))
as _i3.Mutation<T, V>?);
@override
void setQueryData<T extends Object, Outside>(
String? queryKey, _i3.QueryUpdateFunction<T>? updateCb) =>
super.noSuchMethod(Invocation.method(#setQueryData, [queryKey, updateCb]),
returnValueForMissingStub: null);
@override
void
setInfiniteQueryData<T extends Object, Outside, PageParam extends Object>(
String? queryKey,
_i3.QueryUpdateFunction<Map<PageParam, T?>>? updateCb) =>
super.noSuchMethod(
Invocation.method(#setInfiniteQueryData, [queryKey, updateCb]),
returnValueForMissingStub: null);
@override
void resetQueries(List<String>? queryKeys) =>
super.noSuchMethod(Invocation.method(#resetQueries, [queryKeys]),
returnValueForMissingStub: null);
@override
void invalidateQueries(List<String>? queryKeys) =>
super.noSuchMethod(Invocation.method(#invalidateQueries, [queryKeys]),
returnValueForMissingStub: null);
@override
_i4.Future<void> refetchQueries(List<String>? queryKeys) =>
(super.noSuchMethod(Invocation.method(#refetchQueries, [queryKeys]),
returnValue: _i4.Future<void>.value(),
returnValueForMissingStub: _i4.Future<void>.value())
as _i4.Future<void>);
@override
int removeQueries(List<String>? queryKeys) =>
(super.noSuchMethod(Invocation.method(#removeQueries, [queryKeys]),
returnValue: 0) as int);
@override
_i3.Query<T, Outside> addQuery<T extends Object, Outside>(
_i8.QueryJob<T, Outside>? queryJob,
_i3.QueryJob<T, Outside>? queryJob,
{Outside? externalData,
_i2.ValueKey<String>? key,
_i5.ValueKey<String>? key,
_i3.QueryListener<T>? onData,
_i3.QueryListener<dynamic>? onError,
T? previousData}) =>
@@ -143,134 +195,105 @@ class MockQueryBowl extends _i1.Mock implements _i6.QueryBowl {
#onError: onError,
#previousData: previousData
}),
returnValue: _FakeQuery_2<T, Outside>()) as _i3.Query<T, Outside>);
returnValue: _FakeQuery_2<T, Outside>(
this,
Invocation.method(#addQuery, [
queryJob
], {
#externalData: externalData,
#key: key,
#onData: onData,
#onError: onError,
#previousData: previousData
}))) as _i3.Query<T, Outside>);
@override
_i4.Mutation<T, V> addMutation<T extends Object, V>(
_i9.MutationJob<T, V>? mutationJob,
{_i4.MutationListener<T, V>? onData,
_i4.MutationListener<dynamic, V>? onError,
_i4.MutationListenerReturnable<V, dynamic>? onMutate,
_i2.ValueKey<String>? key}) =>
_i3.InfiniteQuery<T, Outside, PageParam> addInfiniteQuery<T extends Object, Outside, PageParam extends Object>(
_i3.InfiniteQueryJob<T, Outside, PageParam>? infiniteQueryJob,
{Outside? externalData,
_i5.ValueKey<String>? key,
_i3.InfiniteQueryListeners<T, PageParam>? onData,
_i3.InfiniteQueryListeners<dynamic, PageParam>? onError}) =>
(super.noSuchMethod(Invocation.method(#addInfiniteQuery, [infiniteQueryJob], {#externalData: externalData, #key: key, #onData: onData, #onError: onError}),
returnValue: _FakeInfiniteQuery_3<T, Outside, PageParam>(
this,
Invocation.method(#addInfiniteQuery, [
infiniteQueryJob
], {
#externalData: externalData,
#key: key,
#onData: onData,
#onError: onError
}))) as _i3.InfiniteQuery<T, Outside, PageParam>);
@override
_i3.Mutation<T, V> addMutation<T extends Object, V>(
_i3.MutationJob<T, V>? mutationJob,
{_i3.MutationListener<T, V>? onData,
_i3.MutationListener<dynamic, V>? onError,
_i3.MutationListenerReturnable<V, dynamic>? onMutate,
_i5.ValueKey<String>? key}) =>
(super
.noSuchMethod(Invocation.method(#addMutation, [mutationJob], {#onData: onData, #onError: onError, #onMutate: onMutate, #key: key}),
returnValue: _FakeMutation_4<T, V>(
this,
Invocation.method(#addMutation, [
mutationJob
], {
#onData: onData,
#onError: onError,
#onMutate: onMutate,
#key: key
}))) as _i3.Mutation<T, V>);
@override
_i4.Future<T?> fetchQuery<T extends Object, Outside>(
_i3.QueryJob<T, Outside>? options,
{Outside? externalData,
_i3.QueryListener<T>? onData,
_i3.QueryListener<dynamic>? onError,
_i5.ValueKey<String>? key}) =>
(super.noSuchMethod(
Invocation.method(#addMutation, [
mutationJob
Invocation.method(#fetchQuery, [
options
], {
#externalData: externalData,
#onData: onData,
#onError: onError,
#onMutate: onMutate,
#key: key
}),
returnValue: _FakeMutation_3<T, V>()) as _i4.Mutation<T, V>);
@override
_i3.Query<T, Outside>? getQuery<T extends Object, Outside>(
String? queryKey) =>
(super.noSuchMethod(Invocation.method(#getQuery, [queryKey]))
as _i3.Query<T, Outside>?);
@override
_i4.Mutation<T, V>? getMutation<T extends Object, V>(String? mutationKey) =>
(super.noSuchMethod(Invocation.method(#getMutation, [mutationKey]))
as _i4.Mutation<T, V>?);
@override
void setQueryData<T extends Object, Outside>(
String? queryKey, _i3.QueryUpdateFunction<T>? updateCb) =>
super.noSuchMethod(Invocation.method(#setQueryData, [queryKey, updateCb]),
returnValueForMissingStub: null);
@override
void resetQueries(List<String>? queryKeys) =>
super.noSuchMethod(Invocation.method(#resetQueries, [queryKeys]),
returnValueForMissingStub: null);
@override
void invalidateQueries(List<String>? queryKeys) =>
super.noSuchMethod(Invocation.method(#invalidateQueries, [queryKeys]),
returnValueForMissingStub: null);
@override
_i7.Future<void> refetchQueries(List<String>? queryKeys) =>
(super.noSuchMethod(Invocation.method(#refetchQueries, [queryKeys]),
returnValue: Future<void>.value(),
returnValueForMissingStub: Future<void>.value()) as _i7.Future<void>);
@override
bool updateShouldNotify(_i2.InheritedWidget? oldWidget) =>
(super.noSuchMethod(Invocation.method(#updateShouldNotify, [oldWidget]),
returnValue: false) as bool);
@override
_i2.InheritedElement createElement() =>
(super.noSuchMethod(Invocation.method(#createElement, []),
returnValue: _FakeInheritedElement_4()) as _i2.InheritedElement);
@override
String toStringShort() => (super
.noSuchMethod(Invocation.method(#toStringShort, []), returnValue: '')
as String);
@override
void debugFillProperties(_i10.DiagnosticPropertiesBuilder? properties) =>
super.noSuchMethod(Invocation.method(#debugFillProperties, [properties]),
returnValueForMissingStub: null);
@override
String toStringShallow(
{String? joiner = r', ',
_i2.DiagnosticLevel? minLevel = _i2.DiagnosticLevel.debug}) =>
(super.noSuchMethod(
Invocation.method(
#toStringShallow, [], {#joiner: joiner, #minLevel: minLevel}),
returnValue: '') as String);
@override
String toStringDeep(
{String? prefixLineOne = r'',
String? prefixOtherLines,
_i2.DiagnosticLevel? minLevel = _i2.DiagnosticLevel.debug}) =>
(super.noSuchMethod(
Invocation.method(#toStringDeep, [], {
#prefixLineOne: prefixLineOne,
#prefixOtherLines: prefixOtherLines,
#minLevel: minLevel
}),
returnValue: '') as String);
@override
_i2.DiagnosticsNode toDiagnosticsNode(
{String? name, _i5.DiagnosticsTreeStyle? style}) =>
(super.noSuchMethod(
Invocation.method(
#toDiagnosticsNode, [], {#name: name, #style: style}),
returnValue: _FakeDiagnosticsNode_5()) as _i2.DiagnosticsNode);
@override
List<_i2.DiagnosticsNode> debugDescribeChildren() =>
(super.noSuchMethod(Invocation.method(#debugDescribeChildren, []),
returnValue: <_i2.DiagnosticsNode>[]) as List<_i2.DiagnosticsNode>);
@override
String toString({_i2.DiagnosticLevel? minLevel = _i2.DiagnosticLevel.info}) =>
super.toString();
returnValue: _i4.Future<T?>.value()) as _i4.Future<T?>);
}
/// A class which mocks [Connectivity].
///
/// See the documentation for Mockito's code generation for more information.
class MockConnectivity extends _i1.Mock implements _i11.Connectivity {
class MockConnectivity extends _i1.Mock implements _i6.Connectivity {
MockConnectivity() {
_i1.throwOnMissingStub(this);
}
@override
_i7.Stream<_i11.ConnectivityResult> get onConnectivityChanged =>
_i4.Stream<_i6.ConnectivityResult> get onConnectivityChanged =>
(super.noSuchMethod(Invocation.getter(#onConnectivityChanged),
returnValue: Stream<_i11.ConnectivityResult>.empty())
as _i7.Stream<_i11.ConnectivityResult>);
returnValue: _i4.Stream<_i6.ConnectivityResult>.empty())
as _i4.Stream<_i6.ConnectivityResult>);
@override
_i7.Future<_i11.ConnectivityResult> checkConnectivity() =>
_i4.Future<_i6.ConnectivityResult> checkConnectivity() =>
(super.noSuchMethod(Invocation.method(#checkConnectivity, []),
returnValue: Future<_i11.ConnectivityResult>.value(
_i11.ConnectivityResult.bluetooth))
as _i7.Future<_i11.ConnectivityResult>);
returnValue: _i4.Future<_i6.ConnectivityResult>.value(
_i6.ConnectivityResult.bluetooth))
as _i4.Future<_i6.ConnectivityResult>);
}
/// A class which mocks [QueryJob].
///
/// See the documentation for Mockito's code generation for more information.
class MockQueryJobVoidObject extends _i1.Mock
implements _i8.QueryJob<Object, void> {
implements _i3.QueryJob<Object, void> {
@override
_i3.QueryTaskFunction<Object, void> get task =>
(super.noSuchMethod(Invocation.getter(#task),
returnValue: (String queryKey, void externalData) =>
Future<Object>.value(_FakeObject_6()))
_i4.Future<Object>.value(
_FakeObject_5(this, Invocation.getter(#task))))
as _i3.QueryTaskFunction<Object, void>);
@override
set task(_i3.QueryTaskFunction<Object, void>? _task) =>
@@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"connectivity_plus","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus-2.3.6/","native_build":true,"dependencies":[]}],"android":[{"name":"connectivity_plus","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus-2.3.6/","native_build":true,"dependencies":[]}],"macos":[{"name":"connectivity_plus_macos","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_macos-1.2.4/","native_build":true,"dependencies":[]}],"linux":[{"name":"connectivity_plus_linux","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_linux-1.3.1/","native_build":false,"dependencies":[]}],"windows":[{"name":"connectivity_plus_windows","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_windows-1.2.2/","native_build":true,"dependencies":[]}],"web":[{"name":"connectivity_plus_web","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_web-1.2.3/","dependencies":[]}]},"dependencyGraph":[{"name":"connectivity_plus","dependencies":["connectivity_plus_linux","connectivity_plus_macos","connectivity_plus_web","connectivity_plus_windows"]},{"name":"connectivity_plus_linux","dependencies":[]},{"name":"connectivity_plus_macos","dependencies":[]},{"name":"connectivity_plus_web","dependencies":[]},{"name":"connectivity_plus_windows","dependencies":[]}],"date_created":"2022-09-14 11:35:47.051873","version":"3.3.0"}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"connectivity_plus","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus-2.3.6/","native_build":true,"dependencies":[]}],"android":[{"name":"connectivity_plus","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus-2.3.6/","native_build":true,"dependencies":[]}],"macos":[{"name":"connectivity_plus_macos","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_macos-1.2.4/","native_build":true,"dependencies":[]}],"linux":[{"name":"connectivity_plus_linux","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_linux-1.3.1/","native_build":false,"dependencies":[]}],"windows":[{"name":"connectivity_plus_windows","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_windows-1.2.2/","native_build":true,"dependencies":[]}],"web":[{"name":"connectivity_plus_web","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_web-1.2.3/","dependencies":[]}]},"dependencyGraph":[{"name":"connectivity_plus","dependencies":["connectivity_plus_linux","connectivity_plus_macos","connectivity_plus_web","connectivity_plus_windows"]},{"name":"connectivity_plus_linux","dependencies":[]},{"name":"connectivity_plus_macos","dependencies":[]},{"name":"connectivity_plus_web","dependencies":[]},{"name":"connectivity_plus_windows","dependencies":[]}],"date_created":"2022-09-22 21:28:31.234632","version":"3.3.0"}
@@ -1,2 +1,2 @@
sdk.dir=/opt/android-sdk
sdk.dir=/home/krtirtho/Android/Sdk
flutter.sdk=/opt/flutter
@@ -19,6 +19,7 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return QueryBowlScope(
bowl: QueryBowl(),
child: MaterialApp(
// showPerformanceOverlay: true,
title: 'Fl-Query Hooks Example',
@@ -2,6 +2,12 @@
file(TO_CMAKE_PATH "/opt/flutter" FLUTTER_ROOT)
file(TO_CMAKE_PATH "/home/krtirtho/dev/fl-query/packages/fl_query_hooks/example" PROJECT_DIR)
set(FLUTTER_VERSION "1.0.0+1" PARENT_SCOPE)
set(FLUTTER_VERSION_MAJOR 1 PARENT_SCOPE)
set(FLUTTER_VERSION_MINOR 0 PARENT_SCOPE)
set(FLUTTER_VERSION_PATCH 0 PARENT_SCOPE)
set(FLUTTER_VERSION_BUILD 1 PARENT_SCOPE)
# Environment variables to pass to tool_backend.sh
list(APPEND FLUTTER_TOOL_ENVIRONMENT
"FLUTTER_ROOT=/opt/flutter"
@@ -19,6 +19,8 @@ InfiniteQuery<T, Outside, PageParam>
List<Object?>? keys,
}) {
final context = useContext();
final mounted = useIsMounted();
final update = useForceUpdate();
final QueryBowl queryBowl = QueryBowl.of(context);
final ValueKey<String> uKey = useMemoized(() => ValueKey(uuid.v4()), []);
final infiniteQuery = useRef(
@@ -42,6 +44,7 @@ InfiniteQuery<T, Outside, PageParam>
onData: onData,
onError: onError,
);
update();
final hasExternalDataChanged = infiniteQuery.value.externalData != null &&
infiniteQuery.value.prevUsedExternalData != null &&
!isShallowEqual(infiniteQuery.value.externalData!,
@@ -73,7 +76,16 @@ InfiniteQuery<T, Outside, PageParam>
]);
useEffect(() {
init();
WidgetsBinding.instance.addPostFrameCallback((_) {
init();
QueryBowl.of(context).onInfiniteQueriesUpdate<T, Outside, PageParam>(
(newInfiniteQuery) {
if (newInfiniteQuery.queryKey != job.queryKey || !mounted()) return;
infiniteQuery.value = newInfiniteQuery;
update();
},
);
});
return disposeQuery;
}, []);
@@ -117,6 +129,5 @@ InfiniteQuery<T, Outside, PageParam>
return null;
});
return queryBowl.getInfiniteQuery<T, Outside, PageParam>(job.queryKey) ??
infiniteQuery.value;
return infiniteQuery.value;
}
@@ -20,6 +20,8 @@ Mutation<T, V> useMutation<T extends Object, V>({
List<Object?>? keys,
}) {
final context = useContext();
final mounted = useIsMounted();
final update = useForceUpdate();
final QueryBowl queryBowl = QueryBowl.of(context);
final ValueKey<String> uKey = useMemoized(() => ValueKey(uuid.v4()), []);
final mutation =
@@ -33,6 +35,7 @@ Mutation<T, V> useMutation<T extends Object, V>({
onMutate: onMutate,
key: uKey,
);
update();
}, [mutation.value, job, onData, onError, onMutate, uKey]);
final disposeMutation = useCallback(() {
@@ -48,7 +51,16 @@ Mutation<T, V> useMutation<T extends Object, V>({
final oldOnMutate = usePrevious(onMutate);
useEffect(() {
init();
WidgetsBinding.instance.addPostFrameCallback((_) {
init();
QueryBowl.of(context).onMutationsUpdate<T, V>(
(newMutation) {
if (newMutation.mutationKey != job.mutationKey || !mounted()) return;
mutation.value = newMutation;
update();
},
);
});
return disposeMutation;
}, []);
@@ -73,5 +85,5 @@ Mutation<T, V> useMutation<T extends Object, V>({
return null;
});
return queryBowl.getMutation<T, V>(job.mutationKey) ?? mutation.value;
return mutation.value;
}
+14 -2
View File
@@ -17,6 +17,8 @@ Query<T, Outside> useQuery<T extends Object, Outside>({
QueryListener<dynamic>? onError,
List<Object?>? keys,
}) {
final mounted = useIsMounted();
final update = useForceUpdate();
final context = useContext();
final QueryBowl queryBowl = QueryBowl.of(context);
final ValueKey<String> uKey = useMemoized(() => ValueKey(uuid.v4()), []);
@@ -42,6 +44,7 @@ Query<T, Outside> useQuery<T extends Object, Outside>({
onData: onData,
onError: onError,
);
update();
final hasExternalDataChanged = query.value.externalData != null &&
query.value.prevUsedExternalData != null &&
!isShallowEqual(
@@ -60,7 +63,16 @@ Query<T, Outside> useQuery<T extends Object, Outside>({
}, [query.value, onData, onError, uKey]);
useEffect(() {
init();
WidgetsBinding.instance.addPostFrameCallback((_) {
init();
QueryBowl.of(context).onQueriesUpdate<T, Outside>(
(newQuery) {
if (newQuery.queryKey != job.queryKey || !mounted()) return;
query.value = newQuery;
update();
},
);
});
return disposeQuery;
}, []);
@@ -113,5 +125,5 @@ Query<T, Outside> useQuery<T extends Object, Outside>({
return null;
});
return queryBowl.getQuery<T, Outside>(job.queryKey) ?? query.value;
return query.value;
}
@@ -1,3 +1,15 @@
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:uuid/uuid.dart';
const uuid = Uuid();
// a flutter hook that can force update the ui
//
// Usage:
// final forceUpdate = useForceUpdate();
// forceUpdate();
useForceUpdate() {
final state = useState(false);
return () => state.value = !state.value;
}