Skip to content

Commit

Permalink
fix(ScrollRegister): use sampleTime for scroll event
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmnlv committed Apr 21, 2017
1 parent 1e405ce commit 2019b07
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 14 deletions.
6 changes: 0 additions & 6 deletions src/modules/infinite-scroll.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ export class InfiniteScroll implements OnDestroy, OnInit {
@Input('immediateCheck') _immediate: boolean = false;
@Input('horizontal') _horizontal: boolean = false;
@Input('alwaysCallback') _alwaysCallback: boolean = false;
@Input()
set debounce(value: string | boolean) {
this.throttleType = value === '' || !!value ? 'debounce' : 'throttle';
}

private throttleType: string = 'throttle';
private disposeScroller: Subscription;

constructor(
Expand All @@ -50,7 +45,6 @@ export class InfiniteScroll implements OnDestroy, OnInit {
});
const options: ScrollRegisterConfig = {
container: positionResolver.container,
throttleType: this.throttleType,
throttleDuration: this._throttle,
filterBefore: () => !this._disabled,
mergeMap: () => positionResolver.calculatePoints(this.element),
Expand Down
11 changes: 4 additions & 7 deletions src/services/scroll-register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,25 @@ import { Injectable, ElementRef } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Subscription } from 'rxjs/Subscription';
import 'rxjs/add/observable/fromEvent';
import 'rxjs/add/observable/timer';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/debounce';
import 'rxjs/add/operator/throttle';
import 'rxjs/add/operator/sampleTime';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/mergeMap';


export interface ScrollRegisterConfig {
container: ContainerRef;
throttleType: string;
throttleDuration: number;
filterBefore: Function;
filterBefore: () => boolean;
mergeMap: Function;
scrollHandler: Function;
scrollHandler: (value: any) => void;
}

@Injectable()
export class ScrollRegister {
attachEvent (options: ScrollRegisterConfig): Subscription {
const scroller$: Subscription = Observable.fromEvent(options.container, 'scroll')
[options.throttleType](() => Observable.timer(options.throttleDuration))
.sampleTime(options.throttleDuration)
.filter(options.filterBefore)
.mergeMap((ev: any) => Observable.of(options.mergeMap(ev)))
.subscribe(options.scrollHandler);
Expand Down
1 change: 0 additions & 1 deletion tests/services/scroll-register.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ describe('Scroll Regsiter', () => {
mergeMap: (e: any) => e,
scrollHandler: (ev: any) => ev,
throttleDuration: 300,
throttleType: 'throttle'

};
const scroller$: Subscription = scrollRegister.attachEvent(scrollConfig);
Expand Down

0 comments on commit 2019b07

Please sign in to comment.