Skip to content

await for ... and ... syntax #1440

Open
@lukepighetti

Description

@lukepighetti

I'm not aware of any way to subscribe to two streams using await for in an async* block. There are stream combining tools made available in rx_dart but I believe there is a lot of benefit to making this available with the core async tools.

As it stands today, I am not aware of any way to combine these two streams in an async* block.

Stream<BBQStatus> deviceStatus() async* {
  var status = BBQStatus.initial();

  /// Temperature probe events
  await for (var e in service.probeEvents()) {
    status = status.copyWith(probes: e);
    yield status;
  }

  print('This is not reachable until probeEvents stream closes');

  /// Battery voltage events
  await for (var e in service.batteryEvents()) {
    status = status.copyWith(battery: e);
    yield status;
  }

  print('This is not reachable until both streams close');
}

You can combine streams using solutions like rx_dart's CombineLatestStream.combine2, but having syntax to allow us to handle this would be really nice.

Here is a concept that is similar to syntax proposed by #1441

Stream<BBQStatus> deviceStatus() async* {
  var status = BBQStatus.initial();

  /// Temperature probe events and battery voltage events
  await for (var e in service.probeEvents()) {
    status = status.copyWith(probes: e);
    yield status;
  } and (var e in service.batteryEvents()) {
    status = status.copyWith(battery: e);
    yield status;
  }

  print('This is reachable when probeEvents and batteryEvents subscriptions complete');
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions