Skip to content

Commit

Permalink
fix(throttle): fix issue of long time to fire event
Browse files Browse the repository at this point in the history
  • Loading branch information
orizens committed Feb 21, 2018
1 parent 678ea1d commit 6e961e7
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ $ cat .gitignore
*.tgz

npm-debug.*
examples/
examples/
package-lock.json
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v 0.8.3 (2018/02/21)
* [FIX] - fix for #198 - takes a long time for onscroll to fire
* [UPDATE] - default value for throttle changed to 150

## v 0.8.2 (2018/01/07)
* [FIX] - fix for #221 - action not fired on Windows scrolling
* [FIX] - fix for #228 - scrolling back up to the top of the list and then scroll down doesn't trigger event
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Currently supported attributes:
* **infiniteScrollDistance**<_number_> - (optional, default: **2**) - the bottom percentage point of the scroll nob relatively to the infinite-scroll container (i.e, 2 (2 * 10 = 20%) is event is triggered when 80% (100% - 20%) has been scrolled).
if container.height is 900px, when the container is scrolled to or past the 720px, it will fire the scrolled event.
* **infiniteScrollUpDistance**<_number_> - (optional, default: **1.5**) - should get a number
* **infiniteScrollThrottle**<_number_> - (optional, default: **300**) - should get a number of **milliseconds** for throttle. The event will be triggered this many milliseconds after the user *stops* scrolling.
* **infiniteScrollThrottle**<_number_> - (optional, default: **150**) - should get a number of **milliseconds** for throttle. The event will be triggered this many milliseconds after the user *stops* scrolling.
* **infiniteScrollContainer**<_string|HTMLElement_> - (optional, default: null) - should get a html element or css selector for a scrollable element; window or current element will be used if this attribute is empty.
* **scrolled**<_function_> - this will callback if the distance threshold has been reached on a scroll down.
* **scrolledUp**<_function_> - (event: InfiniteScrollEvent) - this will callback if the distance threshold has been reached on a scroll up.
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-infinite-scroll",
"version": "0.8.2",
"version": "0.8.3",
"description": "An infinite scroll directive for Angular compatible with AoT compilation and Tree shaking",
"main": "./bundles/ngx-infinite-scroll.umd.js",
"module": "./modules/ngx-infinite-scroll.es5.js",
Expand Down Expand Up @@ -61,7 +61,7 @@
"karma-webpack": "2.0.3",
"reflect-metadata": "0.1.10",
"rollup": "0.41.6",
"rxjs": "5.2.0",
"rxjs": "5.5.2",
"ts-helpers": "1.1.2",
"ts-loader": "2.0.3",
"tslint": "4.5.1",
Expand Down
2 changes: 1 addition & 1 deletion src/modules/infinite-scroll.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class InfiniteScrollDirective

@Input() infiniteScrollDistance: number = 2;
@Input() infiniteScrollUpDistance: number = 1.5;
@Input() infiniteScrollThrottle: number = 300;
@Input() infiniteScrollThrottle: number = 150;
@Input() infiniteScrollDisabled: boolean = false;
@Input() infiniteScrollContainer: any = null;
@Input() scrollWindow: boolean = true;
Expand Down
39 changes: 34 additions & 5 deletions tests/services/scroll-register.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,51 @@ describe('Scroll Regsiter', () => {
return { element: mockedElement, container: mockedContainer };
};

// beforeEach(() => {

// });

it('should create a Observable of scroll observable', () => {
const mockDom = createMockDom();
const scrollConfig: Models.IScrollRegisterConfig = {
container: mockDom.container.nativeElement,
throttle: 300,

};
const scroller$: Observable<{}> = ScrollRegister.attachScrollEvent(scrollConfig);
const actual = scroller$;
expect(actual).toBeDefined();
});

[
{
it: 'should create a Observable of scroll observable with default 300 throttle',
config: {
throttle: 300,
},
expected: 300
},
{
it: 'should create a Observable of scroll observable without a sampleTime if throttle is 0',
config: {
throttle: 0
},
expected: 0
}
].forEach((spec) => {
it(spec.it, (done) => {
const mockDom = createMockDom();
const scrollConfig: Models.IScrollRegisterConfig = {
container: mockDom.container.nativeElement,
...spec.config
};
const scroller$: Observable<{}> = ScrollRegister.attachScrollEvent(scrollConfig);
const start = new Date();
scroller$.subscribe((result) => {
const end = new Date();
const actual = end.getTime() - start.getTime();
expect(actual).toBeGreaterThanOrEqual(spec.expected);
done();
});
mockDom.container.nativeElement.dispatchEvent(new Event('scroll'));
});
});

it('should create a scroll params object', () => {
const lastScrollPosition = 0;
const positionStats = {} as Models.IPositionStats;
Expand Down

0 comments on commit 6e961e7

Please sign in to comment.