mirror of
https://github.com/dbgate/dbgate
synced 2024-11-07 20:26:23 +00:00
throttle for messages view
This commit is contained in:
parent
6e8ef35902
commit
52d2560c78
@ -51,7 +51,7 @@ function formatDuration(duration) {
|
||||
return `${Math.round(duration / 1000)} s`;
|
||||
}
|
||||
|
||||
export default function MessagesView({ items, onMessageClick }) {
|
||||
function MessagesView({ items, onMessageClick }) {
|
||||
const handleClick = (row) => {
|
||||
if (onMessageClick) onMessageClick(row);
|
||||
};
|
||||
@ -99,3 +99,5 @@ export default function MessagesView({ items, onMessageClick }) {
|
||||
</MainContainer>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(MessagesView);
|
@ -1,15 +1,29 @@
|
||||
import _ from 'lodash';
|
||||
import React from 'react';
|
||||
import MessagesView from './MessagesView';
|
||||
import useSocket from '../utility/SocketProvider';
|
||||
|
||||
export default function SessionMessagesView({ sessionId, onMessageClick, executeNumber }) {
|
||||
const [messages, setMessages] = React.useState([]);
|
||||
const [displayedMessages, setDisplayedMessages] = React.useState([]);
|
||||
const cachedMessagesRef = React.useRef([]);
|
||||
const socket = useSocket();
|
||||
|
||||
const handleInfo = React.useCallback((info) => setMessages((items) => [...items, info]), []);
|
||||
const displayCachedMessages = React.useMemo(
|
||||
() =>
|
||||
_.throttle(() => {
|
||||
setDisplayedMessages([...cachedMessagesRef.current]);
|
||||
}, 500),
|
||||
[]
|
||||
);
|
||||
|
||||
const handleInfo = React.useCallback((info) => {
|
||||
cachedMessagesRef.current.push(info);
|
||||
displayCachedMessages();
|
||||
}, []);
|
||||
|
||||
React.useEffect(() => {
|
||||
setMessages([]);
|
||||
setDisplayedMessages([]);
|
||||
cachedMessagesRef.current = [];
|
||||
}, [executeNumber]);
|
||||
|
||||
React.useEffect(() => {
|
||||
@ -21,5 +35,5 @@ export default function SessionMessagesView({ sessionId, onMessageClick, execute
|
||||
}
|
||||
}, [sessionId, socket]);
|
||||
|
||||
return <MessagesView items={messages} onMessageClick={onMessageClick} />;
|
||||
return <MessagesView items={displayedMessages} onMessageClick={onMessageClick} />;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user