refactor(fl_query): bring back jobs API
This commit is contained in:
@@ -111,21 +111,21 @@ packages:
|
||||
path: ".."
|
||||
relative: true
|
||||
source: path
|
||||
version: "1.0.0-alpha.5"
|
||||
version: "1.0.0-alpha.6"
|
||||
fl_query_connectivity_plus_adapter:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
path: "../../fl_query_connectivity_plus_adapter"
|
||||
relative: true
|
||||
source: path
|
||||
version: "0.1.0-alpha.4"
|
||||
version: "0.1.0-alpha.5"
|
||||
fl_query_devtools:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
path: "../../fl_query_devtools"
|
||||
relative: true
|
||||
source: path
|
||||
version: "0.1.0-alpha.3"
|
||||
version: "0.1.0-alpha.4"
|
||||
flutter:
|
||||
dependency: "direct main"
|
||||
description: flutter
|
||||
|
||||
@@ -6,6 +6,10 @@ export 'src/collections/refresh_config.dart';
|
||||
export 'src/collections/retry_config.dart';
|
||||
export 'src/collections/connectivity_adapter.dart';
|
||||
|
||||
export 'src/collections/jobs/query_job.dart';
|
||||
export 'src/collections/jobs/infinite_query_job.dart';
|
||||
export 'src/collections/jobs/mutation_job.dart';
|
||||
|
||||
export 'src/core/cache.dart';
|
||||
export 'src/core/client.dart';
|
||||
export 'src/core/infinite_query.dart';
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
import 'package:fl_query/src/collections/json_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/core/infinite_query.dart';
|
||||
|
||||
typedef InfiniteQueryJobFn<DataType, PageType, ArgsType> = Future<DataType>
|
||||
Function(PageType page, ArgsType args);
|
||||
|
||||
typedef InfiniteQueryJobVariableKeyFn<DataType, ErrorType, PageType, ArgsType>
|
||||
= InfiniteQueryJob<DataType, ErrorType, PageType, ArgsType> Function(
|
||||
String variable);
|
||||
|
||||
class InfiniteQueryJob<DataType, ErrorType, PageType, ArgsType> {
|
||||
final InfiniteQueryJobFn<DataType, PageType, ArgsType?> task;
|
||||
final String queryKey;
|
||||
|
||||
final PageType initialPage;
|
||||
final InfiniteQueryNextPage<DataType, PageType> nextPage;
|
||||
|
||||
final RetryConfig? retryConfig;
|
||||
final RefreshConfig? refreshConfig;
|
||||
final JsonConfig<DataType>? jsonConfig;
|
||||
|
||||
final bool enabled;
|
||||
|
||||
const InfiniteQueryJob({
|
||||
required this.queryKey,
|
||||
required this.task,
|
||||
required this.nextPage,
|
||||
required this.initialPage,
|
||||
this.retryConfig,
|
||||
this.refreshConfig,
|
||||
this.jsonConfig,
|
||||
this.enabled = true,
|
||||
}) : assert(
|
||||
(jsonConfig != null && enabled) || jsonConfig == null,
|
||||
'jsonConfig is only supported when enabled is true',
|
||||
);
|
||||
|
||||
static InfiniteQueryJobVariableKeyFn<DataType, ErrorType, PageType, ArgsType>
|
||||
withVariableKey<DataType, ErrorType, PageType, ArgsType>({
|
||||
required String baseQueryKey,
|
||||
required InfiniteQueryJobFn<DataType, PageType, ArgsType?> task,
|
||||
required final InfiniteQueryNextPage<DataType, PageType> nextPage,
|
||||
required final PageType initialPage,
|
||||
RetryConfig? retryConfig,
|
||||
RefreshConfig? refreshConfig,
|
||||
JsonConfig<DataType>? jsonConfig,
|
||||
bool enabled = true,
|
||||
}) {
|
||||
return (String variable) => InfiniteQueryJob(
|
||||
queryKey: "$baseQueryKey$variable",
|
||||
task: task,
|
||||
nextPage: nextPage,
|
||||
initialPage: initialPage,
|
||||
retryConfig: retryConfig,
|
||||
refreshConfig: refreshConfig,
|
||||
jsonConfig: jsonConfig,
|
||||
enabled: enabled,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import 'package:fl_query/src/collections/retry_config.dart';
|
||||
|
||||
typedef MutationJobFn<DataType, VariablesType, ArgsType> = Future<DataType>
|
||||
Function(
|
||||
VariablesType variables,
|
||||
ArgsType args,
|
||||
);
|
||||
|
||||
typedef MutationJobVariableKeyFn<DataType, ErrorType, VariablesType,
|
||||
RecoveryType, ArgsType>
|
||||
= MutationJob<DataType, ErrorType, VariablesType, RecoveryType, ArgsType>
|
||||
Function(String variable);
|
||||
|
||||
class MutationJob<DataType, ErrorType, VariablesType, RecoveryType, ArgsType> {
|
||||
final MutationJobFn<DataType, VariablesType, ArgsType?> task;
|
||||
final String mutationKey;
|
||||
|
||||
final RetryConfig? retryConfig;
|
||||
|
||||
final List<String>? refreshQueries;
|
||||
final List<String>? refreshInfiniteQueries;
|
||||
|
||||
const MutationJob({
|
||||
required this.mutationKey,
|
||||
required this.task,
|
||||
this.retryConfig,
|
||||
this.refreshQueries,
|
||||
this.refreshInfiniteQueries,
|
||||
});
|
||||
|
||||
static MutationJobVariableKeyFn<DataType, ErrorType, VariablesType,
|
||||
RecoveryType, ArgsType>
|
||||
withVariableKey<DataType, ErrorType, VariablesType, RecoveryType,
|
||||
ArgsType>({
|
||||
required String baseMutationKey,
|
||||
required MutationJobFn<DataType, VariablesType, ArgsType?> task,
|
||||
RetryConfig? retryConfig,
|
||||
List<String>? refreshQueries,
|
||||
List<String>? refreshInfiniteQueries,
|
||||
}) {
|
||||
return (String variable) => MutationJob(
|
||||
mutationKey: "$baseMutationKey$variable",
|
||||
task: task,
|
||||
retryConfig: retryConfig,
|
||||
refreshQueries: refreshQueries,
|
||||
refreshInfiniteQueries: refreshInfiniteQueries,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import 'package:fl_query/src/collections/json_config.dart';
|
||||
import 'package:fl_query/src/collections/refresh_config.dart';
|
||||
import 'package:fl_query/src/collections/retry_config.dart';
|
||||
|
||||
typedef QueryJobFn<DataType, ArgsType> = Future<DataType> Function(
|
||||
ArgsType args);
|
||||
|
||||
typedef QueryJobVariableKeyFn<DataType, ErrorType, ArgsType>
|
||||
= QueryJob<DataType, ErrorType, ArgsType> Function(String variable);
|
||||
|
||||
class QueryJob<DataType, ErrorType, ArgsType> {
|
||||
final QueryJobFn<DataType, ArgsType?> task;
|
||||
final String queryKey;
|
||||
|
||||
final DataType? initial;
|
||||
|
||||
final RetryConfig? retryConfig;
|
||||
final RefreshConfig? refreshConfig;
|
||||
final JsonConfig<DataType>? jsonConfig;
|
||||
|
||||
// widget specific
|
||||
final bool enabled;
|
||||
|
||||
const QueryJob({
|
||||
required this.queryKey,
|
||||
required this.task,
|
||||
this.initial,
|
||||
this.retryConfig,
|
||||
this.refreshConfig,
|
||||
this.jsonConfig,
|
||||
this.enabled = true,
|
||||
}) : assert(
|
||||
(jsonConfig != null && enabled) || jsonConfig == null,
|
||||
'jsonConfig is only supported when enabled is true',
|
||||
);
|
||||
|
||||
static QueryJobVariableKeyFn<DataType, ErrorType, ArgsType>
|
||||
withVariableKey<DataType, ErrorType, ArgsType>({
|
||||
required String baseQueryKey,
|
||||
required QueryJobFn<DataType, ArgsType?> task,
|
||||
DataType? initial,
|
||||
RetryConfig? retryConfig,
|
||||
RefreshConfig? refreshConfig,
|
||||
JsonConfig<DataType>? jsonConfig,
|
||||
bool enabled = true,
|
||||
}) {
|
||||
return (String variable) => QueryJob(
|
||||
queryKey: "$baseQueryKey$variable",
|
||||
task: task,
|
||||
initial: initial,
|
||||
retryConfig: retryConfig,
|
||||
refreshConfig: refreshConfig,
|
||||
jsonConfig: jsonConfig,
|
||||
enabled: enabled,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:fl_query/src/collections/jobs/infinite_query_job.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/retry_config.dart';
|
||||
@@ -55,6 +56,31 @@ class InfiniteQueryBuilder<DataType, ErrorType, PageType>
|
||||
'jsonConfig is only supported when enabled is true',
|
||||
);
|
||||
|
||||
static InfiniteQueryBuilder<DataType, ErrorType, PageType>
|
||||
withJob<DataType, ErrorType, PageType, ArgsType>({
|
||||
required InfiniteQueryJob<DataType, ErrorType, PageType, ArgsType> job,
|
||||
required InfiniteQueryBuilderFn<DataType, ErrorType, PageType> builder,
|
||||
ValueChanged<PageEvent<DataType, PageType>>? onData,
|
||||
ValueChanged<PageEvent<ErrorType, PageType>>? onError,
|
||||
Key? key,
|
||||
ArgsType? args,
|
||||
}) {
|
||||
return InfiniteQueryBuilder<DataType, ErrorType, PageType>(
|
||||
job.queryKey,
|
||||
(page) => job.task(page, args),
|
||||
builder: builder,
|
||||
initialPage: job.initialPage,
|
||||
nextPage: job.nextPage,
|
||||
retryConfig: job.retryConfig,
|
||||
refreshConfig: job.refreshConfig,
|
||||
jsonConfig: job.jsonConfig,
|
||||
onData: onData,
|
||||
onError: onError,
|
||||
enabled: job.enabled,
|
||||
key: key,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
State<InfiniteQueryBuilder<DataType, ErrorType, PageType>> createState() =>
|
||||
_InfiniteQueryBuilderState<DataType, ErrorType, PageType>();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:fl_query/src/collections/jobs/mutation_job.dart';
|
||||
import 'package:fl_query/src/collections/retry_config.dart';
|
||||
import 'package:fl_query/src/core/client.dart';
|
||||
import 'package:fl_query/src/core/mutation.dart';
|
||||
@@ -53,6 +54,32 @@ class MutationBuilder<DataType, ErrorType, VariablesType, RecoveryType>
|
||||
super.key,
|
||||
});
|
||||
|
||||
static MutationBuilder<DataType, ErrorType, VariablesType, RecoveryType>
|
||||
withJob<DataType, ErrorType, VariablesType, RecoveryType, ArgsType>({
|
||||
required MutationJob<DataType, ErrorType, VariablesType, RecoveryType,
|
||||
ArgsType>
|
||||
job,
|
||||
required MutationBuilderFn<DataType, ErrorType, VariablesType> builder,
|
||||
Key? key,
|
||||
MutationOnDataFn<DataType, RecoveryType>? onData,
|
||||
MutationOnErrorFn<ErrorType, RecoveryType>? onError,
|
||||
MutationOnMutationFn<VariablesType, RecoveryType>? onMutate,
|
||||
ArgsType? args,
|
||||
}) {
|
||||
return MutationBuilder<DataType, ErrorType, VariablesType, RecoveryType>(
|
||||
job.mutationKey,
|
||||
(variables) => job.task(variables, args),
|
||||
builder: builder,
|
||||
retryConfig: job.retryConfig,
|
||||
onData: onData,
|
||||
onError: onError,
|
||||
onMutate: onMutate,
|
||||
refreshQueries: job.refreshQueries,
|
||||
refreshInfiniteQueries: job.refreshInfiniteQueries,
|
||||
key: key,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
State<MutationBuilder<DataType, ErrorType, VariablesType, RecoveryType>>
|
||||
createState() => _MutationBuilderState<DataType, ErrorType, VariablesType,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'dart:async';
|
||||
|
||||
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/refresh_config.dart';
|
||||
import 'package:fl_query/src/collections/retry_config.dart';
|
||||
@@ -48,6 +49,30 @@ class QueryBuilder<DataType, ErrorType> extends StatefulWidget {
|
||||
'jsonConfig is only supported when enabled is true',
|
||||
);
|
||||
|
||||
static QueryBuilder<DataType, ErrorType>
|
||||
withJob<DataType, ErrorType, ArgsType>({
|
||||
required QueryJob<DataType, ErrorType, ArgsType> job,
|
||||
required QueryBuilderFn<DataType, ErrorType> builder,
|
||||
ValueChanged<DataType>? onData,
|
||||
ValueChanged<ErrorType>? onError,
|
||||
ArgsType? args,
|
||||
Key? key,
|
||||
}) {
|
||||
return QueryBuilder<DataType, ErrorType>(
|
||||
job.queryKey,
|
||||
() => job.task(args),
|
||||
builder: builder,
|
||||
initial: job.initial,
|
||||
retryConfig: job.retryConfig,
|
||||
refreshConfig: job.refreshConfig,
|
||||
jsonConfig: job.jsonConfig,
|
||||
onData: onData,
|
||||
onError: onError,
|
||||
enabled: job.enabled,
|
||||
key: key,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
State<QueryBuilder<DataType, ErrorType>> createState() =>
|
||||
_QueryBuilderState<DataType, ErrorType>();
|
||||
|
||||
Reference in New Issue
Block a user