Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

fix(scope): should allow removing listener during an event #695

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions lib/core/scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,9 @@ class ScopeStream extends async.Stream<ScopeEvent> {
final _Streams _streams;
final String _name;
final subscriptions = <ScopeStreamSubscription>[];
bool _firing = false;
List<ScopeStreamSubscription> _toRemove;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pavelgj out of curiosity, why have you picked a lazy init here ? (the code could be simpler at the expense of some bytes)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to avoid unnecessary allocations to handle a corner case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be a good use case for introducing a getter ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way I imagine a getter here it would also result in an unnecessary allocation when _remove is never called. How would you do it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right. I must be tired, I've just lost half a day fixing my ubuntu setup...



ScopeStream(this._streams, this._exceptionHandler, this._name);

Expand All @@ -750,16 +753,36 @@ class ScopeStream extends async.Stream<ScopeEvent> {
}

void _fire(ScopeEvent event) {
for (ScopeStreamSubscription subscription in subscriptions) {
try {
subscription._onData(event);
} catch (e, s) {
_exceptionHandler(e, s);
_firing = true;
try {
for (ScopeStreamSubscription subscription in subscriptions) {
try {
subscription._onData(event);
} catch (e, s) {
_exceptionHandler(e, s);
}
}
} finally {
_firing = false;
if (_toRemove != null) {
_toRemove.forEach(_actuallyRemove);
_toRemove = null;
}
}
}

void _remove(ScopeStreamSubscription subscription) {
if (_firing) {
if (_toRemove == null) {
_toRemove = [];
}
_toRemove.add(subscription);
} else {
_actuallyRemove(subscription);
}
}

void _actuallyRemove(ScopeStreamSubscription subscription) {
assert(subscription._scopeStream == this);
if (subscriptions.remove(subscription)) {
if (subscriptions.isEmpty) _streams._addCount(_name, -1);
Expand Down
10 changes: 10 additions & 0 deletions test/core/scope_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,16 @@ void main() {
expect(args.length).toBe(4);
expect(args).toEqual(['do', 're', 'me', 'fa']);
}));

it('should allow removing listener during an event', inject((RootScope rootScope) {
StreamSubscription subscription;
subscription = rootScope.on('foo').listen((_) {
subscription.cancel();
});
expect(() {
rootScope.broadcast('foo');
}).not.toThrow();
}));
});
});
});
Expand Down