refactor: make args required and add fetchQueryJob and fetchInfiniteQueryJob

This commit is contained in:
Kingkor Roy Tirtho
2023-10-20 10:01:36 +06:00
parent 6c65fa2017
commit 81a1fc1515
12 changed files with 52 additions and 20 deletions
@@ -11,7 +11,7 @@ typedef InfiniteQueryJobVariableKeyFn<DataType, ErrorType, PageType, ArgsType>
String variable); String variable);
class InfiniteQueryJob<DataType, ErrorType, PageType, ArgsType> { class InfiniteQueryJob<DataType, ErrorType, PageType, ArgsType> {
final InfiniteQueryJobFn<DataType, PageType, ArgsType?> task; final InfiniteQueryJobFn<DataType, PageType, ArgsType> task;
final String queryKey; final String queryKey;
final PageType initialPage; final PageType initialPage;
@@ -12,7 +12,7 @@ typedef MutationJobVariableKeyFn<DataType, ErrorType, VariablesType,
Function(String variable); Function(String variable);
class MutationJob<DataType, ErrorType, VariablesType, RecoveryType, ArgsType> { class MutationJob<DataType, ErrorType, VariablesType, RecoveryType, ArgsType> {
final MutationJobFn<DataType, VariablesType, ArgsType?> task; final MutationJobFn<DataType, VariablesType, ArgsType> task;
final String mutationKey; final String mutationKey;
final RetryConfig? retryConfig; final RetryConfig? retryConfig;
@@ -9,7 +9,7 @@ typedef QueryJobVariableKeyFn<DataType, ErrorType, ArgsType>
= QueryJob<DataType, ErrorType, ArgsType> Function(String variable); = QueryJob<DataType, ErrorType, ArgsType> Function(String variable);
class QueryJob<DataType, ErrorType, ArgsType> { class QueryJob<DataType, ErrorType, ArgsType> {
final QueryJobFn<DataType, ArgsType?> task; final QueryJobFn<DataType, ArgsType> task;
final String queryKey; final String queryKey;
final DataType? initial; final DataType? initial;
+36 -4
View File
@@ -3,6 +3,8 @@ import 'dart:async';
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
import 'package:fl_query/src/collections/connectivity_adapter.dart'; import 'package:fl_query/src/collections/connectivity_adapter.dart';
import 'package:fl_query/src/collections/default_configs.dart'; import 'package:fl_query/src/collections/default_configs.dart';
import 'package:fl_query/src/collections/jobs/infinite_query_job.dart';
import 'package:fl_query/src/collections/jobs/query_job.dart';
import 'package:fl_query/src/collections/json_config.dart'; import 'package:fl_query/src/collections/json_config.dart';
import 'package:fl_query/src/collections/refresh_config.dart'; import 'package:fl_query/src/collections/refresh_config.dart';
import 'package:fl_query/src/collections/retry_config.dart'; import 'package:fl_query/src/collections/retry_config.dart';
@@ -162,6 +164,20 @@ class QueryClient {
} }
} }
Future<DataType?> fetchQueryJob<DataType, ErrorType, ArgsType>({
required QueryJob<DataType, ErrorType, ArgsType> job,
required ArgsType args,
}) {
return fetchQuery<DataType, ErrorType>(
job.queryKey,
() => job.task(args),
initial: job.initial,
retryConfig: job.retryConfig,
refreshConfig: job.refreshConfig,
jsonConfig: job.jsonConfig,
);
}
/// Finds the [Query] with the given [key] and returns it /// Finds the [Query] with the given [key] and returns it
/// ///
/// [exact] can be used to match the key exactly or by prefix /// [exact] can be used to match the key exactly or by prefix
@@ -220,7 +236,7 @@ class QueryClient {
String key, String key,
InfiniteQueryFn<DataType, PageType> queryFn, { InfiniteQueryFn<DataType, PageType> queryFn, {
required InfiniteQueryNextPage<DataType, PageType> nextPage, required InfiniteQueryNextPage<DataType, PageType> nextPage,
required PageType initialParam, required PageType initialPage,
RetryConfig? retryConfig, RetryConfig? retryConfig,
RefreshConfig? refreshConfig, RefreshConfig? refreshConfig,
JsonConfig<DataType>? jsonConfig, JsonConfig<DataType>? jsonConfig,
@@ -232,7 +248,7 @@ class QueryClient {
key, key,
queryFn, queryFn,
nextPage: nextPage, nextPage: nextPage,
initialParam: initialParam, initialPage: initialPage,
retryConfig: retryConfig ?? this.retryConfig, retryConfig: retryConfig ?? this.retryConfig,
refreshConfig: refreshConfig ?? this.refreshConfig, refreshConfig: refreshConfig ?? this.refreshConfig,
jsonConfig: jsonConfig, jsonConfig: jsonConfig,
@@ -254,7 +270,7 @@ class QueryClient {
String key, String key,
InfiniteQueryFn<DataType, PageType> queryFn, { InfiniteQueryFn<DataType, PageType> queryFn, {
required InfiniteQueryNextPage<DataType, PageType> nextPage, required InfiniteQueryNextPage<DataType, PageType> nextPage,
required PageType initialParam, required PageType initialPage,
RetryConfig? retryConfig, RetryConfig? retryConfig,
RefreshConfig? refreshConfig, RefreshConfig? refreshConfig,
JsonConfig<DataType>? jsonConfig, JsonConfig<DataType>? jsonConfig,
@@ -266,7 +282,7 @@ class QueryClient {
key, key,
queryFn, queryFn,
nextPage: nextPage, nextPage: nextPage,
initialParam: initialParam, initialPage: initialPage,
retryConfig: retryConfig, retryConfig: retryConfig,
refreshConfig: refreshConfig, refreshConfig: refreshConfig,
jsonConfig: jsonConfig, jsonConfig: jsonConfig,
@@ -292,6 +308,22 @@ class QueryClient {
} }
} }
Future<DataType?>
fetchInfiniteQueryJob<DataType, ErrorType, PageType, ArgsType>({
required InfiniteQueryJob<DataType, ErrorType, PageType, ArgsType> job,
required ArgsType args,
}) {
return fetchInfiniteQuery<DataType, ErrorType, PageType>(
job.queryKey,
(page) => job.task(page, args),
nextPage: job.nextPage,
initialPage: job.initialPage,
retryConfig: job.retryConfig,
refreshConfig: job.refreshConfig,
jsonConfig: job.jsonConfig,
);
}
/// Finds the [InfiniteQuery] with the given [key] /// Finds the [InfiniteQuery] with the given [key]
/// ///
/// [exact] can be used to match the key exactly or by prefix /// [exact] can be used to match the key exactly or by prefix
@@ -122,7 +122,7 @@ class InfiniteQuery<DataType, ErrorType, PageType>
final RefreshConfig refreshConfig; final RefreshConfig refreshConfig;
final JsonConfig<DataType>? jsonConfig; final JsonConfig<DataType>? jsonConfig;
final PageType _initialParam; final PageType _initialPage;
InfiniteQueryFn<DataType, PageType> _queryFn; InfiniteQueryFn<DataType, PageType> _queryFn;
InfiniteQueryNextPage<DataType, PageType> _nextPage; InfiniteQueryNextPage<DataType, PageType> _nextPage;
@@ -131,11 +131,11 @@ class InfiniteQuery<DataType, ErrorType, PageType>
this.key, this.key,
InfiniteQueryFn<DataType, PageType> queryFn, { InfiniteQueryFn<DataType, PageType> queryFn, {
required InfiniteQueryNextPage<DataType, PageType> nextPage, required InfiniteQueryNextPage<DataType, PageType> nextPage,
required PageType initialParam, required PageType initialPage,
required this.retryConfig, required this.retryConfig,
required this.refreshConfig, required this.refreshConfig,
this.jsonConfig, this.jsonConfig,
}) : _initialParam = initialParam, }) : _initialPage = initialPage,
_dataController = StreamController.broadcast(), _dataController = StreamController.broadcast(),
_errorController = StreamController.broadcast(), _errorController = StreamController.broadcast(),
_box = Hive.lazyBox(QueryClient.infiniteQueryCachePrefix), _box = Hive.lazyBox(QueryClient.infiniteQueryCachePrefix),
@@ -144,7 +144,7 @@ class InfiniteQuery<DataType, ErrorType, PageType>
super(InfiniteQueryState<DataType, ErrorType, PageType>( super(InfiniteQueryState<DataType, ErrorType, PageType>(
pages: { pages: {
InfiniteQueryPage<DataType, ErrorType, PageType>( InfiniteQueryPage<DataType, ErrorType, PageType>(
page: initialParam, page: initialPage,
updatedAt: DateTime.now(), updatedAt: DateTime.now(),
staleDuration: refreshConfig.staleDuration, staleDuration: refreshConfig.staleDuration,
), ),
@@ -490,7 +490,7 @@ class InfiniteQuery<DataType, ErrorType, PageType>
await _operation?.cancel(); await _operation?.cancel();
state = state.copyWith(pages: { state = state.copyWith(pages: {
InfiniteQueryPage<DataType, ErrorType, PageType>( InfiniteQueryPage<DataType, ErrorType, PageType>(
page: _initialParam, page: _initialPage,
updatedAt: DateTime.now(), updatedAt: DateTime.now(),
staleDuration: refreshConfig.staleDuration, staleDuration: refreshConfig.staleDuration,
) )
@@ -60,10 +60,10 @@ class InfiniteQueryBuilder<DataType, ErrorType, PageType>
withJob<DataType, ErrorType, PageType, ArgsType>({ withJob<DataType, ErrorType, PageType, ArgsType>({
required InfiniteQueryJob<DataType, ErrorType, PageType, ArgsType> job, required InfiniteQueryJob<DataType, ErrorType, PageType, ArgsType> job,
required InfiniteQueryBuilderFn<DataType, ErrorType, PageType> builder, required InfiniteQueryBuilderFn<DataType, ErrorType, PageType> builder,
required ArgsType args,
ValueChanged<PageEvent<DataType, PageType>>? onData, ValueChanged<PageEvent<DataType, PageType>>? onData,
ValueChanged<PageEvent<ErrorType, PageType>>? onError, ValueChanged<PageEvent<ErrorType, PageType>>? onError,
Key? key, Key? key,
ArgsType? args,
}) { }) {
return InfiniteQueryBuilder<DataType, ErrorType, PageType>( return InfiniteQueryBuilder<DataType, ErrorType, PageType>(
job.queryKey, job.queryKey,
@@ -116,7 +116,7 @@ class _InfiniteQueryBuilderState<DataType, ErrorType, PageType>
query = client.createInfiniteQuery( query = client.createInfiniteQuery(
widget.queryKey, widget.queryKey,
widget.queryFn, widget.queryFn,
initialParam: widget.initialPage, initialPage: widget.initialPage,
nextPage: widget.nextPage, nextPage: widget.nextPage,
retryConfig: widget.retryConfig, retryConfig: widget.retryConfig,
refreshConfig: widget.refreshConfig, refreshConfig: widget.refreshConfig,
@@ -60,11 +60,11 @@ class MutationBuilder<DataType, ErrorType, VariablesType, RecoveryType>
ArgsType> ArgsType>
job, job,
required MutationBuilderFn<DataType, ErrorType, VariablesType> builder, required MutationBuilderFn<DataType, ErrorType, VariablesType> builder,
required ArgsType args,
Key? key, Key? key,
MutationOnDataFn<DataType, RecoveryType>? onData, MutationOnDataFn<DataType, RecoveryType>? onData,
MutationOnErrorFn<ErrorType, RecoveryType>? onError, MutationOnErrorFn<ErrorType, RecoveryType>? onError,
MutationOnMutationFn<VariablesType, RecoveryType>? onMutate, MutationOnMutationFn<VariablesType, RecoveryType>? onMutate,
ArgsType? args,
}) { }) {
return MutationBuilder<DataType, ErrorType, VariablesType, RecoveryType>( return MutationBuilder<DataType, ErrorType, VariablesType, RecoveryType>(
job.mutationKey, job.mutationKey,
@@ -53,9 +53,9 @@ class QueryBuilder<DataType, ErrorType> extends StatefulWidget {
withJob<DataType, ErrorType, ArgsType>({ withJob<DataType, ErrorType, ArgsType>({
required QueryJob<DataType, ErrorType, ArgsType> job, required QueryJob<DataType, ErrorType, ArgsType> job,
required QueryBuilderFn<DataType, ErrorType> builder, required QueryBuilderFn<DataType, ErrorType> builder,
required ArgsType args,
ValueChanged<DataType>? onData, ValueChanged<DataType>? onData,
ValueChanged<ErrorType>? onError, ValueChanged<ErrorType>? onError,
ArgsType? args,
Key? key, Key? key,
}) { }) {
return QueryBuilder<DataType, ErrorType>( return QueryBuilder<DataType, ErrorType>(
@@ -5,9 +5,9 @@ import 'package:flutter/material.dart';
InfiniteQuery<DataType, ErrorType, PageType> InfiniteQuery<DataType, ErrorType, PageType>
useInfiniteQueryJob<DataType, ErrorType, PageType, ArgsType>({ useInfiniteQueryJob<DataType, ErrorType, PageType, ArgsType>({
required InfiniteQueryJob<DataType, ErrorType, PageType, ArgsType> job, required InfiniteQueryJob<DataType, ErrorType, PageType, ArgsType> job,
required ArgsType args,
ValueChanged<PageEvent<DataType, PageType>>? onData, ValueChanged<PageEvent<DataType, PageType>>? onData,
ValueChanged<PageEvent<ErrorType, PageType>>? onError, ValueChanged<PageEvent<ErrorType, PageType>>? onError,
ArgsType? args,
List<Object?>? keys, List<Object?>? keys,
}) { }) {
return useInfiniteQuery<DataType, ErrorType, PageType>( return useInfiniteQuery<DataType, ErrorType, PageType>(
@@ -9,7 +9,7 @@ Mutation<DataType, ErrorType, VariablesType>
MutationOnDataFn<DataType, RecoveryType>? onData, MutationOnDataFn<DataType, RecoveryType>? onData,
MutationOnErrorFn<ErrorType, RecoveryType>? onError, MutationOnErrorFn<ErrorType, RecoveryType>? onError,
MutationOnMutationFn<VariablesType, RecoveryType>? onMutate, MutationOnMutationFn<VariablesType, RecoveryType>? onMutate,
ArgsType? args, required ArgsType args,
List<Object?>? keys, List<Object?>? keys,
}) { }) {
return useMutation( return useMutation(
@@ -6,7 +6,7 @@ Query<DataType, ErrorType> useQueryJob<DataType, ErrorType, ArgsType>({
required QueryJob<DataType, ErrorType, ArgsType> job, required QueryJob<DataType, ErrorType, ArgsType> job,
ValueChanged<DataType>? onData, ValueChanged<DataType>? onData,
ValueChanged<ErrorType>? onError, ValueChanged<ErrorType>? onError,
ArgsType? args, required ArgsType args,
}) { }) {
return useQuery<DataType, ErrorType>( return useQuery<DataType, ErrorType>(
job.queryKey, job.queryKey,
@@ -26,7 +26,7 @@ InfiniteQuery<DataType, ErrorType, PageType>
final query = client.createInfiniteQuery<DataType, ErrorType, PageType>( final query = client.createInfiniteQuery<DataType, ErrorType, PageType>(
queryKey, queryKey,
queryFn, queryFn,
initialParam: initialPage, initialPage: initialPage,
nextPage: nextPage, nextPage: nextPage,
jsonConfig: jsonConfig, jsonConfig: jsonConfig,
refreshConfig: refreshConfig, refreshConfig: refreshConfig,