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

feat: Added to allow setting the validity period of hydrated data. #4332

Closed
wants to merge 2 commits into from
Closed
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
21 changes: 21 additions & 0 deletions packages/hydrated_bloc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,27 @@ class CounterCubit extends HydratedCubit<int> {
}
```

### Cubit storage set expireIn

You can override the `hydrationExpiresIn` to customize the expiration period of a specific hydration using instances of `HydratedBloc` or `HydratedCubit`.

```dart
class CounterCubit extends HydratedCubit<int> {
CounterCubit() : super(0, storage: EncryptedStorage());

void increment() => emit(state + 1);

@override
int fromJson(Map<String, dynamic> json) => json['value'] as int;

@override
Map<String, int> toJson(int state) => { 'value': state };

@override
Duration? get hydrationExpiresIn => const Duration(minutes: 1);
}
```

## Custom Storage Directory

Any `storageDirectory` can be used when creating an instance of `HydratedStorage`:
Expand Down
44 changes: 42 additions & 2 deletions packages/hydrated_bloc/lib/src/hydrated_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,25 @@
void hydrate({Storage? storage}) {
__storage = storage ??= HydratedBloc.storage;
try {
final stateJson = __storage.read(storageToken) as Map<dynamic, dynamic>?;
Map<dynamic, dynamic>? stateJson =
__storage.read(storageToken) as Map<dynamic, dynamic>?;

try {
final stateExpireIn = __storage.read(expirationStorageToken) as int?;
if (stateExpireIn != null) {
final expireIn = DateTime.fromMicrosecondsSinceEpoch(stateExpireIn);
if (DateTime.now().isAfter(expireIn)) {

Check warning on line 131 in packages/hydrated_bloc/lib/src/hydrated_bloc.dart

View check run for this annotation

Codecov / codecov/patch

packages/hydrated_bloc/lib/src/hydrated_bloc.dart#L130-L131

Added lines #L130 - L131 were not covered by tests
stateJson = null;
__storage.delete(storageToken).then((_) {}, onError: onError);
__storage
.delete(expirationStorageToken)
.then((_) {}, onError: onError);

Check warning on line 136 in packages/hydrated_bloc/lib/src/hydrated_bloc.dart

View check run for this annotation

Codecov / codecov/patch

packages/hydrated_bloc/lib/src/hydrated_bloc.dart#L133-L136

Added lines #L133 - L136 were not covered by tests
}
}
} catch (error, stackTrace) {
onError(error, stackTrace);
}

_state = stateJson != null ? _fromJson(stateJson) : super.state;
} catch (error, stackTrace) {
onError(error, stackTrace);
Expand All @@ -133,6 +151,12 @@
if (stateJson != null) {
__storage.write(storageToken, stateJson).then((_) {}, onError: onError);
}
if (hydrationExpiresIn != null) {
__storage
.write(expirationStorageToken,
DateTime.now().add(hydrationExpiresIn!).microsecondsSinceEpoch)
.then((_) {}, onError: onError);

Check warning on line 158 in packages/hydrated_bloc/lib/src/hydrated_bloc.dart

View check run for this annotation

Codecov / codecov/patch

packages/hydrated_bloc/lib/src/hydrated_bloc.dart#L155-L158

Added lines #L155 - L158 were not covered by tests
}
} catch (error, stackTrace) {
onError(error, stackTrace);
if (error is StorageNotFound) rethrow;
Expand All @@ -153,6 +177,12 @@
if (stateJson != null) {
__storage.write(storageToken, stateJson).then((_) {}, onError: onError);
}
if (hydrationExpiresIn != null) {
__storage
.write(expirationStorageToken,
DateTime.now().add(hydrationExpiresIn!).microsecondsSinceEpoch)
.then((_) {}, onError: onError);

Check warning on line 184 in packages/hydrated_bloc/lib/src/hydrated_bloc.dart

View check run for this annotation

Codecov / codecov/patch

packages/hydrated_bloc/lib/src/hydrated_bloc.dart#L181-L184

Added lines #L181 - L184 were not covered by tests
}
} catch (error, stackTrace) {
onError(error, stackTrace);
rethrow;
Expand Down Expand Up @@ -309,10 +339,17 @@
@nonVirtual
String get storageToken => '$storagePrefix$id';

/// `expirationStorageToken` is used as hydrated storage expiration date.
@nonVirtual
String get expirationStorageToken => '$storagePrefix$id-expireIn';

/// [clear] is used to wipe or invalidate the cache of a [HydratedBloc].
/// Calling [clear] will delete the cached state of the bloc
/// but will not modify the current state of the bloc.
Future<void> clear() => __storage.delete(storageToken);
Future<void> clear() async {
await __storage.delete(expirationStorageToken);
await __storage.delete(storageToken);
}

/// Responsible for converting the `Map<String, dynamic>` representation
/// of the bloc state into a concrete instance of the bloc state.
Expand All @@ -323,6 +360,9 @@
///
/// If [toJson] returns `null`, then no state changes will be persisted.
Map<String, dynamic>? toJson(State state);

/// `hydrationExpiresIn` sets the expiration duration for the stored state.
Duration? hydrationExpiresIn;
}

/// Reports that an object could not be serialized due to cyclic references.
Expand Down
Loading