mirror of https://github.com/msgbyte/tailchat
feat: 增加禁言功能
parent
5dfc9785f5
commit
43c5c04056
@ -0,0 +1,41 @@
|
||||
import { useCallback, useEffect, useRef } from 'react';
|
||||
import { useUpdateRef } from './useUpdateRef';
|
||||
|
||||
export function useInterval(
|
||||
fn: () => void,
|
||||
delay: number | undefined,
|
||||
options?: {
|
||||
immediate?: boolean;
|
||||
}
|
||||
) {
|
||||
const immediate = options?.immediate;
|
||||
|
||||
const fnRef = useUpdateRef(fn);
|
||||
const timerRef = useRef<number>();
|
||||
|
||||
useEffect(() => {
|
||||
if (typeof delay !== 'number' || delay < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (immediate) {
|
||||
fnRef.current();
|
||||
}
|
||||
timerRef.current = window.setInterval(() => {
|
||||
fnRef.current();
|
||||
}, delay);
|
||||
return () => {
|
||||
if (timerRef.current) {
|
||||
window.clearInterval(timerRef.current);
|
||||
}
|
||||
};
|
||||
}, [delay]);
|
||||
|
||||
const clear = useCallback(() => {
|
||||
if (timerRef.current) {
|
||||
window.clearInterval(timerRef.current);
|
||||
}
|
||||
}, []);
|
||||
|
||||
return clear;
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
import { useLayoutEffect, useState } from 'react';
|
||||
import { shallowEqual } from 'react-redux';
|
||||
|
||||
/**
|
||||
* 对输入对象增加一层浅比较, 如果对象浅比较结果一致则返回原对象(防止更新)
|
||||
*/
|
||||
export function useShallowObject<T>(object: T): T {
|
||||
const [state, setState] = useState<T>(object);
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (!shallowEqual(state, object)) {
|
||||
setState(object);
|
||||
}
|
||||
}, [object]);
|
||||
|
||||
return state;
|
||||
}
|
Loading…
Reference in New Issue