test created fo NotifyManager

This commit is contained in:
Kingkor Roy Tirtho
2022-02-12 20:26:11 +06:00
parent 58f98e6e74
commit 57c265ba72
6 changed files with 119 additions and 134 deletions
+29 -117
View File
@@ -22,123 +22,35 @@ var todos = [
"title": "qui ullam ratione quibusdam voluptatem quia omnis", "title": "qui ullam ratione quibusdam voluptatem quia omnis",
"completed": false "completed": false
}, },
{
"userId": 1,
"id": 7,
"title": "illo expedita consequatur quia in",
"completed": false
},
{
"userId": 1,
"id": 8,
"title": "quo adipisci enim quam ut ab",
"completed": true
},
{
"userId": 1,
"id": 9,
"title": "molestiae perspiciatis ipsa",
"completed": false
},
{
"userId": 1,
"id": 10,
"title": "illo est ratione doloremque quia maiores aut",
"completed": true
},
{
"userId": 1,
"id": 11,
"title": "vero rerum temporibus dolor",
"completed": true
},
{
"userId": 1,
"id": 12,
"title": "ipsa repellendus fugit nisi",
"completed": true
},
{"userId": 1, "id": 13, "title": "et doloremque nulla", "completed": false},
{
"userId": 1,
"id": 14,
"title": "repellendus sunt dolores architecto voluptatum",
"completed": true
},
{
"userId": 1,
"id": 15,
"title": "ab voluptatum amet voluptas",
"completed": true
},
{
"userId": 1,
"id": 16,
"title": "accusamus eos facilis sint et aut voluptatem",
"completed": true
},
{
"userId": 1,
"id": 17,
"title": "quo laboriosam deleniti aut qui",
"completed": true
},
{
"userId": 1,
"id": 18,
"title": "dolorum est consequatur ea mollitia in culpa",
"completed": false
},
{
"userId": 1,
"id": 19,
"title": "molestiae ipsa aut voluptatibus pariatur dolor nihil",
"completed": true
},
{
"userId": 1,
"id": 20,
"title": "ullam nobis libero sapiente ad optio sint",
"completed": true
},
{
"userId": 2,
"id": 21,
"title": "suscipit repellat esse quibusdam voluptatem incidunt",
"completed": false
},
{
"userId": 2,
"id": 22,
"title": "distinctio vitae autem nihil ut molestias quo",
"completed": true
},
{
"userId": 2,
"id": 23,
"title": "et itaque necessitatibus maxime molestiae qui quas velit",
"completed": false
},
{
"userId": 2,
"id": 24,
"title": "adipisci non ad dicta qui amet quaerat doloribus ea",
"completed": false
},
]; ];
void main(List<String> arguments) async { void main() async {
var key = QueryKey("TEST"); try {
QueryClient queryClient = QueryClient(); var key = QueryKey("TEST");
queryClient.mount(); QueryClient queryClient = QueryClient();
var data = await queryClient.fetchQuery<Map, dynamic, Map>( queryClient.mount();
queryKey: key, var data = await queryClient.fetchQuery<Map, dynamic, Map>(
queryFn: (context) { queryKey: key,
return Future.value(todos.first); queryFn: (context) {
}, return Future.value(todos.first);
); },
print("FETCHED DATA====="); );
print(data); print("======FETCHED DATA======");
print("======CACHED DATA"); print(data);
print(queryClient.getQueryData(key)); print("======CACHED DATA======");
print(queryClient.getQueryData(key));
queryClient.setQueryData<Map>(key, (prevData) {
return {
...(prevData) ?? {},
"title": "Yehi aloh heh",
"completed": true,
};
});
print("======CACHED DATA======");
print(queryClient.getQueryData(key));
print("======STATE======");
print(queryClient.getQueryState(key)?.toJson());
} catch (e) {
print(e);
}
} }
+2 -4
View File
@@ -522,10 +522,8 @@ class DefaultOptions<TError> {
class FetchQueryOptions<TQueryFnData, TError, TData> class FetchQueryOptions<TQueryFnData, TError, TData>
extends QueryOptions<TQueryFnData, TError, TData> { extends QueryOptions<TQueryFnData, TError, TData> {
/** /// The time after data is considered stale.
* The time in milliseconds after data is considered stale. /// If the data is fresh it will be returned from the cache.
* If the data is fresh it will be returned from the cache.
*/
Duration? staleTime; Duration? staleTime;
FetchQueryOptions( FetchQueryOptions(
ShouldRetryFunction<TError>? retry, ShouldRetryFunction<TError>? retry,
@@ -8,13 +8,13 @@ typedef NotifyFunction = void Function(void Function() callback);
typedef BatchNotifyFunction = void Function(void Function() callback); typedef BatchNotifyFunction = void Function(void Function() callback);
class _NotifyManager { class NotifyManager {
List<NotifyCallback> _queue; List<NotifyCallback> _queue;
int _transactions; int _transactions;
late NotifyFunction _notifyFn; late NotifyFunction _notifyFn;
late BatchNotifyFunction _batchNotifyFn; late BatchNotifyFunction _batchNotifyFn;
_NotifyManager() NotifyManager()
: _queue = [], : _queue = [],
_transactions = 0 { _transactions = 0 {
_notifyFn = (void Function() callback) { _notifyFn = (void Function() callback) {
@@ -27,7 +27,7 @@ class _NotifyManager {
} }
T batch<T>(T Function() callback) { T batch<T>(T Function() callback) {
T result; final T result;
_transactions++; _transactions++;
try { try {
result = callback(); result = callback();
@@ -40,7 +40,7 @@ class _NotifyManager {
return result; return result;
} }
schedule(NotifyCallback callback) { void schedule(NotifyCallback callback) {
if (_transactions > 0) { if (_transactions > 0) {
_queue.add(callback); _queue.add(callback);
} else { } else {
@@ -92,4 +92,4 @@ class _NotifyManager {
// SINGLETON // SINGLETON
_NotifyManager notifyManager = new _NotifyManager(); NotifyManager notifyManager = new NotifyManager();
@@ -60,8 +60,10 @@ class QueryCache extends Subscribable<QueryCacheListener> {
super(); super();
Query<TQueryFnData, TError, TData> build<TQueryFnData, TError, TData>( Query<TQueryFnData, TError, TData> build<TQueryFnData, TError, TData>(
QueryClient client, QueryOptions<TQueryFnData, TError, TData> options, QueryClient client,
[QueryState<TData, TError>? state]) { QueryOptions<TQueryFnData, TError, TData> options, [
QueryState<TData, TError>? state,
]) {
QueryKey queryKey = options.queryKey!; QueryKey queryKey = options.queryKey!;
String queryHash = String queryHash =
options.queryHash ?? hashQueryKeyByOptions(queryKey, options); options.queryHash ?? hashQueryKeyByOptions(queryKey, options);
@@ -112,10 +112,16 @@ class QueryClient {
DataUpdateFunction<TData?, TData> updater, [ DataUpdateFunction<TData?, TData> updater, [
DateTime? updatedAt, DateTime? updatedAt,
]) { ]) {
var defaultedOptions = final QueryOptions<dynamic, dynamic, TData> defaultedOptions =
defaultQueryOptions(QueryObserverOptions(queryKey: queryKey)); QueryOptions<dynamic, dynamic, TData>.fromJson(
return _queryCache.build(this, defaultedOptions).setData( defaultQueryOptions<dynamic, dynamic, TData, dynamic>(
updater as Function(dynamic), QueryObserverOptions<dynamic, dynamic, TData, dynamic>(
queryKey: queryKey))
.toJson());
return _queryCache
.build<dynamic, dynamic, TData>(this, defaultedOptions)
.setData(
updater,
updatedAt: updatedAt, updatedAt: updatedAt,
); );
} }
@@ -144,8 +150,7 @@ class QueryClient {
} }
QueryState<TData, TError>? getQueryState<TData, TError>( QueryState<TData, TError>? getQueryState<TData, TError>(
QueryKey, QueryKey queryKey, [
queryKey, [
QueryFilters? filters, QueryFilters? filters,
]) { ]) {
return _queryCache return _queryCache
@@ -0,0 +1,68 @@
import 'package:fl_query/src/core/notify_manager.dart';
import 'package:test/expect.dart';
import 'package:test/scaffolding.dart';
class SpyNotifyManager extends NotifyManager {
SpyNotifyManager() : super();
int flushCall = 0;
@override
void flush() {
super.flush();
flushCall++;
}
}
void main() {
group("NotifyManager", () {
test(
"Should call _notifyFn in schedule When no callback is batched",
() async {
final NotifyManager notifyManager = NotifyManager();
int called = 0;
notifyManager.schedule(() => called++);
await Future.delayed(Duration(seconds: 1));
expect(called, equals(1));
},
);
test(
"Should call default _batchNotifyFn even When multiple level deep callbacks are registered",
() async {
final NotifyManager notifyManager = NotifyManager();
int level1 = 0;
int level2 = 0;
int level3 = 0;
callback() async {
await Future.delayed(Duration(seconds: 20));
level3++;
}
notifyManager.batch(() {
notifyManager.batch(() {
notifyManager.schedule(callback);
level2++;
});
level1++;
});
await Future.delayed(Duration(seconds: 30));
expect(level1, equals(1));
expect(level2, equals(1));
expect(level3, equals(1));
},
timeout: Timeout(Duration(minutes: 2)),
);
test("Should flush When Exception is thrown in a batched callback", () {
final SpyNotifyManager notifyManager = SpyNotifyManager();
try {
notifyManager.batch(() {
throw Exception("Damn an exception");
});
} catch (e) {}
expect(notifyManager.flushCall, equals(1));
});
});
}