refactor(fl_query_hooks): bring back jobs API

This commit is contained in:
Kingkor Roy Tirtho
2023-10-19 12:13:58 +06:00
parent 1ff91c2dce
commit 6c65fa2017
4 changed files with 78 additions and 0 deletions
@@ -4,3 +4,7 @@ export 'src/use_query_client.dart';
export 'src/use_query.dart';
export 'src/use_infinite_query.dart';
export 'src/use_mutation.dart';
export 'src/jobs/use_query_job.dart';
export 'src/jobs/use_infinite_query_job.dart';
export 'src/jobs/use_mutation_job.dart';
@@ -0,0 +1,26 @@
import 'package:fl_query/fl_query.dart';
import 'package:fl_query_hooks/src/use_infinite_query.dart';
import 'package:flutter/material.dart';
InfiniteQuery<DataType, ErrorType, PageType>
useInfiniteQueryJob<DataType, ErrorType, PageType, ArgsType>({
required InfiniteQueryJob<DataType, ErrorType, PageType, ArgsType> job,
ValueChanged<PageEvent<DataType, PageType>>? onData,
ValueChanged<PageEvent<ErrorType, PageType>>? onError,
ArgsType? args,
List<Object?>? keys,
}) {
return useInfiniteQuery<DataType, ErrorType, PageType>(
job.queryKey,
(page) => job.task(page, args),
initialPage: job.initialPage,
nextPage: job.nextPage,
retryConfig: job.retryConfig,
refreshConfig: job.refreshConfig,
jsonConfig: job.jsonConfig,
onData: onData,
onError: onError,
enabled: job.enabled,
keys: keys,
);
}
@@ -0,0 +1,26 @@
import 'package:fl_query/fl_query.dart';
import 'package:fl_query_hooks/src/use_mutation.dart';
Mutation<DataType, ErrorType, VariablesType>
useMutationJob<DataType, ErrorType, VariablesType, RecoveryType, ArgsType>({
required MutationJob<DataType, ErrorType, VariablesType, RecoveryType,
ArgsType>
job,
MutationOnDataFn<DataType, RecoveryType>? onData,
MutationOnErrorFn<ErrorType, RecoveryType>? onError,
MutationOnMutationFn<VariablesType, RecoveryType>? onMutate,
ArgsType? args,
List<Object?>? keys,
}) {
return useMutation(
job.mutationKey,
(variables) => job.task(variables, args),
retryConfig: job.retryConfig,
onData: onData,
onError: onError,
onMutate: onMutate,
refreshQueries: job.refreshQueries,
refreshInfiniteQueries: job.refreshInfiniteQueries,
keys: keys,
);
}
@@ -0,0 +1,22 @@
import 'package:fl_query/fl_query.dart';
import 'package:fl_query_hooks/src/use_query.dart';
import 'package:flutter/material.dart';
Query<DataType, ErrorType> useQueryJob<DataType, ErrorType, ArgsType>({
required QueryJob<DataType, ErrorType, ArgsType> job,
ValueChanged<DataType>? onData,
ValueChanged<ErrorType>? onError,
ArgsType? args,
}) {
return useQuery<DataType, ErrorType>(
job.queryKey,
() => job.task(args),
initial: job.initial,
retryConfig: job.retryConfig,
refreshConfig: job.refreshConfig,
jsonConfig: job.jsonConfig,
onData: onData,
onError: onError,
enabled: job.enabled,
);
}