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",
"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 {
var key = QueryKey("TEST");
QueryClient queryClient = QueryClient();
queryClient.mount();
var data = await queryClient.fetchQuery<Map, dynamic, Map>(
queryKey: key,
queryFn: (context) {
return Future.value(todos.first);
},
);
print("FETCHED DATA=====");
print(data);
print("======CACHED DATA");
print(queryClient.getQueryData(key));
void main() async {
try {
var key = QueryKey("TEST");
QueryClient queryClient = QueryClient();
queryClient.mount();
var data = await queryClient.fetchQuery<Map, dynamic, Map>(
queryKey: key,
queryFn: (context) {
return Future.value(todos.first);
},
);
print("======FETCHED DATA======");
print(data);
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>
extends QueryOptions<TQueryFnData, TError, TData> {
/**
* The time in milliseconds after data is considered stale.
* If the data is fresh it will be returned from the cache.
*/
/// The time after data is considered stale.
/// If the data is fresh it will be returned from the cache.
Duration? staleTime;
FetchQueryOptions(
ShouldRetryFunction<TError>? retry,
@@ -8,13 +8,13 @@ typedef NotifyFunction = void Function(void Function() callback);
typedef BatchNotifyFunction = void Function(void Function() callback);
class _NotifyManager {
class NotifyManager {
List<NotifyCallback> _queue;
int _transactions;
late NotifyFunction _notifyFn;
late BatchNotifyFunction _batchNotifyFn;
_NotifyManager()
NotifyManager()
: _queue = [],
_transactions = 0 {
_notifyFn = (void Function() callback) {
@@ -27,7 +27,7 @@ class _NotifyManager {
}
T batch<T>(T Function() callback) {
T result;
final T result;
_transactions++;
try {
result = callback();
@@ -40,7 +40,7 @@ class _NotifyManager {
return result;
}
schedule(NotifyCallback callback) {
void schedule(NotifyCallback callback) {
if (_transactions > 0) {
_queue.add(callback);
} else {
@@ -92,4 +92,4 @@ class _NotifyManager {
// SINGLETON
_NotifyManager notifyManager = new _NotifyManager();
NotifyManager notifyManager = new NotifyManager();
@@ -60,8 +60,10 @@ class QueryCache extends Subscribable<QueryCacheListener> {
super();
Query<TQueryFnData, TError, TData> build<TQueryFnData, TError, TData>(
QueryClient client, QueryOptions<TQueryFnData, TError, TData> options,
[QueryState<TData, TError>? state]) {
QueryClient client,
QueryOptions<TQueryFnData, TError, TData> options, [
QueryState<TData, TError>? state,
]) {
QueryKey queryKey = options.queryKey!;
String queryHash =
options.queryHash ?? hashQueryKeyByOptions(queryKey, options);
@@ -112,10 +112,16 @@ class QueryClient {
DataUpdateFunction<TData?, TData> updater, [
DateTime? updatedAt,
]) {
var defaultedOptions =
defaultQueryOptions(QueryObserverOptions(queryKey: queryKey));
return _queryCache.build(this, defaultedOptions).setData(
updater as Function(dynamic),
final QueryOptions<dynamic, dynamic, TData> defaultedOptions =
QueryOptions<dynamic, dynamic, TData>.fromJson(
defaultQueryOptions<dynamic, dynamic, TData, dynamic>(
QueryObserverOptions<dynamic, dynamic, TData, dynamic>(
queryKey: queryKey))
.toJson());
return _queryCache
.build<dynamic, dynamic, TData>(this, defaultedOptions)
.setData(
updater,
updatedAt: updatedAt,
);
}
@@ -144,8 +150,7 @@ class QueryClient {
}
QueryState<TData, TError>? getQueryState<TData, TError>(
QueryKey,
queryKey, [
QueryKey queryKey, [
QueryFilters? filters,
]) {
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));
});
});
}