fluttercommunity/rx_command

Create new stream without recreating manager.

TheHemantKaushik opened this issue · 8 comments

Can we create a command createFromStream where the request param is updatable?

Current flow:

class SiteManager {
  RxCommand<Site, Site> selectedSite;
  RxCommand<String, String> selectedSiteId;

  final SiteService _siteService;

  StreamSubscription _siteSubs;

  SiteManager(this._siteService) {
    selectedSite = RxCommand.createSync(
      (v) => v,
      emitsLastValueToNewSubscriptions: true,
    );
    selectedSiteId = RxCommand.createSync(
      (v) => v,
      emitsLastValueToNewSubscriptions: true,
    );

    selectedSiteId.listen((siteId) {
      _siteSubs?.cancel();
      _siteSubs = _siteService.getByIdStream(siteId).listen((value) {
        selectedSite.execute(value);
      });
    });
  }
}

Expected flow:

class SiteManager2 {
  RxCommand<String, Site> selectedSite;

  final SiteService _siteService;

  SiteManager2(this._siteService) {
    selectedSite = RxCommand.createFromStream(
      _siteService.getByIdStream,
      emitsLastValueToNewSubscriptions: true,
    );
  }
}

I am registering SiteManager in get_it service locator as singleton and accessing it different places in my app. So I don't want to recreate it.

Please let me know if there is some possible solution for this.

Thanks in advance!

@paulcbetts Do you understand what he wants to achieve?

Hi @escamoteur @paulcbetts

I mean to say that if I execute selectedSite.execute("any-id") command second time then it should call _siteService.getByIdStream(id) again.

Sorry, did miss your reply I was pretty busy the last time.
Shouldn't createFromStream do exactly what you want?
Did you solve the problem?

createFromStream is not working as expected. In my case, manager classes are singleton, and it is initialized only once. I'm using multiple RxCommand's currently. Holding value in one command, listening it to create stream, and updating results in another command.

What exactly is the problem with create from stream?

I'll create a sample and share it with you shortly.

Still waiting :-)

Do you still have a problem with this?