Skip to content

Commit 24e703a

Browse files
committed
fix(scroll-dispatcher): unable to unsubscribe from global listener
Fixes not being able to unsubscribe from the global `scrolled` listener.
1 parent c524438 commit 24e703a

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/lib/core/overlay/scroll/scroll-dispatcher.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ describe('Scroll Dispatcher', () => {
6767
tick(throttleTime);
6868
expect(hasServiceScrollNotified).toBe(true);
6969
}));
70+
71+
it('should be able to unsubscribe from the global scrollable', () => {
72+
const spy = jasmine.createSpy('global scroll callback');
73+
const subscription = scroll.scrolled(0, spy);
74+
75+
dispatchFakeEvent(document, 'scroll');
76+
expect(spy).toHaveBeenCalledTimes(1);
77+
78+
subscription.unsubscribe();
79+
dispatchFakeEvent(document, 'scroll');
80+
81+
expect(spy).toHaveBeenCalledTimes(1);
82+
});
7083
});
7184

7285
describe('Nested scrollables', () => {

src/lib/core/overlay/scroll/scroll-dispatcher.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,18 @@ export class ScrollDispatcher {
7777

7878
// Note that we need to do the subscribing from here, in order to be able to remove
7979
// the global event listeners once there are no more subscriptions.
80-
return observable.subscribe(callback).add(() => {
80+
let subscription = observable.subscribe(callback);
81+
82+
subscription.add(() => {
8183
this._scrolledCount--;
8284

8385
if (this._globalSubscription && !this.scrollableReferences.size && !this._scrolledCount) {
8486
this._globalSubscription.unsubscribe();
8587
this._globalSubscription = null;
8688
}
8789
});
90+
91+
return subscription;
8892
}
8993

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

0 commit comments

Comments
 (0)