fix: fetch query and infinite query resolving immediately without proper result
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:collection/collection.dart';
|
import 'package:collection/collection.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/json_config.dart';
|
import 'package:fl_query/src/collections/json_config.dart';
|
||||||
@@ -50,6 +52,8 @@ class QueryClient {
|
|||||||
RefreshConfig refreshConfig = DefaultConstants.refreshConfig,
|
RefreshConfig refreshConfig = DefaultConstants.refreshConfig,
|
||||||
JsonConfig<DataType>? jsonConfig,
|
JsonConfig<DataType>? jsonConfig,
|
||||||
}) async {
|
}) async {
|
||||||
|
DataType? result;
|
||||||
|
final completer = Completer<DataType>();
|
||||||
final query = createQuery<DataType, ErrorType>(
|
final query = createQuery<DataType, ErrorType>(
|
||||||
key,
|
key,
|
||||||
queryFn,
|
queryFn,
|
||||||
@@ -58,7 +62,16 @@ class QueryClient {
|
|||||||
refreshConfig: refreshConfig,
|
refreshConfig: refreshConfig,
|
||||||
jsonConfig: jsonConfig,
|
jsonConfig: jsonConfig,
|
||||||
);
|
);
|
||||||
return await query.fetch();
|
|
||||||
|
final subscription = query.dataStream.listen((data) {
|
||||||
|
completer.complete(data);
|
||||||
|
});
|
||||||
|
|
||||||
|
result = await query.fetch();
|
||||||
|
result ??= await completer.future;
|
||||||
|
|
||||||
|
subscription.cancel();
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Query<DataType, ErrorType>? getQuery<DataType, ErrorType>(
|
Query<DataType, ErrorType>? getQuery<DataType, ErrorType>(
|
||||||
@@ -122,6 +135,8 @@ class QueryClient {
|
|||||||
RetryConfig retryConfig = DefaultConstants.retryConfig,
|
RetryConfig retryConfig = DefaultConstants.retryConfig,
|
||||||
RefreshConfig refreshConfig = DefaultConstants.refreshConfig,
|
RefreshConfig refreshConfig = DefaultConstants.refreshConfig,
|
||||||
JsonConfig<DataType>? jsonConfig}) async {
|
JsonConfig<DataType>? jsonConfig}) async {
|
||||||
|
DataType? result;
|
||||||
|
final completer = Completer<DataType>();
|
||||||
final query = createInfiniteQuery<DataType, ErrorType, PageType>(
|
final query = createInfiniteQuery<DataType, ErrorType, PageType>(
|
||||||
key,
|
key,
|
||||||
queryFn,
|
queryFn,
|
||||||
@@ -131,7 +146,16 @@ class QueryClient {
|
|||||||
refreshConfig: refreshConfig,
|
refreshConfig: refreshConfig,
|
||||||
jsonConfig: jsonConfig,
|
jsonConfig: jsonConfig,
|
||||||
);
|
);
|
||||||
return await query.fetch();
|
|
||||||
|
final subscription = query.dataStream.listen((event) {
|
||||||
|
completer.complete(event.data);
|
||||||
|
});
|
||||||
|
|
||||||
|
result = await query.fetch();
|
||||||
|
result ??= await completer.future;
|
||||||
|
|
||||||
|
subscription.cancel();
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
InfiniteQuery<DataType, ErrorType, PageType>?
|
InfiniteQuery<DataType, ErrorType, PageType>?
|
||||||
|
|||||||
Reference in New Issue
Block a user