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: Cancel running events after closing the bloc #4214

Closed
karvulf opened this issue Jul 24, 2024 · 1 comment
Closed

feat: Cancel running events after closing the bloc #4214

karvulf opened this issue Jul 24, 2024 · 1 comment
Labels
duplicate This issue or pull request already exists

Comments

@karvulf
Copy link

karvulf commented Jul 24, 2024

Description
At first, thanks for this awesome package, this is really useful for our project.

My feature request is about handling asynchronous events. In my case I have an event which calls a usecase very often and it takes some time before this event was handled.
If I close this bloc, the asynchronous calls are still running which leads to issues.

Desired Solution

So I wanted to ask if it's possible to call something to close all current running events, e.g. cancelRunningEvents();.
Or the default behavior could be that it just cancels automatically all running events after closing the bloc.
Another idea would be to add some configuration to the bloc or to a specific event which tells it to stop the event after closing the bloc.

Additional Context

This is an example code which illustrates my issue:

class TestBloc extends Bloc<TestEvent, TestState> {
  TestBloc() : super(TestInitial()) {
    on<TestRunEvent>(this._onTestRunEvent);
  }

  @override
  Future<void> close() async {
    cancelAllRunningEvents(); // I would wish to something like this
    await super.close();
  }

  Future<void> _onTestRunEvent(
    TestRunEvent event,
    Emitter<TestState> emit,
  ) async {
    for (int i = 0; i < 10; i++) {
      if(isClosed) return; // I would like to prevent doing these checks

      print('Running with counter = $i');
      // currently this call is still running even though my bloc was closed
      await Future.delayed(const Duration(seconds: 2));
    }
  }
}
@felangel
Copy link
Owner

This is a duplicate of #4165. Happy to continue the discussion there 👍

@felangel felangel added the duplicate This issue or pull request already exists label Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants