Skip to content

Commit

Permalink
fix bug in query cache and fix todos page
Browse files Browse the repository at this point in the history
  • Loading branch information
41y08h committed Nov 16, 2024
1 parent 50f4307 commit f8fc994
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
8 changes: 3 additions & 5 deletions example/lib/pages/todos_page.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first

import 'dart:math';
import 'package:basic/main.dart';
import 'package:basic/widgets/todo_list_tile.dart';
import 'package:basic/models/todos.dart';
import 'package:flutter/cupertino.dart';
Expand All @@ -26,11 +25,10 @@ class TodosPage extends HookWidget {
final todoInputController = useTextEditingController();
final addTodoMutation = useMutation<Todo, Exception, String, List<Todo>>(
todosAPI.add, onMutate: (text) async {
final previousTodos =
queryClient.getQueryData<List<Todo>>(['todos']) ?? [];
final previousTodos = client.getQueryData<List<Todo>>(['todos']) ?? [];

// Optimistically update the todo list
queryClient.setQueryData<List<Todo>>(['todos'], (previous) {
client.setQueryData<List<Todo>>(['todos'], (previous) {
final id = Random().nextInt(pow(10, 6).toInt());
final newTodo = Todo(id: id, text: text);
return [...(previous ?? []), newTodo];
Expand All @@ -40,7 +38,7 @@ class TodosPage extends HookWidget {
return previousTodos;
}, onError: (err, text, previousTodos) {
// On failure, revert back to original data
queryClient.setQueryData<List<Todo>>(
client.setQueryData<List<Todo>>(
['todos'],
(_) => previousTodos as List<Todo>,
);
Expand Down
19 changes: 14 additions & 5 deletions lib/src/query_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ class QueryCache extends ChangeNotifier {
final QueriesMap _queries = {};
QueriesMap get queries => _queries;

Query<TData, TError>? get<TData, TError>(QueryKey queryKey) {
return _queries[queryKey.lock] as Query<TData, TError>?;
Query<TData, TError> get<TData, TError>(QueryKey queryKey) {
final query = _queries[queryKey.lock];
if (query == null) {
throw ArgumentError("Query with given key doesn't exist.");
}
return query as Query<TData, TError>;
}

void add(QueryKey queryKey, Query query) {
Expand All @@ -30,9 +34,14 @@ class QueryCache extends ChangeNotifier {
required QueryKey queryKey,
required QueryClient client,
}) {
var query =
get<TData, TError>(queryKey) ?? Query(client: client, key: queryKey);
add(queryKey, query);
late final Query<TData, TError> query;
try {
query = get<TData, TError>(queryKey);
add(queryKey, query);
} catch (e) {
query = Query(client: client, key: queryKey);
add(queryKey, query);
}
return query;
}

Expand Down
8 changes: 6 additions & 2 deletions lib/src/query_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ class QueryClient {
}

TData? getQueryData<TData>(QueryKey queryKey) {
final query = queryCache.get<TData, dynamic>(queryKey);
return query?.state.data;
try {
final query = queryCache.get<TData, dynamic>(queryKey);
return query.state.data;
} catch (e) {
return null as TData?;
}
}

/// Marks the query as stale.
Expand Down

0 comments on commit f8fc994

Please sign in to comment.