fix(scroll-dispatcher): unable to unsubscribe from global listener (#3729)

Fixes not being able to unsubscribe from the global `scrolled` listener.
This commit is contained in:
Kristiyan Kostadinov
2017-03-28 22:28:30 +02:00
committed by tinayuangao
parent 61085633ef
commit 68db6ba118
2 changed files with 18 additions and 1 deletions
@@ -89,6 +89,19 @@ describe('Scroll Dispatcher', () => {
expect(spy).not.toHaveBeenCalled();
subscription.unsubscribe();
});
it('should be able to unsubscribe from the global scrollable', () => {
const spy = jasmine.createSpy('global scroll callback');
const subscription = scroll.scrolled(0, spy);
dispatchFakeEvent(document, 'scroll');
expect(spy).toHaveBeenCalledTimes(1);
subscription.unsubscribe();
dispatchFakeEvent(document, 'scroll');
expect(spy).toHaveBeenCalledTimes(1);
});
});
describe('Nested scrollables', () => {
@@ -81,7 +81,9 @@ export class ScrollDispatcher {
// Note that we need to do the subscribing from here, in order to be able to remove
// the global event listeners once there are no more subscriptions.
return observable.subscribe(callback).add(() => {
let subscription = observable.subscribe(callback);
subscription.add(() => {
this._scrolledCount--;
if (this._globalSubscription && !this.scrollableReferences.size && !this._scrolledCount) {
@@ -89,6 +91,8 @@ export class ScrollDispatcher {
this._globalSubscription = null;
}
});
return subscription;
}
/** Returns all registered Scrollables that contain the provided element. */