Skip to content

Commit

Permalink
remove internal caching
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel committed Jan 9, 2025
1 parent d61b91b commit 852ce4e
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 10 deletions.
2 changes: 0 additions & 2 deletions packages/hydrated_bloc/lib/src/hydrated_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ abstract class HydratedBloc<Event, State> extends Bloc<Event, State>

/// 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;
}

Expand Down
6 changes: 1 addition & 5 deletions packages/hydrated_bloc/lib/src/hydrated_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -116,7 +115,7 @@ class HydratedStorage implements Storage {
await migrate(storageDirectory.path, box);
}

return _instance = HydratedStorage(box);
return HydratedStorage(box);
});
}

Expand All @@ -126,7 +125,6 @@ class HydratedStorage implements Storage {
static late HiveInterface hive;

static final _lock = Lock();
static HydratedStorage? _instance;

final Box<dynamic> _box;

Expand All @@ -150,15 +148,13 @@ class HydratedStorage implements Storage {
@override
Future<void> clear() async {
if (_box.isOpen) {
_instance = null;
return _lock.synchronized(_box.clear);
}
}

@override
Future<void> close() async {
if (_box.isOpen) {
_instance = null;
return _lock.synchronized(_box.close);
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/hydrated_bloc/test/hydrated_storage_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -196,7 +196,7 @@ void main() {
storageDirectory: HydratedStorageDirectory(cwd),
);

final written = storage.read(token) as List<List<String>>;
final written = storage.read(token);
expect(written, isNotNull);
expect(written, record);
}).drain<dynamic>();
Expand Down

0 comments on commit 852ce4e

Please sign in to comment.