Skip to content

Unexpected Behavior with Nested Async Generators #44616

Description

@felangel
  • This tracker is for issues related to: Dart core libraries ("dart:async")
  • Dart SDK Version (dart --version): Dart SDK version: 2.12.0-133.2.beta (beta) (Tue Dec 15 09:55:09 2020 +0100) on "macos_x64"
  • Whether you are using Windows, MacOSX, or Linux (if applicable): MacOSX

Reproduction Sample

import 'dart:async';

void main() async {
  final bloc = Counter()..listen(print);
  bloc.add(CounterEvent.increment);
}

enum CounterEvent { increment }

class Counter extends Bloc<CounterEvent, int> {
  Counter() : super(0);

  @override
  Stream<int> mapEventToState(CounterEvent event) async* {
    // Does not work
    yield* _increment();

    // Works
    // yield state + 1;
    // yield state + 1;
  }

  Stream<int> _increment() async* {
    yield state + 1;
    yield state + 1;
  }
}

abstract class Bloc<E, S> {
  Bloc(this._state) {
    _eventController.stream.asyncExpand(mapEventToState).listen((s) {
      _state = s;
      _stateController.add(_state);
    });
  }

  final _eventController = StreamController<E>();
  final _stateController = StreamController<S>.broadcast();

  S _state;
  S get state => _state;

  StreamSubscription<S> listen(void Function(S) onData) {
    return _stateController.stream.listen(onData);
  }

  Stream<S> mapEventToState(E event);

  void add(E event) => _eventController.add(event);
}

Reproduction Steps

  1. Run above sample observe output is:
1
1
  1. Comment line 16 and uncomment lines 19-20
@override
  Stream<int> mapEventToState(CounterEvent event) async* {
    // Does not work
    // yield* _increment();

    // Works
    yield state + 1;
    yield state + 1;
  }
  1. Re-run and observe output is:
1
2

Expectations

I would expect that yielding inline would exhibit the same behavior as using yield* with a nested async generator.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.library-async

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions