From a5f520dd36c648516539495f95115daf60daa339 Mon Sep 17 00:00:00 2001 From: Tristan Yang Date: Thu, 16 Mar 2023 17:37:49 +0800 Subject: [PATCH] refactor: after mid --- src/common/hook/useStreaming/chat.handler.ts | 9 +++++++-- src/common/hook/useStreaming/index.ts | 6 ++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/common/hook/useStreaming/chat.handler.ts b/src/common/hook/useStreaming/chat.handler.ts index ebb86cad..060f7978 100644 --- a/src/common/hook/useStreaming/chat.handler.ts +++ b/src/common/hook/useStreaming/chat.handler.ts @@ -9,6 +9,7 @@ import { ContentTypes } from "../../../app/config"; import { ChatEvent } from "../../../types/sse"; import { AppDispatch } from "../../../app/store"; type CurrentState = { + afterMid: number, ready: boolean; loginUid: number; readUsers: { @@ -47,8 +48,12 @@ const handler = (data: ChatEvent, dispatch: AppDispatch, currState: CurrentState switch (type) { case "normal": case "reply": - // 更新after_mid - dispatch(updateAfterMid(mid)); + { + if (currState.afterMid < mid) { + // 更新after_mid + dispatch(updateAfterMid(mid)); + } + } break; } } diff --git a/src/common/hook/useStreaming/index.ts b/src/common/hook/useStreaming/index.ts index 6d9e3d1a..7a3cde63 100644 --- a/src/common/hook/useStreaming/index.ts +++ b/src/common/hook/useStreaming/index.ts @@ -1,5 +1,8 @@ import { useEffect, useState, useCallback } from "react"; import toast from "react-hot-toast"; +import dayjs from "dayjs"; +import { isNull, omitBy } from "lodash"; + import BASE_URL from "../../../app/config"; import { setReady } from "../../../app/slices/ui"; import { @@ -21,9 +24,7 @@ import { resetAuthData, updateLoginUser } from "../../../app/slices/auth.data"; import chatMessageHandler from "./chat.handler"; import { useAppDispatch, useAppSelector } from "../../../app/store"; import { ServerEvent, UsersStateEvent } from "../../../types/sse"; -import { isNull, omitBy } from "lodash"; import { useRenewMutation } from "../../../app/services/auth"; -import dayjs from "dayjs"; import { getLocalAuthData } from "../../utils"; const getQueryString = (params: { [key: string]: string }) => { @@ -272,6 +273,7 @@ export default function useStreaming() { break; case "chat": { chatMessageHandler(data, dispatch, { + afterMid, ready, loginUid, readUsers,