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(cubit): optional StateError when emit happens #3042

Closed

Conversation

maRci002
Copy link

@maRci002 maRci002 commented Dec 4, 2021

Status

READY

Breaking Changes

NO

Description

This PR focuses on Local Cubits where the cubit might become closed / disposed during processing async Cubit functions.

Current problem and workarounds:

class SomeCubit extends Cubit<int> {
  SomeCubit() : super(0);

  Future<void> test() async {
    try {
      emit(1);
      await Future.delayed(const Duration(seconds: 5));
      // if user leaves the page / dialog E.g. `pushReplacementNamed` then cubit is already closed
      // check `isClosed` every time after a future completes?
      if (!isClosed) {
        emit(2);
      }
    } on StateError {
      // here we could ignore StateError('Cannot emit new states after calling close')
      // but this seems overkill for every single cubit function
    }
  }
}

Syntax if programmer knows the Cubit is a local cubit / temporary cubit:

SomeCubit() : super(0, emitFailsWhenClosed: false)

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ 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
  • 📝 Documentation
  • 🗑️ Chore

closes #2980

@maRci002 maRci002 requested a review from felangel as a code owner December 4, 2021 20:52
@maRci002 maRci002 marked this pull request as draft December 5, 2021 09:35
@maRci002
Copy link
Author

maRci002 commented Dec 5, 2021

I'm sorry I need to do a little refactor since bloc already awaits ongoing event emitters.

In addition, if [close] is called while events are still being processed, the [Bloc] will finish processing the pending events.

However Cubit doesn't have this feature so I'm going to apply the changes only for it.

@maRci002 maRci002 marked this pull request as ready for review December 5, 2021 10:20
@maRci002 maRci002 changed the title Optional StateError when emit happens feat(cubit): optional StateError when emit happens Dec 5, 2021
@maRci002
Copy link
Author

@felangel will you please review this PR?

Maybe Cubit should give explicit emitFailsWhenClosed: false to his base contructor (so all Cubits are safe) or BlocBase should mark bool get emitFailsWhenClosed accessor as abstract.
Or just completly refactor Cubit's logic for instance keep track of async Cubit functions.

@felangel
Copy link
Owner

@felangel will you please review this PR?

Maybe Cubit should give explicit emitFailsWhenClosed: false to his base contructor (so all Cubits are safe) or BlocBase should mark bool get emitFailsWhenClosed accessor as abstract. Or just completly refactor Cubit's logic for instance keep track of async Cubit functions.

I'd prefer to hold off on this change for a little bit and evaluate other potential solutions. I want to make sure we carefully consider all options/approaches to resolve this before adding a new public API. Really appreciate you taking the time to create this PR 👍

I'll think about it a bit more over the next few days and get back to you, thanks! 🙏

@felangel
Copy link
Owner

@maRci002 I'd love your thoughts on #3069, thanks! 🙏

@felangel
Copy link
Owner

Closing this for now since we need to discuss/align on an approach first (#3069)

@felangel felangel closed this Dec 20, 2021
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.

question: StateError
3 participants