Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hydrated_bloc): Clearing existing storage when build storage called #4016

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -85,7 +85,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 @@ -105,7 +104,7 @@ class HydratedStorage implements Storage {
await _migrate(storageDirectory, box);
}

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

Expand Down Expand Up @@ -133,7 +132,6 @@ class HydratedStorage implements Storage {
static late HiveInterface hive;

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

final Box<dynamic> _box;

Expand All @@ -157,15 +155,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
Empty file.
16 changes: 11 additions & 5 deletions packages/hydrated_bloc/test/hydrated_storage_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,19 @@ void main() {
await storage.close();
});

test('reuses 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,
);
expect(instanceA, instanceB);

await instanceA.close();
await instanceB.close();
expect(instanceA, isNot(instanceB));
});

test('creates new instance if storage was closed', () async {
Expand Down Expand Up @@ -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<List<String>>;
final written = storage.read(token) as List;
expect(written, isNotNull);
expect(written, record);
}).drain<dynamic>();
Expand Down