From 852ce4e15443120c8931dbde0372104e48a111bf Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Wed, 8 Jan 2025 23:26:10 -0600 Subject: [PATCH] remove internal caching --- packages/hydrated_bloc/lib/src/hydrated_bloc.dart | 2 -- packages/hydrated_bloc/lib/src/hydrated_storage.dart | 6 +----- packages/hydrated_bloc/test/hydrated_storage_test.dart | 6 +++--- 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/packages/hydrated_bloc/lib/src/hydrated_bloc.dart b/packages/hydrated_bloc/lib/src/hydrated_bloc.dart index 90f9f3eba51..c114c87ebca 100644 --- a/packages/hydrated_bloc/lib/src/hydrated_bloc.dart +++ b/packages/hydrated_bloc/lib/src/hydrated_bloc.dart @@ -41,9 +41,7 @@ abstract class HydratedBloc extends Bloc /// Setter for instance of [Storage] which will be used to /// manage persisting/restoring the [Bloc] state. - /// Closes the previous [Storage] instance if applicable. static set storage(Storage? storage) { - _storage?.close(); _storage = storage; } diff --git a/packages/hydrated_bloc/lib/src/hydrated_storage.dart b/packages/hydrated_bloc/lib/src/hydrated_storage.dart index 2de18ace614..19bf78f6ebe 100644 --- a/packages/hydrated_bloc/lib/src/hydrated_storage.dart +++ b/packages/hydrated_bloc/lib/src/hydrated_storage.dart @@ -96,7 +96,6 @@ class HydratedStorage implements Storage { HydratedCipher? encryptionCipher, }) { return _lock.synchronized(() async { - if (_instance != null) return _instance!; // Use HiveImpl directly to avoid conflicts with existing Hive.init // https://github.com/hivedb/hive/issues/336 hive = HiveImpl(); @@ -116,7 +115,7 @@ class HydratedStorage implements Storage { await migrate(storageDirectory.path, box); } - return _instance = HydratedStorage(box); + return HydratedStorage(box); }); } @@ -126,7 +125,6 @@ class HydratedStorage implements Storage { static late HiveInterface hive; static final _lock = Lock(); - static HydratedStorage? _instance; final Box _box; @@ -150,7 +148,6 @@ class HydratedStorage implements Storage { @override Future clear() async { if (_box.isOpen) { - _instance = null; return _lock.synchronized(_box.clear); } } @@ -158,7 +155,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/test/hydrated_storage_test.dart b/packages/hydrated_bloc/test/hydrated_storage_test.dart index 13a30eed3c9..45767d10c67 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('returns new 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 { @@ -196,7 +196,7 @@ void main() { storageDirectory: HydratedStorageDirectory(cwd), ); - final written = storage.read(token) as List>; + final written = storage.read(token); expect(written, isNotNull); expect(written, record); }).drain();