fix cache bugs
MODIFY
lib/src/cache/in_memory.dart
lib/src/cache/in_memory.dart
@@ -23,8 +23,12 @@ class InMemoryCache implements Cache {
/// Writes an entity to the internal HashMap.
@override
void write(String key, dynamic value) {
+ if (_inMemoryCache.containsKey(key) && _inMemoryCache[key] is Map) {
+ _inMemoryCache[key].addAll(value);
+ } else {
_inMemoryCache[key] = value;
}
+ }
/// Saves the internal HashMap to a file.
@override
@@ -56,8 +60,15 @@ class InMemoryCache implements Cache {
return File('$path/cache.txt');
}
+ bool writing = false;
Future<dynamic> _writeToStorage() async {
+ if (!writing) {
+ writing = true;
+ } else {
+ return;
+ }
final File file = await _localStorageFile;
+
final IOSink sink = file.openWrite();
_inMemoryCache.forEach((String key, dynamic value) {
@@ -65,7 +76,7 @@ class InMemoryCache implements Cache {
});
await sink.close();
-
+ writing = false;
return;
}
@@ -76,7 +87,6 @@ class InMemoryCache implements Cache {
if (file.existsSync()) {
final Stream<List<int>> inputStream = file.openRead();
-
inputStream
.transform(utf8.decoder) // Decode bytes to UTF8.
.transform(

MODIFY
test/normalized_in_memory_test.dart
test/normalized_in_memory_test.dart
@@ -66,6 +66,47 @@ final Map<String, Object> updatedCOperationData = <String, Object>{
'aField': <String, Object>{'field': false}
};
+final Map<String, Object> smallerAValue = <String, Object>{
+ 'a': <String, Object>{
+ '__typename': 'A',
+ 'id': 1,
+ 'list': <Object>[
+ 5,
+ 6,
+ 7,
+ <String, Object>{
+ '__typename': 'Item',
+ 'id': 8,
+ 'value': 8,
+ }
+ ],
+ },
+};
+
+final Map<String, Object> updatedAOperationData = <String, Object>{
+ 'a': <String, Object>{
+ '__typename': 'A',
+ 'id': 1,
+ 'list': <Object>[
+ 5,
+ 6,
+ 7,
+ <String, Object>{
+ '__typename': 'Item',
+ 'id': 8,
+ 'value': 8,
+ }
+ ],
+ 'b': <String, Object>{
+ '__typename': 'B',
+ 'id': 5,
+ 'c': updatedCValue,
+ 'bField': <String, Object>{'field': true}
+ },
+ },
+ 'aField': <String, Object>{'field': false}
+};
+
void main() {
group('Normalizes writes', () {
final NormalizedInMemoryCache cache = NormalizedInMemoryCache(
@@ -79,5 +120,10 @@ void main() {
cache.write('C/6', updatedCValue);
expect(cache.read(rawOperationKey), equals(updatedCOperationData));
});
+
+ test('updating smaller query update parent', () {
+ cache.write('anotherKey', smallerAValue);
+ expect(cache.read(rawOperationKey), equals(updatedAOperationData));
+ });
});
}

fix cache bugs