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(hydrated_bloc): add ttl and shouldPersistOnEvent to HydratedBloc… #4344

Closed
wants to merge 1 commit into from

Conversation

0niel
Copy link

@0niel 0niel commented Jan 21, 2025

Status

READY

Breaking Changes

NO

Description

This PR adds two new features to hydrated_bloc:

  1. Time-to-Live (TTL):

    • Purpose: Allows state persistence to expire after a specified duration.
    • Usage: Pass a ttl parameter when initializing HydratedBloc or HydratedCubit.
    class UserSessionCubit extends HydratedCubit<UserSession> {
      UserSessionCubit() : super(UserSession.initial(), ttl: Duration(hours: 2));
    
      void updateSession(UserSession session) => emit(session);
    
      @override
      UserSession fromJson(Map<String, dynamic> json) => UserSession.fromJson(json);
    
      @override
      Map<String, dynamic>? toJson(UserSession state) => state.toJson();
    }
  2. shouldPersistOnEvent:

    • Purpose: Enables selective state persistence based on events.
    • Usage: Override shouldPersistOnEvent in your HydratedBloc to specify which events trigger persistence.

The shouldPersistOnEvent method enables selective persistence by allowing developers to specify which events should trigger state saving. By avoiding unnecessary persistence operations for irrelevant or frequent events, this feature reduces the computational overhead and prevents potential UI lags, ensuring smoother application performance.

class CounterBloc extends HydratedBloc<CounterEvent, int> {
  CounterBloc() : super(0) {
    on<CounterIncrementPressed>((event, emit) => emit(state + 1));
    on<CounterDecrementPressed>((event, emit) => emit(state - 1));
  }

  @override
  bool shouldPersistOnEvent(CounterEvent? event) {
    return event is CounterIncrementPressed || event is CounterDecrementPressed;
  }

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

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

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 📝 Documentation
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 🗑️ Chore

@0niel 0niel requested a review from felangel as a code owner January 21, 2025 14:49
@felangel
Copy link
Owner

Hi @0niel 👋
Thanks for opening a pull request!

Since this is introducing new APIs/functionality I highly recommend filing an issue first clearly describing the problem you're facing/trying to solve. This way we can have a discussion and agree upon a solution/implementation before writing any code. Closing this for now but looking forward to learning more about the issues you're facing, thanks 🙏

@felangel felangel closed this Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants