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

Regression when switchMap is used together with asynchronous generator #764

Open
ky1vstar opened this issue Aug 26, 2024 · 2 comments
Open
Assignees

Comments

@ky1vstar
Copy link

ky1vstar commented Aug 26, 2024

Here is minimal reproducible example for regression which I found during migration from rxdart 0.27.7 to 0.28.0.

Dart SDK version: 3.5.1

Source code

import 'package:rxdart/rxdart.dart';

void main(List<String> arguments) async {
  getStream().listen(print);
}

Stream<String> getStream() {
  final stream1 = Future.delayed(Duration(seconds: 1), () => "1").asStream();
  final stream2 = stream1.startWith("2");

  final stream3 = stream2.switchMap((value) async* {
    await for (final innerValue in BehaviorSubject.seeded(value)) {
      yield innerValue;
    }
  });

  return stream3;
}

Expected result (rxdart 0.27.7):

2
1

Actual result (rxdart 0.28.0):

2

@ReactiveX ReactiveX deleted a comment Aug 26, 2024
@ReactiveX ReactiveX deleted a comment Aug 26, 2024
@ReactiveX ReactiveX deleted a comment Aug 26, 2024
@hoc081098
Copy link
Collaborator

hoc081098 commented Aug 28, 2024

possibly related to dart-lang/sdk#42717 (comment), dart-lang/sdk#56619

@drstranges
Copy link

The issue is not only with the asynchronous generator, but also with someFuture.asStream too.
Here is the test that will fail on 0.28.0 but succeed on 0.27.7

  testWidgets('switchMap regression test', (tester) async {

    const delay = Duration(seconds: 1);

    Future<String> someFuture(String value) async {
      await Future<void>.delayed(delay);
      return value;
    }

    final subject = PublishSubject<String>();
    final beforeSwitchMap = <String>[];
    final afterSwitchMap = <String>[];

    subject
        .doOnData(beforeSwitchMap.add)
        .switchMap(
          (value) => someFuture(value).asStream(),
        )
        .doOnData(afterSwitchMap.add)
        .listen((event) {
      debugPrint('event: $event');
    });

    await tester.pump(const Duration(seconds: 2));
    subject.add('1');
    await tester.pump(const Duration(seconds: 2));
    subject.add('2');
    // Delay before next value < [delay] will fail the test in 0.28.0
    await tester.pump(delay - const Duration(microseconds: 1));
    subject.add('3');
    await tester.pump(const Duration(seconds: 2));
    subject.add('4');
    await tester.pump(const Duration(seconds: 2));

    expect(beforeSwitchMap, ['1', '2', '3', '4']);

    // Next line will succeed in 0.27.7 but fail in 0.28.0
    // with the message:
    // Expected: ['1', '3', '4']
    // Actual: ['1']
    expect(afterSwitchMap, ['1', '3', '4']);
  });

I can't reproduce the same behavior in runtime. Only widget tests seem to be affected (26 of our tests are red now ).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@drstranges @ky1vstar @hoc081098 and others