mirror of https://github.com/msgbyte/tailchat
refactor: 会话ack管理
parent
d53602cdc7
commit
f08b122322
@ -0,0 +1,17 @@
|
|||||||
|
import { DependencyList, useEffect } from 'react';
|
||||||
|
import { useTimeoutFn } from './useTimeoutFn';
|
||||||
|
|
||||||
|
export type UseDebounceReturn = [() => boolean | null, () => void];
|
||||||
|
|
||||||
|
export function useDebounce(
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||||
|
fn: Function,
|
||||||
|
ms = 0,
|
||||||
|
deps: DependencyList = []
|
||||||
|
): UseDebounceReturn {
|
||||||
|
const [isReady, cancel, reset] = useTimeoutFn(fn, ms);
|
||||||
|
|
||||||
|
useEffect(reset, deps);
|
||||||
|
|
||||||
|
return [isReady, cancel];
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
import { useCallback, useEffect, useRef } from 'react';
|
||||||
|
|
||||||
|
export type UseTimeoutFnReturn = [() => boolean | null, () => void, () => void];
|
||||||
|
|
||||||
|
export function useTimeoutFn(
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||||
|
fn: Function,
|
||||||
|
ms = 0
|
||||||
|
): UseTimeoutFnReturn {
|
||||||
|
const ready = useRef<boolean | null>(false);
|
||||||
|
const timeout = useRef<ReturnType<typeof setTimeout>>();
|
||||||
|
const callback = useRef(fn);
|
||||||
|
|
||||||
|
const isReady = useCallback(() => ready.current, []);
|
||||||
|
|
||||||
|
const set = useCallback(() => {
|
||||||
|
ready.current = false;
|
||||||
|
timeout.current && clearTimeout(timeout.current);
|
||||||
|
|
||||||
|
timeout.current = setTimeout(() => {
|
||||||
|
ready.current = true;
|
||||||
|
callback.current();
|
||||||
|
}, ms);
|
||||||
|
}, [ms]);
|
||||||
|
|
||||||
|
const clear = useCallback(() => {
|
||||||
|
ready.current = null;
|
||||||
|
timeout.current && clearTimeout(timeout.current);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
// update ref when function changes
|
||||||
|
useEffect(() => {
|
||||||
|
callback.current = fn;
|
||||||
|
}, [fn]);
|
||||||
|
|
||||||
|
// set on mount, clear on unmount
|
||||||
|
useEffect(() => {
|
||||||
|
set();
|
||||||
|
|
||||||
|
return clear;
|
||||||
|
}, [ms]);
|
||||||
|
|
||||||
|
return [isReady, clear, set];
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
import { useCallback, useEffect, useMemo, useRef } from 'react';
|
||||||
|
import {
|
||||||
|
ChatMessage,
|
||||||
|
isValidStr,
|
||||||
|
updateAck,
|
||||||
|
useAppDispatch,
|
||||||
|
useUpdateRef,
|
||||||
|
} from 'tailchat-shared';
|
||||||
|
import { chatActions } from 'tailchat-shared/redux/slices';
|
||||||
|
import _debounce from 'lodash/debounce';
|
||||||
|
|
||||||
|
export function useMessageAck(converseId: string, messages: ChatMessage[]) {
|
||||||
|
const messagesRef = useUpdateRef(messages);
|
||||||
|
const dispatch = useAppDispatch();
|
||||||
|
const lastMessageIdRef = useRef('');
|
||||||
|
|
||||||
|
const setConverseAck = useMemo(
|
||||||
|
() =>
|
||||||
|
_debounce(
|
||||||
|
(converseId: string, lastMessageId: string) => {
|
||||||
|
if (
|
||||||
|
isValidStr(lastMessageIdRef.current) &&
|
||||||
|
lastMessageId <= lastMessageIdRef.current
|
||||||
|
) {
|
||||||
|
// 更新的数字比较小,跳过
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
dispatch(chatActions.setConverseAck({ converseId, lastMessageId }));
|
||||||
|
updateAck(converseId, lastMessageId);
|
||||||
|
lastMessageIdRef.current = lastMessageId;
|
||||||
|
},
|
||||||
|
1000,
|
||||||
|
{ leading: false, trailing: true }
|
||||||
|
),
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// 设置当前
|
||||||
|
if (messagesRef.current.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const lastMessageId =
|
||||||
|
messagesRef.current[messagesRef.current.length - 1]._id;
|
||||||
|
setConverseAck(converseId, lastMessageId);
|
||||||
|
}, [converseId]);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新会话最新消息
|
||||||
|
*/
|
||||||
|
const updateConverseAck = useCallback(
|
||||||
|
(lastMessageId: string) => {
|
||||||
|
setConverseAck(converseId, lastMessageId);
|
||||||
|
},
|
||||||
|
[converseId]
|
||||||
|
);
|
||||||
|
|
||||||
|
return { updateConverseAck };
|
||||||
|
}
|
Loading…
Reference in New Issue