Skip to content
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

Fix-unhandled-exception #27

Merged
merged 1 commit into from
Jan 16, 2025
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: 18 additions & 11 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ class InViewport extends Component<InViewportProps, InViewportState> {
constructor(props: InViewportProps) {
super(props);
this.state = { rectTop: 0, rectBottom: 0, rectHeight: 0, rectWidth: 0 };
// Debounce handleVisibilityChange for performance
this.handleVisibilityChange = debounce(this.handleVisibilityChange, 200);
}

/**
Expand All @@ -74,10 +72,14 @@ class InViewport extends Component<InViewportProps, InViewportState> {
* Calls the onChange callback with the updated visibility state.
* Uses a debounced version of the function for efficiency.
* @param {boolean} isVisible - Whether the component is visible
*
* Debounce handleVisibilityChange for performance
*/
handleVisibilityChange(isVisible: boolean) {
this.props.onChange(isVisible);
}
handleVisibilityChange = debounce((isVisible: boolean) => {
if (this.props.onChange) {
this.props.onChange(isVisible);
}
}, 200);
Copy link
Owner Author

Choose a reason for hiding this comment

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

LGTM


/**
* componentDidMount
Expand Down Expand Up @@ -134,7 +136,7 @@ class InViewport extends Component<InViewportProps, InViewportState> {
* Starts the interval for monitoring visibility and sets up
* measurements for the component's position and size.
*/
startWatching() {
startWatching = () => {
this.interval = setInterval(() => {
if (!this.myview.current) {
return;
Expand All @@ -151,18 +153,18 @@ class InViewport extends Component<InViewportProps, InViewportState> {

this.isInViewPort();
}, this.props.delay || 1000);
}
};

/**
* stopWatching
*
* Clears the interval that monitors visibility, stopping the checks.
*/
stopWatching() {
stopWatching = () => {
if (this.interval) {
clearInterval(this.interval);
}
}
};

/**
* isInViewPort
Expand All @@ -171,7 +173,12 @@ class InViewport extends Component<InViewportProps, InViewportState> {
* its top and bottom positions, the viewport height, and the specified
* threshold. Calls handleVisibilityChange if the visibility state changes.
*/
isInViewPort() {
isInViewPort = () => {
// Add defensive programming to ensure props and state are defined before accessing them
if (!this.props || !this.state) {
console.warn('Props or state is undefined');
return;
}
let visiblePercentage = 100; // Default to 100% visibility

if (this.props?.threshold) {
Expand Down Expand Up @@ -205,7 +212,7 @@ class InViewport extends Component<InViewportProps, InViewportState> {
// this.props.onChange(isVisible);
this.handleVisibilityChange(isVisible); // Debounced visibility change
}
}
};

/**
* render
Expand Down
Loading