From 9beeb22b55aa37801f8d9f0b48ad244adf9644a1 Mon Sep 17 00:00:00 2001 From: Aravind Date: Sat, 2 Dec 2023 22:10:50 +0530 Subject: [PATCH 1/3] Clearing Existing Instance --- packages/hydrated_bloc/hydrated_box.hive | 0 packages/hydrated_bloc/lib/src/hydrated_storage.dart | 4 +++- packages/hydrated_bloc/test/hydrated_storage_test.dart | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 packages/hydrated_bloc/hydrated_box.hive diff --git a/packages/hydrated_bloc/hydrated_box.hive b/packages/hydrated_bloc/hydrated_box.hive new file mode 100644 index 00000000000..e69de29bb2d diff --git a/packages/hydrated_bloc/lib/src/hydrated_storage.dart b/packages/hydrated_bloc/lib/src/hydrated_storage.dart index 7ef30640f57..59652394a33 100644 --- a/packages/hydrated_bloc/lib/src/hydrated_storage.dart +++ b/packages/hydrated_bloc/lib/src/hydrated_storage.dart @@ -85,7 +85,9 @@ class HydratedStorage implements Storage { HydratedCipher? encryptionCipher, }) { return _lock.synchronized(() async { - if (_instance != null) return _instance!; + if (_instance != null) { + await _instance!.close(); + } // Use HiveImpl directly to avoid conflicts with existing Hive.init // https://github.com/hivedb/hive/issues/336 hive = HiveImpl(); diff --git a/packages/hydrated_bloc/test/hydrated_storage_test.dart b/packages/hydrated_bloc/test/hydrated_storage_test.dart index 84c006bd7e5..5e88647be3a 100644 --- a/packages/hydrated_bloc/test/hydrated_storage_test.dart +++ b/packages/hydrated_bloc/test/hydrated_storage_test.dart @@ -50,14 +50,14 @@ void main() { await storage.close(); }); - test('reuses existing instance when called multiple times', () async { + test('clear existing instance when called multiple times', () async { final instanceA = storage = await HydratedStorage.build( storageDirectory: storageDirectory, ); final instanceB = await HydratedStorage.build( storageDirectory: storageDirectory, ); - expect(instanceA, instanceB); + expect(instanceA, isNot(instanceB)); }); test('creates new instance if storage was closed', () async { From 78f7323467e5655185dd3229ba5a97f252c0c34c Mon Sep 17 00:00:00 2001 From: Aravind <63452117+codesculpture@users.noreply.github.com> Date: Sat, 2 Dec 2023 22:15:40 +0530 Subject: [PATCH 2/3] Delete packages/hydrated_bloc/hydrated_box.hive --- packages/hydrated_bloc/hydrated_box.hive | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 packages/hydrated_bloc/hydrated_box.hive diff --git a/packages/hydrated_bloc/hydrated_box.hive b/packages/hydrated_bloc/hydrated_box.hive deleted file mode 100644 index e69de29bb2d..00000000000 From 6d89e47d3e75632ed1bdfa8cd8a0e28fb023555f Mon Sep 17 00:00:00 2001 From: Aravind Date: Sat, 9 Dec 2023 19:24:41 +0530 Subject: [PATCH 3/3] Removed private instance --- .../hydrated_bloc/lib/src/hydrated_storage.dart | 8 +------- packages/hydrated_bloc/{ => new}/hydrated_box.hive | 0 .../hydrated_bloc/test/hydrated_storage_test.dart | 14 ++++++++++---- 3 files changed, 11 insertions(+), 11 deletions(-) rename packages/hydrated_bloc/{ => new}/hydrated_box.hive (100%) diff --git a/packages/hydrated_bloc/lib/src/hydrated_storage.dart b/packages/hydrated_bloc/lib/src/hydrated_storage.dart index 59652394a33..98f59f12e1c 100644 --- a/packages/hydrated_bloc/lib/src/hydrated_storage.dart +++ b/packages/hydrated_bloc/lib/src/hydrated_storage.dart @@ -85,9 +85,6 @@ class HydratedStorage implements Storage { HydratedCipher? encryptionCipher, }) { return _lock.synchronized(() async { - if (_instance != null) { - await _instance!.close(); - } // Use HiveImpl directly to avoid conflicts with existing Hive.init // https://github.com/hivedb/hive/issues/336 hive = HiveImpl(); @@ -107,7 +104,7 @@ class HydratedStorage implements Storage { await _migrate(storageDirectory, box); } - return _instance = HydratedStorage(box); + return HydratedStorage(box); }); } @@ -135,7 +132,6 @@ class HydratedStorage implements Storage { static late HiveInterface hive; static final _lock = Lock(); - static HydratedStorage? _instance; final Box _box; @@ -159,7 +155,6 @@ class HydratedStorage implements Storage { @override Future clear() async { if (_box.isOpen) { - _instance = null; return _lock.synchronized(_box.clear); } } @@ -167,7 +162,6 @@ class HydratedStorage implements Storage { @override Future close() async { if (_box.isOpen) { - _instance = null; return _lock.synchronized(_box.close); } } diff --git a/packages/hydrated_bloc/hydrated_box.hive b/packages/hydrated_bloc/new/hydrated_box.hive similarity index 100% rename from packages/hydrated_bloc/hydrated_box.hive rename to packages/hydrated_bloc/new/hydrated_box.hive diff --git a/packages/hydrated_bloc/test/hydrated_storage_test.dart b/packages/hydrated_bloc/test/hydrated_storage_test.dart index 5e88647be3a..26d7d14e485 100644 --- a/packages/hydrated_bloc/test/hydrated_storage_test.dart +++ b/packages/hydrated_bloc/test/hydrated_storage_test.dart @@ -50,13 +50,18 @@ void main() { await storage.close(); }); - test('clear existing instance when called multiple times', () async { + test('override existing instance when called multiple times', () async { final instanceA = storage = await HydratedStorage.build( - storageDirectory: storageDirectory, + storageDirectory: + await Directory(storageDirectory.path + "/new").create(), ); + final instanceB = await HydratedStorage.build( storageDirectory: storageDirectory, ); + + await instanceA.close(); + await instanceB.close(); expect(instanceA, isNot(instanceB)); }); @@ -191,12 +196,13 @@ void main() { // ignore: unawaited_futures storage.write(token, record); - + await storage.close(); + await storage.clear(); storage = await HydratedStorage.build( storageDirectory: Directory(cwd), ); - final written = storage.read(token) as List>; + final written = storage.read(token) as List; expect(written, isNotNull); expect(written, record); }).drain();