felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.53k stars 3.37k forks source link

feat: allow user to choose which event or state change to be captured in the changeStack or not #4178

Open whowillcare opened 1 month ago

whowillcare commented 1 month ago

Description

The current solution doesn't have a feature that allow user to specify which events to be added to the changeStack, and which ones are not, which will give user flexiblity to redo or undo specific states changes

Desired Solution

/// allow user to overwrite this method
  bool shouldChangeStack(State current, State next)=>true

@override
  void emit(State state) {
   if (shouldChangeStack(this.state,state)){
     _changeStack.add(
      _Change<State>(
        this.state,
        state,
        () {
          final event = _Redo();
          onEvent(event);
          onTransition(
            Transition(
              currentState: this.state,
              event: event,
              nextState: state,
            ),
          );
          // ignore: invalid_use_of_visible_for_testing_member
          super.emit(state);
        },
        (val) {
          final event = _Undo();
          onEvent(event);
          onTransition(
            Transition(
              currentState: this.state,
              event: event,
              nextState: val,
            ),
          );
          // ignore: invalid_use_of_visible_for_testing_member
          super.emit(val);
        },
      ),
    );
 }
    // ignore: invalid_use_of_visible_for_testing_member
    super.emit(state);
  }

Alternatives Considered

or keep the super.emit to an alternative method name, so the child class can always override the emit method for implementing the similar feature

Additional Context

which will save the resource and allow user to have more flexiblities

felangel commented 1 month ago

Hi @whowillcare 👋 Thanks for opening an issue!

Are you able to provide a bit more context around your use-case? ReplayBloc has a shouldReplay API which seems like it might be what you're looking for.

whowillcare commented 1 month ago

The simplest user case would be like: State: list: ['a','b','c'] currentIndex: 0 Event: add: 'd' # add an element to list remove: 'a' # remove 'a' from list Event: select: 1 # select to new index change the currentIndex to 1

So I would like the Undo or Redo events don't need to catch the select event, but still catch the add or remove event, which gives more flexibility by making certain changes are Replay-able, certains are not.

On Tue, Jun 4, 2024 at 1:08 AM Felix Angelov @.***> wrote:

Hi @whowillcare https://github.com/whowillcare 👋 Thanks for opening an issue!

Are you able to provide a bit more context around your use-case? ReplayBloc has a shouldReplay https://pub.dev/documentation/replay_bloc/latest/replay_bloc/ReplayBlocMixin/shouldReplay.html API which seems like it might be what you're looking for.

— Reply to this email directly, view it on GitHub https://github.com/felangel/bloc/issues/4178#issuecomment-2146621416, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABUL3KF6XUGOSIEG4VRIIJLZFVDTJAVCNFSM6AAAAABIFVWRUWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNBWGYZDCNBRGY . You are receiving this because you were mentioned.Message ID: @.***>