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
@@ -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;
}