@@ -71,37 +71,60 @@ class QuerySnapshot<T extends Model> {
71
71
QuerySnapshot <T > withSubscriptionEvent ({
72
72
required SubscriptionEvent <T > event,
73
73
}) {
74
- final sortedListCopy = SortedList <T >.from (_sortedList);
75
74
SortedList <T >? updatedSortedList;
76
75
77
76
final newItem = event.item;
78
- final newItemMatchesPredicate = where == null || where! .evaluate (newItem);
79
- final currentItemIndex =
77
+ final matchesPredicate = where == null || where! .evaluate (newItem);
78
+ final currentIndex =
80
79
// TODO(HuiSF): remove the ignore when merging CPK feature commits
81
80
// ignore: deprecated_member_use_from_same_package
82
- sortedListCopy.indexWhere ((item) => item.getId () == newItem.getId ());
83
- final currentItem =
84
- currentItemIndex == - 1 ? null : sortedListCopy[currentItemIndex];
85
- final currentItemMatchesPredicate =
86
- currentItem != null && (where == null || where! .evaluate (currentItem));
81
+ _sortedList.indexWhere ((item) => item.getId () == newItem.getId ());
82
+ final currentItem = currentIndex == - 1 ? null : _sortedList[currentIndex];
87
83
88
- if (event.eventType == EventType .create &&
89
- newItemMatchesPredicate &&
90
- currentItem == null ) {
91
- updatedSortedList = sortedListCopy..addSorted (newItem);
92
- } else if (event.eventType == EventType .delete && currentItem != null ) {
93
- updatedSortedList = sortedListCopy..removeAt (currentItemIndex);
94
- } else if (event.eventType == EventType .update) {
95
- if (currentItemMatchesPredicate &&
96
- newItemMatchesPredicate &&
97
- currentItem != newItem) {
98
- updatedSortedList = sortedListCopy
99
- ..updateAtSorted (currentItemIndex, newItem);
100
- } else if (currentItemMatchesPredicate && ! newItemMatchesPredicate) {
101
- updatedSortedList = sortedListCopy..removeAt (currentItemIndex);
102
- } else if (currentItem == null && newItemMatchesPredicate) {
103
- updatedSortedList = sortedListCopy..addSorted (newItem);
104
- }
84
+ switch (event.eventType) {
85
+ case EventType .create:
86
+ // Skip any new item that doesn't match the predicate.
87
+ if (! matchesPredicate) break ;
88
+ if (currentItem == null ) {
89
+ // Add the item to the list. This is a new item and matches the
90
+ // predicate, it should be added.
91
+ updatedSortedList = _sortedList.copy ()..addSorted (newItem);
92
+ } else if (currentItem != newItem) {
93
+ // Update the item in the list. This is a "new" item, but it already
94
+ // exists in the list with a different value. This is the result of
95
+ // the item being created on this device and App Sync returning an
96
+ // updated item during the create mutation. This can happen when using
97
+ // custom resolvers.
98
+ updatedSortedList = _sortedList.copy ()
99
+ ..updateAtSorted (
100
+ currentIndex,
101
+ newItem,
102
+ );
103
+ }
104
+ case EventType .update:
105
+ if (currentItem == null && matchesPredicate) {
106
+ // Add the item to the list. This is an existing item that matches the
107
+ // predicate but is not yet in the list.
108
+ updatedSortedList = _sortedList.copy ()..addSorted (newItem);
109
+ } else if (currentItem != newItem && matchesPredicate) {
110
+ // Update the item in the list. This item exists in the list but the
111
+ // value of the item has changed.
112
+ updatedSortedList = _sortedList.copy ()
113
+ ..updateAtSorted (
114
+ currentIndex,
115
+ newItem,
116
+ );
117
+ } else if (currentItem != null && ! matchesPredicate) {
118
+ // Remove the item from the list. The item exist in the list but no
119
+ // longer matches the predicate.
120
+ updatedSortedList = _sortedList.copy ()..removeAt (currentIndex);
121
+ }
122
+ case EventType .delete:
123
+ if (currentItem != null ) {
124
+ // Remove the item from the list. The item exists in the list but was
125
+ // just deleted.
126
+ updatedSortedList = _sortedList.copy ()..removeAt (currentIndex);
127
+ }
105
128
}
106
129
if (updatedSortedList != null ) {
107
130
return QuerySnapshot ._(
0 commit comments