Coder Social home page Coder Social logo

Delay when dispatching event about bloc HOT 17 CLOSED

felangel avatar felangel commented on May 22, 2024
Delay when dispatching event

from bloc.

Comments (17)

danielfpedro avatar danielfpedro commented on May 22, 2024 5

@danielfpedro check out #524 (comment)

Appreciate!

from bloc.

felangel avatar felangel commented on May 22, 2024 4

@danielfpedro check out #524 (comment)

from bloc.

shelarsuhas avatar shelarsuhas commented on May 22, 2024 3

The Observable is replaced by Stream in the latest RXDart version.

@override Stream<GithubSearchState> transformEvents( Stream<GithubSearchEvent> events, Stream<GithubSearchState> Function(GithubSearchEvent event) next, ) { return (events as Observable<GithubSearchEvent>) .debounceTime( Duration(milliseconds: 300), ) .switchMap(next); }

Doesnt work as it doesnt override transformEvents().

Any input about how it can be done now?

Never mind, understood it now.

If any one comes looking for it, here's how its done now:

@override
  Stream<Transition< GithubSearchEvent, GithubSearchState >> transformEvents(
      Stream< GithubSearchEvent > events, transitionFn) {
    return events
        .debounceTime(const Duration(milliseconds: 500))
        .switchMap((transitionFn));
  }

from bloc.

felangel avatar felangel commented on May 22, 2024 2

No problem :) Glad I could help!

from bloc.

Greatcallie avatar Greatcallie commented on May 22, 2024 2

Hi @Greatcallie πŸ‘‹

Have a look at an example here #1107 (comment)

Thanks. debounceTime wasn't recognised until i added rxdart

from bloc.

felangel avatar felangel commented on May 22, 2024 1

@m7mdra thanks for the video! Have you tried debouncing because it seems like the problem is your app is spamming the api on every text change.

// Add this to top with imports
import 'package:rxdart/rxdart.dart';


// Add this to your Bloc
@override
Stream<SearchEvent> transform(Stream<SearchEvent> events) {
  return (events as Observable<SearchEvent>)
      .debounce(Duration(milliseconds: 500));
}

Let me know if that helps.

from bloc.

m7mdra avatar m7mdra commented on May 22, 2024 1

@felangel yes its working now. thanks for your time.

from bloc.

felangel avatar felangel commented on May 22, 2024 1

Hi @antonygunawan94 πŸ‘‹
Check out

for an example.

from bloc.

narcodico avatar narcodico commented on May 22, 2024 1

Hi @Greatcallie πŸ‘‹

Have a look at an example here #1107 (comment)

from bloc.

fvisticot avatar fvisticot commented on May 22, 2024 1

Is it still possible to use the debounce with flutter_bloc (7.0.1) ?
is there any solution without the import 'package:rxdart/rxdart.dart' ?

from bloc.

felangel avatar felangel commented on May 22, 2024

Thanks for opening an issue. Can you please be a bit more specific about what you expected as opposed to what you observed?

Transitions occur after a state change but before the bloc’s state is updated. The reason there is a delay is because your code is dependent on a network request so the transition won’t occur until after the github api responds with data for the success state.

Does that help?

from bloc.

m7mdra avatar m7mdra commented on May 22, 2024

@felangel
sorry if i made myself unclear since english is not native tongue.
perhaps this video might explain what im getting at: link

from bloc.

antonygunawan94 avatar antonygunawan94 commented on May 22, 2024

hello,
can you provide updated example on how to debounce event?
because I can't seem to find this override function in the latest flutter bloc

@override Stream<SearchEvent> transform(Stream<SearchEvent> events) { return (events as Observable<SearchEvent>) .debounce(Duration(milliseconds: 500)); }

thank you

from bloc.

danielfpedro avatar danielfpedro commented on May 22, 2024

@m7mdra thanks for the video! Have you tried debouncing because it seems like the problem is your app is spamming the api on every text change.

// Add this to top with imports
import 'package:rxdart/rxdart.dart';


// Add this to your Bloc
@override
Stream<SearchEvent> transform(Stream<SearchEvent> events) {
  return (events as Observable<SearchEvent>)
      .debounce(Duration(milliseconds: 500));
}

Let me know if that helps.

What if I have several events on the bloc, all events will be debounced? How to debounce just the event that fetch data from server?

from bloc.

Greatcallie avatar Greatcallie commented on May 22, 2024

@m7mdra thanks for the video! Have you tried debouncing because it seems like the problem is your app is spamming the api on every text change.

// Add this to top with imports
import 'package:rxdart/rxdart.dart';


// Add this to your Bloc
@override
Stream<SearchEvent> transform(Stream<SearchEvent> events) {
  return (events as Observable<SearchEvent>)
      .debounce(Duration(milliseconds: 500));
}

Let me know if that helps.

this doesn't work anymore. Please how do I do it now with the latest library?

from bloc.

shelarsuhas avatar shelarsuhas commented on May 22, 2024

The Observable is replaced by Stream in the latest RXDart version.

@override
  Stream<GithubSearchState> transformEvents(
    Stream<GithubSearchEvent> events,
    Stream<GithubSearchState> Function(GithubSearchEvent event) next,
  ) {
    return (events as Observable<GithubSearchEvent>)
        .debounceTime(
          Duration(milliseconds: 300),
        )
        .switchMap(next);
  }

Doesnt work as it doesnt override transformEvents().

Any input about how it can be done now?

from bloc.

felangel avatar felangel commented on May 22, 2024

Is it still possible to use the debounce with flutter_bloc (7.0.1) ?
is there any solution without the import 'package:rxdart/rxdart.dart' ?

Yup you can still use debounce in 7.0.1. The easiest way is to use rxdart πŸ‘

from bloc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.