Skip to content

Commit 68db6ba

Browse files
crisbetotinayuangao
authored andcommitted
fix(scroll-dispatcher): unable to unsubscribe from global listener (#3729)
Fixes not being able to unsubscribe from the global `scrolled` listener.
1 parent 6108563 commit 68db6ba

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
@@ -89,6 +89,19 @@ describe('Scroll Dispatcher', () => {
8989
expect(spy).not.toHaveBeenCalled();
9090
subscription.unsubscribe();
9191
});
92+
93+
it('should be able to unsubscribe from the global scrollable', () => {
94+
const spy = jasmine.createSpy('global scroll callback');
95+
const subscription = scroll.scrolled(0, spy);
96+
97+
dispatchFakeEvent(document, 'scroll');
98+
expect(spy).toHaveBeenCalledTimes(1);
99+
100+
subscription.unsubscribe();
101+
dispatchFakeEvent(document, 'scroll');
102+
103+
expect(spy).toHaveBeenCalledTimes(1);
104+
});
92105
});
93106

94107
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
@@ -81,14 +81,18 @@ export class ScrollDispatcher {
8181

8282
// Note that we need to do the subscribing from here, in order to be able to remove
8383
// the global event listeners once there are no more subscriptions.
84-
return observable.subscribe(callback).add(() => {
84+
let subscription = observable.subscribe(callback);
85+
86+
subscription.add(() => {
8587
this._scrolledCount--;
8688

8789
if (this._globalSubscription && !this.scrollableReferences.size && !this._scrolledCount) {
8890
this._globalSubscription.unsubscribe();
8991
this._globalSubscription = null;
9092
}
9193
});
94+
95+
return subscription;
9296
}
9397

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

0 commit comments

Comments
 (0)