Skip to content

cleanup(cdk-scrollable): use fromEvent under the hood #12589

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

Merged
merged 1 commit into from
Aug 13, 2018
Merged
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
29 changes: 13 additions & 16 deletions src/cdk/scrolling/scrollable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Directive, ElementRef, OnInit, OnDestroy, NgZone} from '@angular/core';
import {Observable, Subject} from 'rxjs';
import {Directive, ElementRef, NgZone, OnDestroy, OnInit} from '@angular/core';
import {fromEvent, Observable, Subject} from 'rxjs';
import {takeUntil} from 'rxjs/operators';
import {ScrollDispatcher} from './scroll-dispatcher';


Expand All @@ -20,36 +21,32 @@ import {ScrollDispatcher} from './scroll-dispatcher';
selector: '[cdk-scrollable], [cdkScrollable]'
})
export class CdkScrollable implements OnInit, OnDestroy {
private _elementScrolled: Subject<Event> = new Subject();
private _scrollListener = (event: Event) => this._elementScrolled.next(event);
private _destroyed = new Subject();

private _elementScrolled: Observable<Event> = Observable.create(observer =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need the Observable.create here? You can just do this:

private _elementScrolled = this._ngZone.runOutsideAngular(() => {
  return fromEvent(...).pipe(takeUntil(this._destroyed), tap(() => ...));
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the tap(() => ...) do here?

I was under the impression that the call to subscribe has to occur inside the runOutsideAngular function, that's why I did it this way

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I forgot that about having to subscribe.

this._ngZone.runOutsideAngular(() =>
fromEvent(this._elementRef.nativeElement, 'scroll').pipe(takeUntil(this._destroyed))
.subscribe(observer)));

constructor(private _elementRef: ElementRef,
private _scroll: ScrollDispatcher,
private _ngZone: NgZone) {}

ngOnInit() {
this._ngZone.runOutsideAngular(() => {
this.getElementRef().nativeElement.addEventListener('scroll', this._scrollListener);
});

this._scroll.register(this);
}

ngOnDestroy() {
this._scroll.deregister(this);

if (this._scrollListener) {
this.getElementRef().nativeElement.removeEventListener('scroll', this._scrollListener);
}

this._elementScrolled.complete();
this._destroyed.next();
this._destroyed.complete();
}

/**
* Returns observable that emits when a scroll event is fired on the host element.
*/
elementScrolled(): Observable<any> {
return this._elementScrolled.asObservable();
elementScrolled(): Observable<Event> {
return this._elementScrolled;
}

getElementRef(): ElementRef {
Expand Down