-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useRafLoop): reworked the hook, now it do not re-render parent c…
…omponent. BREAKING CHANGE: changed return array, now it returns only functions in next order: [stop, start, isActive]. Parent component is not re-rendered on loop start/stop.
- Loading branch information
Showing
5 changed files
with
158 additions
and
94 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,45 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
import { useCallback, useEffect, useMemo, useRef } from 'react'; | ||
|
||
export type RafLoopReturns = [() => void, boolean, () => void]; | ||
export type RafLoopReturns = [() => void, () => void, () => boolean]; | ||
|
||
export default function useRafLoop(callback: CallableFunction): RafLoopReturns { | ||
export default function useRafLoop(callback: FrameRequestCallback, initiallyActive = true): RafLoopReturns { | ||
const raf = useRef<number | null>(null); | ||
const [isActive, setIsActive] = useState<boolean>(true); | ||
|
||
function loopStep(time: number) { | ||
callback(time); | ||
raf.current = requestAnimationFrame(loopStep); | ||
} | ||
|
||
function loopStop() { | ||
setIsActive(false); | ||
} | ||
|
||
function loopStart() { | ||
setIsActive(true); | ||
} | ||
|
||
function clearCurrentLoop() { | ||
raf.current && cancelAnimationFrame(raf.current); | ||
} | ||
|
||
useEffect(() => clearCurrentLoop, []); | ||
const rafActivity = useRef<boolean>(false); | ||
const rafCallback = useRef(callback); | ||
rafCallback.current = callback; | ||
|
||
const step = useCallback((time: number) => { | ||
if (rafActivity.current) { | ||
rafCallback.current(time); | ||
raf.current = requestAnimationFrame(step); | ||
} | ||
}, []); | ||
|
||
const result = useMemo(() => ([ | ||
() => { // stop | ||
if (rafActivity.current) { | ||
rafActivity.current = false; | ||
raf.current && cancelAnimationFrame(raf.current); | ||
} | ||
}, | ||
() => { // start | ||
if (!rafActivity.current) { | ||
rafActivity.current = true; | ||
raf.current = requestAnimationFrame(step); | ||
} | ||
}, | ||
(): boolean => rafActivity.current // isActive | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
] as RafLoopReturns), []); | ||
|
||
useEffect(() => { | ||
clearCurrentLoop(); | ||
isActive && (raf.current = requestAnimationFrame(loopStep)); | ||
if (initiallyActive) { | ||
result[1](); | ||
} | ||
|
||
return clearCurrentLoop; | ||
}, [isActive, callback]); | ||
return result[0]; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
return [loopStop, isActive, loopStart]; | ||
return result; | ||
} |
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