Properties aren't delivering the current value to multiple listeners
Andersmholmgren opened this issue · comments
The following prints
p1: first
p2: true
p1b: first
but should print
p1: first
p2: true
p1b: first
p2b: true
main() async {
listen(Reactable r, String l) =>
r.listen((v) => print('$l: $v'));
final p1 = new Property.constant('first');
final p2 = p1.map((s) => s == 'first').asProperty();
listen(p1, 'p1');
listen(p2, 'p2');
await new Future.delayed(const Duration(milliseconds:100));
listen(p1, 'p1b');
listen(p2, 'p2b');
await new Future.delayed(const Duration(milliseconds:100));
}
It seems that as map turns the property into a stream, the call to asProperty comes too late to save the value???
Incidentally it would be really nice if Property implemented map and returned a Property rather than needing to call asProperty(). Or maybe what is needed is another method with these semantics.
I find it is super common to derive properties from properties. In fact you could increasingly describe my whole app that way ;-)
Looks like the current value is not actually being delivered to multiple listeners. This only happens when you add a delay, because the listener is added in another event loop.
The test case can be simplified with this:
var p = new Property.constant(1).map((s) => s + 1);
p.listen(print);
new Future.delayed(new Duration(milliseconds: 100), () {
p.listen(print);
});
I'll rename the ticket.