Skip to content

Commit

Permalink
Merge pull request #76 from IO-Design-Team/feature/lazy-box-dispose-h…
Browse files Browse the repository at this point in the history
…ive-object

Ensure HiveObject disposal in lazy boxes
  • Loading branch information
Rexios80 authored Feb 6, 2025
2 parents 6e8f715 + 2ab4c37 commit 932f17d
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 10 deletions.
4 changes: 4 additions & 0 deletions hive/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.10.1

- Fixes `HiveObject` disposal in lazy boxes

## 2.10.0

- Raises the maximum type ID from 223 to 65439
Expand Down
8 changes: 6 additions & 2 deletions hive/lib/src/object/hive_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ mixin HiveObjectMixin {
}

/// Deletes this object from the box it is stored in.
Future<void> delete() {
Future<void> delete() async {
_requireInitialized();
return _box!.delete(_key);
await _box!.delete(_key);
if (_box?.lazy == true) {
// Lazy boxes won't automatically dispose their HiveObjects
dispose();
}
}

/// Returns whether this object is currently stored in a box.
Expand Down
2 changes: 1 addition & 1 deletion hive/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: hive_ce
description: Hive Community Edition - A spiritual continuation of Hive v2
version: 2.10.0
version: 2.10.1
homepage: https://github.com/IO-Design-Team/hive_ce/tree/main/hive
documentation: https://docs.hivedb.dev/

Expand Down
24 changes: 24 additions & 0 deletions hive/test/integration/hive_object_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,28 @@ void main() {
},
timeout: longTimeout,
);

test('move HiveObject between lazy boxes', () async {
final hive = await createHive();
hive.registerAdapter<_TestObject>(_TestObjectAdapter());

final box1 = await openBox(true, hive: hive);
final box2 = await openBox(true, hive: hive);

final obj = _TestObject('test');
expect(obj.box, null);
expect(obj.key, null);

final key1 = await box1.add(obj);
expect(obj.box, box1);
expect(obj.key, key1);

await obj.delete();
expect(obj.box, null);
expect(obj.key, null);

final key2 = await box2.add(obj);
expect(obj.box, box2);
expect(obj.key, key2);
});
}
4 changes: 3 additions & 1 deletion hive/test/tests/box/box_base_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import '../common.dart';
import '../mocks.dart';

class _BoxBaseMock<E> extends BoxBaseImpl<E> with Mock {
@override
final lazy = false;

_BoxBaseMock(
super.hive,
super.name,
Expand Down Expand Up @@ -158,7 +161,6 @@ void main() {
test('.initialize()', () async {
final backend = MockStorageBackend();
final box = _openBoxBaseMock(backend: backend);
when(() => box.lazy).thenReturn(false);

when(() => backend.initialize(any(), any(), any())).thenAnswer((i) async {
i.positionalArguments[1].insert(Frame('key1', 1));
Expand Down
7 changes: 6 additions & 1 deletion hive/test/tests/mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import 'package:mocktail/mocktail.dart';

// Mocks

class MockBox<E> extends Mock implements Box<E> {}
class MockBox<E> extends Mock implements Box<E> {
@override
final bool lazy;

MockBox({this.lazy = false});
}

class MockChangeNotifier extends Mock implements ChangeNotifier {}

Expand Down
4 changes: 1 addition & 3 deletions hive/test/tests/object/hive_object_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ void main() {
test('returns true if object is in normal box', () {
final obj = TestHiveObject();
final box = MockBox();
when(() => box.lazy).thenReturn(false);
obj.init('key', box);

expect(obj.isInBox, true);
Expand All @@ -162,8 +161,7 @@ void main() {
test('returns the result ob box.containsKey() if object is in lazy box',
() {
final obj = TestHiveObject();
final box = MockBox();
when(() => box.lazy).thenReturn(true);
final box = MockBox(lazy: true);
obj.init('key', box);

when(() => box.containsKey('key')).thenReturn(true);
Expand Down
4 changes: 2 additions & 2 deletions hive_flutter/test/mocks.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,8 @@ class MockBinaryWriter extends _i2.Mock implements _i8.BinaryWriter {
);

@override
void write<T>(T? value, {bool? writeTypeId = true}) => super.noSuchMethod(
Invocation.method(#write, [value], {#writeTypeId: writeTypeId}),
void write<T>(T? value, {bool? withTypeId = true}) => super.noSuchMethod(
Invocation.method(#write, [value], {#withTypeId: withTypeId}),
returnValueForMissingStub: null,
);
}

0 comments on commit 932f17d

Please sign in to comment.