Skip to content

Commit

Permalink
fix(scroll-dispatcher): unable to unsubscribe from global listener
Browse files Browse the repository at this point in the history
Fixes not being able to unsubscribe from the global `scrolled` listener.
  • Loading branch information
crisbeto committed Mar 27, 2017
1 parent e964734 commit 68cdb4e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/lib/core/overlay/scroll/scroll-dispatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
6 changes: 5 additions & 1 deletion src/lib/core/overlay/scroll/scroll-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,18 @@ 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) {
this._globalSubscription.unsubscribe();
this._globalSubscription = null;
}
});

return subscription;
}

/** Returns all registered Scrollables that contain the provided element. */
Expand Down

0 comments on commit 68cdb4e

Please sign in to comment.