Skip to content

Commit

Permalink
Adds isInitialized field to Hive
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexios80 committed Oct 28, 2024
1 parent 3a32285 commit 1d62e6b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
5 changes: 1 addition & 4 deletions hive/lib/src/box_collection/box_collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class BoxCollection implements implementation.BoxCollection {
/// TODO: Document this!
BoxCollection(this.name, this.boxNames);

static bool _hiveInit = false;

late Box<String> _badKeyBox;

/// TODO: Document this!
Expand All @@ -29,9 +27,8 @@ class BoxCollection implements implementation.BoxCollection {
String? path,
HiveCipher? key,
}) async {
if (!_hiveInit) {
if (!Hive.isInitialized) {
Hive.init(path ?? './');
_hiveInit = true;
}
final collection = BoxCollection(name, boxNames);
if (key != null) {
Expand Down
3 changes: 3 additions & 0 deletions hive/lib/src/hive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ abstract class HiveInterface implements TypeRegistry {
HiveStorageBackendPreference.native,
});

/// Returns true if Hive is initialized.
bool get isInitialized;

/// Opens a box.
///
/// If the box is already open, the instance is returned and all provided
Expand Down
3 changes: 3 additions & 0 deletions hive/lib/src/hive_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface {
@visibleForTesting
String? homePath;

@override
bool get isInitialized => homePath != null;

/// Not part of public API
HiveImpl() {
_registerDefaultAdapters();
Expand Down
9 changes: 8 additions & 1 deletion hive/test/tests/box_collection/box_collection_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:hive_ce/src/box_collection/box_collection.dart';
import 'package:hive_ce/hive.dart';
import 'package:hive_ce/src/hive_impl.dart';
import 'package:test/test.dart';

Future<BoxCollection> _openCollection({bool withData = false}) async {
Expand Down Expand Up @@ -116,5 +117,11 @@ void main() {
await box.clear();
expect(await box.getAllKeys(), []);
});

test('Does not reinitialize Hive path', () async {
Hive.init('MYPATH');
await _openCollection();
expect((Hive as HiveImpl).homePath, 'MYPATH');
});
});
}

0 comments on commit 1d62e6b

Please sign in to comment.