diff --git a/packages/fl_query/example/bin/example.dart b/packages/fl_query/example/bin/example.dart index 619c3d9..c4b83df 100644 --- a/packages/fl_query/example/bin/example.dart +++ b/packages/fl_query/example/bin/example.dart @@ -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 arguments) async { - var key = QueryKey("TEST"); - QueryClient queryClient = QueryClient(); - queryClient.mount(); - var data = await queryClient.fetchQuery( - 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( + queryKey: key, + queryFn: (context) { + return Future.value(todos.first); + }, + ); + print("======FETCHED DATA======"); + print(data); + print("======CACHED DATA======"); + print(queryClient.getQueryData(key)); + queryClient.setQueryData(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); + } } diff --git a/packages/fl_query/lib/src/core/models.dart b/packages/fl_query/lib/src/core/models.dart index ba4d3b8..a880959 100644 --- a/packages/fl_query/lib/src/core/models.dart +++ b/packages/fl_query/lib/src/core/models.dart @@ -522,10 +522,8 @@ class DefaultOptions { class FetchQueryOptions extends QueryOptions { - /** - * 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? retry, diff --git a/packages/fl_query/lib/src/core/notify_manager.dart b/packages/fl_query/lib/src/core/notify_manager.dart index 85231e9..8acdc71 100644 --- a/packages/fl_query/lib/src/core/notify_manager.dart +++ b/packages/fl_query/lib/src/core/notify_manager.dart @@ -8,13 +8,13 @@ typedef NotifyFunction = void Function(void Function() callback); typedef BatchNotifyFunction = void Function(void Function() callback); -class _NotifyManager { +class NotifyManager { List _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 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(); diff --git a/packages/fl_query/lib/src/core/query_cache.dart b/packages/fl_query/lib/src/core/query_cache.dart index 0130614..2b0e288 100644 --- a/packages/fl_query/lib/src/core/query_cache.dart +++ b/packages/fl_query/lib/src/core/query_cache.dart @@ -60,8 +60,10 @@ class QueryCache extends Subscribable { super(); Query build( - QueryClient client, QueryOptions options, - [QueryState? state]) { + QueryClient client, + QueryOptions options, [ + QueryState? state, + ]) { QueryKey queryKey = options.queryKey!; String queryHash = options.queryHash ?? hashQueryKeyByOptions(queryKey, options); diff --git a/packages/fl_query/lib/src/core/query_client.dart b/packages/fl_query/lib/src/core/query_client.dart index 23c62b6..0c6d61c 100644 --- a/packages/fl_query/lib/src/core/query_client.dart +++ b/packages/fl_query/lib/src/core/query_client.dart @@ -112,10 +112,16 @@ class QueryClient { DataUpdateFunction updater, [ DateTime? updatedAt, ]) { - var defaultedOptions = - defaultQueryOptions(QueryObserverOptions(queryKey: queryKey)); - return _queryCache.build(this, defaultedOptions).setData( - updater as Function(dynamic), + final QueryOptions defaultedOptions = + QueryOptions.fromJson( + defaultQueryOptions( + QueryObserverOptions( + queryKey: queryKey)) + .toJson()); + return _queryCache + .build(this, defaultedOptions) + .setData( + updater, updatedAt: updatedAt, ); } @@ -144,8 +150,7 @@ class QueryClient { } QueryState? getQueryState( - QueryKey, - queryKey, [ + QueryKey queryKey, [ QueryFilters? filters, ]) { return _queryCache diff --git a/packages/fl_query/test/src/core/notify_manager_test.dart b/packages/fl_query/test/src/core/notify_manager_test.dart new file mode 100644 index 0000000..acf35f4 --- /dev/null +++ b/packages/fl_query/test/src/core/notify_manager_test.dart @@ -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)); + }); + }); +}