Repository: IO-Design-Team/hive_ce

Issue:
Return the delete data when removing
**Is your feature request related to a problem? Please describe.**
Without this, it requires much more complex caching in the app. This should be a non-breaking change (unless people are checking for nulls rather than the delete flag)

**Describe the solution you'd like**
Rather than returning null on deletion, return the value (watch)





Fix patch:
diff --git a/hive/CHANGELOG.md b/hive/CHANGELOG.md
index 909a2fea..ae395e24 100644
--- a/hive/CHANGELOG.md
+++ b/hive/CHANGELOG.md
@@ -1,5 +1,6 @@
-## NEXT
+## 2.12.0
 
+- Notify with the value of a deleted frame instead of `null`
 - Improves adapter of same type warning
 
 ## 2.11.4
diff --git a/hive/lib/src/box/box_base.dart b/hive/lib/src/box/box_base.dart
index e68a6a0b..d52376eb 100644
--- a/hive/lib/src/box/box_base.dart
+++ b/hive/lib/src/box/box_base.dart
@@ -6,7 +6,7 @@ class BoxEvent {
   /// The key of the changed entry
   final dynamic key;
 
-  /// The value of a new entry of `null` if the entry has been deleted
+  /// The value of the entry
   final dynamic value;
 
   /// Whether the entry has been deleted
diff --git a/hive/lib/src/box/change_notifier.dart b/hive/lib/src/box/change_notifier.dart
index 9498c5c5..e821610b 100644
--- a/hive/lib/src/box/change_notifier.dart
+++ b/hive/lib/src/box/change_notifier.dart
@@ -17,8 +17,14 @@ class ChangeNotifier {
   const ChangeNotifier.debug(this._streamController);
 
   /// Not part of public API
-  void notify(Frame frame) {
-    _streamController.add(BoxEvent(frame.key, frame.value, frame.deleted));
+  void notify(Frame frame, {Object? deletedValue}) {
+    _streamController.add(
+      BoxEvent(
+        frame.key,
+        frame.deleted ? deletedValue : frame.value,
+        frame.deleted,
+      ),
+    );
   }
 
   /// Not part of public API
diff --git a/hive/lib/src/box/keystore.dart b/hive/lib/src/box/keystore.dart
index 7e5bb387..0657a1a6 100644
--- a/hive/lib/src/box/keystore.dart
+++ b/hive/lib/src/box/keystore.dart
@@ -174,7 +174,7 @@ class Keystore<E> {
     }
 
     if (notify && (!frame.deleted || deletedFrame != null)) {
-      _notifier.notify(frame);
+      _notifier.notify(frame, deletedValue: deletedFrame?.value);
     }
 
     return deletedFrame;
@@ -244,8 +244,8 @@ class Keystore<E> {
         }
       }
       if (!isOverride) {
-        _store.delete(key);
-        _notifier.notify(Frame.deleted(key));
+        final deletedFrame = _store.delete(key);
+        _notifier.notify(Frame.deleted(key), deletedValue: deletedFrame?.value);
       }
     }
   }
@@ -260,7 +260,7 @@ class Keystore<E> {
       if (frame.value is HiveObjectMixin) {
         (frame.value as HiveObjectMixin).dispose();
       }
-      _notifier.notify(Frame.deleted(frame.key));
+      _notifier.notify(Frame.deleted(frame.key), deletedValue: frame.value);
     }
 
     _deletedEntries = 0;
diff --git a/hive/pubspec.yaml b/hive/pubspec.yaml
index 25862b65..5738699c 100644
--- a/hive/pubspec.yaml
+++ b/hive/pubspec.yaml
@@ -1,6 +1,6 @@
 name: hive_ce
 description: Hive Community Edition - A spiritual continuation of Hive v2
-version: 2.11.4
+version: 2.12.0
 homepage: https://github.com/IO-Design-Team/hive_ce/tree/main/hive
 
 topics:


Test patch:
diff --git a/hive/test/integration/watch_box_updates_test.dart b/hive/test/integration/watch_box_updates_test.dart
index 1cd8b148..02ec1ef6 100644
--- a/hive/test/integration/watch_box_updates_test.dart
+++ b/hive/test/integration/watch_box_updates_test.dart
@@ -82,6 +82,7 @@ void main() {
             await box.delete('key');
           },
           key: 'key',
+          value: 'value',
           deleted: true,
         );
       });
diff --git a/hive/test/tests/box/keystore_test.dart b/hive/test/tests/box/keystore_test.dart
index 25d5d96f..2fd7c47f 100644
--- a/hive/test/tests/box/keystore_test.dart
+++ b/hive/test/tests/box/keystore_test.dart
@@ -269,7 +269,9 @@ void main() {
           verify(() => notifier.notify(Frame('key1', 'val1')));
 
           keystore.insert(Frame('key1', 'val2'));
-          verify(() => notifier.notify(Frame('key1', 'val2')));
+          verify(
+            () => notifier.notify(Frame('key1', 'val2'), deletedValue: 'val1'),
+          );
         });
       });
 
@@ -323,7 +325,9 @@ void main() {
           reset(notifier);
 
           keystore.insert(Frame.deleted('key1'));
-          verify(() => notifier.notify(Frame.deleted('key1')));
+          verify(
+            () => notifier.notify(Frame.deleted('key1'), deletedValue: 'val1'),
+          );
 
           keystore.insert(Frame.deleted('key1'));
           verifyNoMoreInteractions(notifier);
@@ -366,7 +370,9 @@ void main() {
           'key1': Frame('key1', 'val1'),
         });
         expect(keystore.frames, [Frame('key1', 'val2'), Frame('key2', 'val3')]);
-        verify(() => notifier.notify(Frame('key1', 'val2')));
+        verify(
+          () => notifier.notify(Frame('key1', 'val2'), deletedValue: 'val1'),
+        );
         verify(() => notifier.notify(Frame('key2', 'val3')));
       });
 
@@ -406,7 +412,9 @@ void main() {
           'key1': Frame('key1', 'val1'),
         });
         expect(keystore.frames, [Frame('key2', 'val2')]);
-        verify(() => notifier.notify(Frame.deleted('key1')));
+        verify(
+          () => notifier.notify(Frame.deleted('key1'), deletedValue: 'val1'),
+        );
       });
     });
 
@@ -445,7 +453,9 @@ void main() {
           keystore.transactions,
           [KeyTransaction()..added.add('otherKey')],
         );
-        verify(() => notifier.notify(Frame.deleted('key')));
+        verify(
+          () => notifier.notify(Frame.deleted('key'), deletedValue: 'val1'),
+        );
       });
 
       test('add then override', () {
@@ -635,8 +645,12 @@ void main() {
         reset(notifier);
 
         keystore.clear();
-        verify(() => notifier.notify(Frame.deleted('key1')));
-        verify(() => notifier.notify(Frame.deleted('key2')));
+        verify(
+          () => notifier.notify(Frame.deleted('key1'), deletedValue: 'val1'),
+        );
+        verify(
+          () => notifier.notify(Frame.deleted('key2'), deletedValue: 'val2'),
+        );
       });
     });
   });