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/","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;
}