-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
145 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import {useCallback, useEffect, useRef, useState} from 'react'; | ||
import {DeviceEventEmitter} from 'react-native'; | ||
import CONST from '@src/CONST'; | ||
|
||
/** | ||
* This hook tracks scroll events and emits a "scrolling" event when scrolling starts and ends. | ||
*/ | ||
const useScrollEventEmitter = <T extends Object>(additinalEmitData: T) => { | ||
const [lastScrollEvent, setLastScrollEvent] = useState<number | null>(null); | ||
const isScrollingRef = useRef<boolean>(false); | ||
|
||
const triggerScrollEvent = useCallback(() => { | ||
setLastScrollEvent(Date.now()); | ||
}, []); | ||
|
||
useEffect(() => { | ||
const emitScrolling = (isScrolling: boolean) => { | ||
DeviceEventEmitter.emit(CONST.EVENTS.SCROLLING, { | ||
isScrolling, | ||
...additinalEmitData, | ||
}); | ||
}; | ||
|
||
// Start emitting the scrolling event when the scroll begins | ||
if (!isScrollingRef.current) { | ||
emitScrolling(true); | ||
isScrollingRef.current = true; | ||
} | ||
|
||
// End the scroll and emit after a brief timeout to detect the end of scrolling | ||
const timeout = setTimeout(() => { | ||
emitScrolling(false); | ||
isScrollingRef.current = false; | ||
}, 250); | ||
|
||
return () => clearTimeout(timeout); | ||
}, [lastScrollEvent]); | ||
|
||
return triggerScrollEvent; | ||
}; | ||
|
||
export default useScrollEventEmitter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters