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

Stream waiting and blocking other events using Bloc in Flutter #1853

Closed
yaminet1024 opened this issue Oct 22, 2020 · 3 comments
Closed

Stream waiting and blocking other events using Bloc in Flutter #1853

yaminet1024 opened this issue Oct 22, 2020 · 3 comments
Assignees
Labels
pkg:bloc This issue is related to the bloc package question Further information is requested
Projects

Comments

@yaminet1024
Copy link

yaminet1024 commented Oct 22, 2020

class EditorBloc extends Bloc<BookEvent, BooksState> {

  @override
  BooksState get initialState => BooksLoading();

  EditorBloc();

  Stream<BooksState> _loadBooks() async* {
await future.delay(Duration(seconds:1s,(){return Future.value(0)}))
  }

  Stream<BooksState> _saveBooks(Gym gym, Sector sector, BookHistory booksHistory) async* {
    // Some code
  }

  Stream<BooksState> _addBooks(Gym gym, Sector sector, BookHistory booksHistory) async* {
    // Some code
  }

  @override
  Stream<BooksState> mapEventToState(BooksState currentState, BookEvent event) async* {

    if (event is LoadBooks) {
      yield* _loadBooks(event.gym, event.sector);
    } else if(event is SaveBooks) {
      yield BooksLoading();
      yield* _saveBooks(event.gym, event.sector, event.booksHistory);
    } else if (event is AddBook) {
      yield* _addBooks(gym, sector, booksHistory)
    }
  }
}

if I add the three event same time, it seems that is will be block the event and not async , can deal witch event not block the steam?

@felangel felangel self-assigned this Oct 23, 2020
@felangel felangel added pkg:bloc This issue is related to the bloc package question Further information is requested labels Oct 23, 2020
@felangel felangel added this to To do in bloc via automation Oct 23, 2020
@felangel
Copy link
Owner

felangel commented Oct 23, 2020

Hi @yaminet1024 👋
Thanks for opening an issue!

This is how bloc works by default (each event is processed in the exact order it was received). If you want to override this behavior you can override transformEvents and apply switchMap or flatMap depending on the desired functionality.

  @override
  Stream<Transition< BookEvent, BooksState >> transformEvents(
    Stream< BookEvent > events,
    TransitionFunction< BookEvent, BooksState > transitionFn,
  ) {
    return events.flatMap(transitionFn);
  }

Closing for now but feel free to comment with additional questions and I'm happy to continue the conversation 👍

bloc automation moved this from To do to Done Oct 23, 2020
@yaminet1024
Copy link
Author

Hi @yaminet1024 👋
Thanks for opening an issue!

This is how bloc works by default (each event is processed in the exact order it was received). If you want to override this behavior you can override transformEvents and apply switchMap or flatMap depending on the desired functionality.

  @override
  Stream<Transition< BookEvent, BooksState >> transformEvents(
    Stream< BookEvent > events,
    TransitionFunction< BookEvent, BooksState > transitionFn,
  ) {
    return events.flatMap(transitionFn);
  }

Closing for now but feel free to comment with additional questions and I'm happy to continue the conversation 👍

Thank you very much for your answer, but I find that Stream< BookEvent > events does not have switchMap or flatMap method in flutter

@felangel
Copy link
Owner

You need to import rxdart to use those operators 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg:bloc This issue is related to the bloc package question Further information is requested
Projects
bloc
  
Done
Development

No branches or pull requests

2 participants