hi,
I started playing with Dart couple of weeks ago and find it quite interesting, especially the
idea to represent event sequences as streams.
What I find awkward is that a StreamSubscription instance is required to cancel a subscription,
since weak references are not supported.
My understanding is that it might not be required to do it if running in Dart VM (at least eventually)
as it can control the GC, but that it is necessary if going through dart2js.
E.g. I need to observe a number of events and need also to detach from them at some point.
This means I need to collect all the StreamSubscription instances and cancel them later on.
If I need to cancel them at the same time, then I can just dump them into a list and then call
subscriptions.forEach((sub) => sub.cancel());
However, if I need to detach at different times, then I need to either have a separate StreamSubscription
instances (e.g. as fields of a class) or keep them in a map and look them up by name or something:
// either
sub1.cancel();
// later
sub2.cancel();
// or
subs['event1'].cancel();
// later
subs['event2'].cancel();I find this quite ugly as it pollutes the class state with something which should not really be part of the state.
I understand why
obj.onSomeEvent.unsubscribe(handler) is not possible (or at least I think I do),
but some solution which doesn't require caching the subscriptions would make it easier to use streams correctly.
Am I missing something? Are there any ideas to improve on this front?
I couldn't find this issue being discussed in other topics, except that it is mentioned at the end of
https://groups.google.com/a/dartlang.org/d/topic/misc/waZkFDO9YCQ/discussion