Skip to content

Commit 76d063b

Browse files
author
Rajat Sancheti
committed
convert utils to ts
1 parent 62e1f52 commit 76d063b

File tree

5 files changed

+19
-30
lines changed

5 files changed

+19
-30
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
},
5353
"dependencies": {
5454
"@types/prop-types": "^15.7.2",
55-
"@types/react": "^16.9.2"
55+
"@types/react": "^16.9.2",
56+
"@types/throttle-debounce": "^2.1.0",
57+
"throttle-debounce": "^2.1.0"
5658
}
5759
}

src/index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Component, ReactNode, CSSProperties } from "react";
2-
import throttle from "./utils/throttle";
2+
import { throttle } from 'throttle-debounce';
33
import { ThresholdUnits, parseThreshold } from "./utils/threshold";
44

55
type Fn = () => any;
@@ -41,7 +41,7 @@ export default class InfiniteScroll extends Component<Props, State> {
4141
};
4242

4343
this.onScrollListener = this.onScrollListener.bind(this);
44-
this.throttledOnScrollListener = throttle(this.onScrollListener, 150).bind(
44+
this.throttledOnScrollListener = throttle(150, this.onScrollListener).bind(
4545
this
4646
);
4747
this.onStart = this.onStart.bind(this);
@@ -50,7 +50,7 @@ export default class InfiniteScroll extends Component<Props, State> {
5050
this.getScrollableTarget = this.getScrollableTarget.bind(this);
5151
}
5252

53-
private throttledOnScrollListener: () => void;
53+
private throttledOnScrollListener: (e: MouseEvent) => void;
5454
private _scrollableNode: HTMLElement | undefined | null;
5555
private el: HTMLElement | undefined | Window & typeof globalThis;
5656
private _infScroll: HTMLDivElement | undefined;
@@ -74,7 +74,7 @@ export default class InfiniteScroll extends Component<Props, State> {
7474
: this._scrollableNode || window;
7575

7676
if (this.el) {
77-
this.el.addEventListener("scroll", this.throttledOnScrollListener);
77+
this.el.addEventListener("scroll", (e) => this.throttledOnScrollListener(e as MouseEvent));
7878
}
7979

8080
if (
@@ -116,7 +116,7 @@ export default class InfiniteScroll extends Component<Props, State> {
116116

117117
componentWillUnmount() {
118118
if (this.el) {
119-
this.el.removeEventListener("scroll", this.throttledOnScrollListener);
119+
this.el.removeEventListener("scroll", (e) => this.throttledOnScrollListener(e as MouseEvent));
120120

121121
if (this.props.pullDownToRefresh) {
122122
this.el.removeEventListener("touchstart", this.onStart);

src/utils/threshold.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const defaultThreshold = {
88
value: 0.8,
99
};
1010

11-
export function parseThreshold(scrollThreshold) {
11+
export function parseThreshold(scrollThreshold: string | number) {
1212
if (typeof scrollThreshold === "number") {
1313
return {
1414
unit: ThresholdUnits.Percent,

src/utils/throttle.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
"@types/prop-types" "*"
1616
csstype "^2.2.0"
1717

18+
"@types/throttle-debounce@^2.1.0":
19+
version "2.1.0"
20+
resolved "https://registry.yarnpkg.com/@types/throttle-debounce/-/throttle-debounce-2.1.0.tgz#1c3df624bfc4b62f992d3012b84c56d41eab3776"
21+
integrity sha512-5eQEtSCoESnh2FsiLTxE121IiE60hnMqcb435fShf4bpLRjEu1Eoekht23y6zXS9Ts3l+Szu3TARnTsA0GkOkQ==
22+
1823
abbrev@1:
1924
version "1.1.1"
2025
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8"
@@ -3329,6 +3334,11 @@ tar@^4:
33293334
safe-buffer "^5.1.2"
33303335
yallist "^3.0.3"
33313336

3337+
throttle-debounce@^2.1.0:
3338+
version "2.1.0"
3339+
resolved "https://registry.yarnpkg.com/throttle-debounce/-/throttle-debounce-2.1.0.tgz#257e648f0a56bd9e54fe0f132c4ab8611df4e1d5"
3340+
integrity sha512-AOvyNahXQuU7NN+VVvOOX+uW6FPaWdAOdRP5HfwYxAfCzXTFKRMoIMk+n+po318+ktcChx+F1Dd91G3YHeMKyg==
3341+
33323342
through@~2.3.6, through@~2.3.8:
33333343
version "2.3.8"
33343344
resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5"

0 commit comments

Comments
 (0)