danschultz / frappe

A Dart package for functional reactive programming

Home Page:http://pub.dartlang.org/packages/frappe

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error when performing flatMapLatest on property within a property

Andersmholmgren opened this issue · comments

I suspect this is related to #23 but filing separately as it may be different

Stack trace:

Unhandled exception:
Uncaught Error: Bad state: Cannot add event after closing
Stack Trace:
#0      _StreamController.add (dart:async/stream_controller.dart:411)
#1      _FlatMapLatestReactable.onData.<anonymous closure> (package:frappe/src/flat_map_latest_reactable.dart:15:69)
#2      _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
#3      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341)
#4      _DelayedData.perform (dart:async/stream_impl.dart:595)
#5      _StreamImplEvents.handleNext (dart:async/stream_impl.dart:711)
#6      _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:671)
#7      _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:41)
#8      _asyncRunCallback (dart:async/schedule_microtask.dart:48)
#9      _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:95)
#10     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:142)

#0      _rootHandleUncaughtError.<anonymous closure> (dart:async/zone.dart:886)
#1      _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:41)
#2      _asyncRunCallback (dart:async/schedule_microtask.dart:48)
#3      _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:95)
#4      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:142)

code

class A {
  final Property<B> b;

  A(this.b);
}

class B {
  final Property<String> s;

  B(this.s);
}

test function

runnopt(Property p1, B b, A a) async {
  a.b.map((B b) => b.s)
    .flatMapLatest((Property<String> p) => p.asStream())
    .listen(print);

  await new Future.delayed(const Duration(milliseconds:1000));
}

running with the following works

main() async {
  final p1 = new Property.constant('first');

  final b = new B(p1);

  final pb = new Property.constant(b);

  final a = new A(pb);

  await runnopt(p1, b, a);
}

whereas with the following it throws the above error

main() async {
  final sc = new StreamController.broadcast();
  final p1 = new Property.fromStreamWithInitialValue('first', sc.stream);

  final b = new B(p1);

  final pb = new Property.constant(b);

  final a = new A(pb);

  await runnopt(p1, b, a);
}

The work done in #28 and #23 fixed this.