Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Mar 28, 2017
Merged
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
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