fix: Delete in transaction on new store does not clear cache correctly

This commit is contained in:
Krille 2024-01-02 10:01:33 +01:00
parent af9006a278
commit 40565464e0
No known key found for this signature in database
GPG Key ID: E067ECD60F1A0652
3 changed files with 21 additions and 4 deletions

View File

@ -166,7 +166,10 @@ class Box<V> {
txn ??= boxCollection._db.transaction(name, 'readwrite');
final store = txn.objectStore(name);
await store.delete(key);
_cache.remove(key);
// Set to null instead remove() so that inside of transactions null is
// returned.
_cache[key] = null;
_cachedKeys?.remove(key);
return;
}
@ -183,7 +186,7 @@ class Box<V> {
final store = txn.objectStore(name);
for (final key in keys) {
await store.delete(key);
_cache.remove(key);
_cache[key] = null;
_cachedKeys?.remove(key);
}
return;

View File

@ -302,7 +302,9 @@ class Box<V> {
txn.delete(name, where: 'k = ?', whereArgs: [key]);
}
_cache.remove(key);
// Set to null instead remove() so that inside of transactions null is
// returned.
_cache[key] = null;
_cachedKeys?.remove(key);
return;
}
@ -326,7 +328,7 @@ class Box<V> {
}
for (final key in keys) {
_cache.remove(key);
_cache[key] = null;
_cachedKeys?.removeAll(keys);
}
return;

View File

@ -59,6 +59,18 @@ void main() {
await box.clear();
});
test('Box.delete in transaction', () async {
final box = collection.openBox<Map>('cats');
await box.put('fluffy', data);
await box.put('loki', data2);
await collection.transaction(() async {
await box.delete('fluffy');
expect(await box.get('fluffy'), null);
});
expect(await box.get('fluffy'), null);
await box.clear();
});
test('Box.deleteAll', () async {
final box = collection.openBox<Map>('cats');
await box.put('fluffy', data);