mirror of https://github.com/msgbyte/tailchat
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
563 B
TypeScript
28 lines
563 B
TypeScript
1 year ago
|
import { DependencyList, useLayoutEffect, useRef } from 'react';
|
||
|
import { useEvent } from './useEvent';
|
||
|
|
||
|
/**
|
||
|
* Call once on data ready(validator return true)
|
||
|
*/
|
||
|
export function useDataReady(
|
||
|
validator: () => boolean,
|
||
|
cb: () => void,
|
||
|
deps?: DependencyList
|
||
|
) {
|
||
|
const isReadyRef = useRef(false);
|
||
|
|
||
|
const _validator = useEvent(validator);
|
||
|
const _callback = useEvent(cb);
|
||
|
|
||
|
useLayoutEffect(() => {
|
||
|
if (isReadyRef.current) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (_validator() === true) {
|
||
|
_callback();
|
||
|
isReadyRef.current = true;
|
||
|
}
|
||
|
}, deps);
|
||
|
}
|