-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚗️ [useVurttle] Experiment with throttle hook (#11)
* 🚪 [useVurtis] Expose more internals from hook * ⚗️ [useVurttle] New timeout and throttle hooks * 📝 [Report] Add changeset
- Loading branch information
Showing
6 changed files
with
112 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"vurtis": patch | ||
--- | ||
|
||
Export useTimeout() and useVurttle() hooks. |
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,38 @@ | ||
import {useEffect, useRef} from 'react'; | ||
import type {TimeoutId} from 'beeftools'; | ||
|
||
import {useIsoEffect} from './useIsoEffect'; | ||
|
||
export type TimeoutCallback = (timestamp: number) => void; | ||
|
||
export interface TimeoutOptions { | ||
duration?: number; | ||
disabled?: boolean; | ||
} | ||
|
||
export function useTimeout( | ||
callback: TimeoutCallback, | ||
options: TimeoutOptions = {}, | ||
): void { | ||
const {duration = 0, disabled = false} = options; | ||
|
||
const callbackRef = useRef<TimeoutCallback>(); | ||
const timeoutRef = useRef<TimeoutId>(); | ||
|
||
useIsoEffect(() => { | ||
callbackRef.current = callback; | ||
}, [callback]); | ||
|
||
useEffect(() => { | ||
if (!disabled) { | ||
timeoutRef.current = setTimeout( | ||
() => callbackRef.current?.(Date.now()), | ||
duration, | ||
); | ||
} | ||
|
||
return () => { | ||
clearTimeout(timeoutRef.current); | ||
}; | ||
}, [duration, disabled]); | ||
} |
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,36 @@ | ||
import {useCallback, useEffect, useState} from 'react'; | ||
import {useMounted, useTimeout} from './hooks'; | ||
|
||
// This hook is an opinionated "throttle" for `vurt` changes. | ||
// The idea is that you will feed this hook a value such as | ||
// `listWidth` or `itemHeight`. Upon receiving a new value, | ||
// the `pending` state will become `true` and the timer will | ||
// begin counting down before returning to `false`. This is | ||
// useful for when you need to perform a side-effect to | ||
// virtual container/item changes. A common use-case for | ||
// this throttling layout animations during resize operations. | ||
// This may be necessary to avoid very aggresive re-renders. | ||
|
||
export const VURTTLE_DURATION = 200; | ||
|
||
export function useVurttle(vurtValue = 0) { | ||
const isMounted = useMounted(); | ||
const [pending, setPending] = useState(false); | ||
|
||
const handleReset = useCallback(() => { | ||
setPending(false); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (isMounted() && !pending && vurtValue) { | ||
setPending(true); | ||
} | ||
}, [isMounted, vurtValue]); | ||
|
||
useTimeout(handleReset, { | ||
duration: VURTTLE_DURATION, | ||
disabled: !isMounted() || !pending, | ||
}); | ||
|
||
return pending; | ||
} |