fix: api watch data

This commit is contained in:
jialin
2024-06-29 23:24:12 +08:00
parent afb2a13b80
commit 9f4f28edaf
8 changed files with 48 additions and 12 deletions
+1 -1
View File
@@ -99,7 +99,7 @@ const SealTable: React.FC<SealTableProps> = (props) => {
columnProps as SealColumnProps;
if (React.isValidElement(child)) {
return (
<Col span={span}>
<Col span={span} key={i}>
<TableHeader
title={title}
style={headerStyle}
+12 -2
View File
@@ -125,11 +125,21 @@ const useSetChunkRequest = () => {
};
const resetResultSchema = (result: any[]) => {
// ============ handle list data ============
// return _.map(result, (data: any) => {
// if (data.type === WatchEventType.DELETE) {
// data.ids = _.map(data.items, (item: any) => item.id);
// }
// data.collection = data.items || [];
// return data;
// });
// ============ handle list data ============
return _.map(result, (data: any) => {
if (data.type === WatchEventType.DELETE) {
data.ids = _.map(data.items, (item: any) => item.id);
data.ids = data.data?.id ? [data.data.id] : [];
}
data.collection = data.items || [];
data.collection = data.data ? [data.data] : [];
return data;
});
};
+1 -1
View File
@@ -37,8 +37,8 @@ export default function useEventSource() {
eventSourceRef.current.onmessage = (res: any) => {
try {
console.log('event source message: ', { res, resData: res });
const data = JSON.parse(res.data);
console.log('event source message: ', { data, resData: res });
onmessage(data);
} catch (error) {
// error
+2 -1
View File
@@ -3,8 +3,9 @@ import _ from 'lodash';
interface ChunkedCollection {
ids: string[];
data: any;
collection: any[];
type: string;
type: string | number;
}
// Only used to update lists without nested state
export function useUpdateChunkedList(
+3 -7
View File
@@ -372,19 +372,15 @@ const Models: React.FC = () => {
useEffect(() => {
fetchData();
// createModelsDataByFetch();
createModelEvent();
createModelsChunkRequest();
setTimeout(() => {
createModelsChunkRequest();
}, 1000);
return () => {
chunkRequedtRef.current?.current?.cancel?.();
};
}, [queryParams]);
useEffect(() => {
// watch models list
}, [queryParams]);
const renderChildren = (list: any) => {
return (
<Space size={16} direction="vertical" style={{ width: '100%' }}>
+16
View File
@@ -75,3 +75,19 @@ export const readStreamData = async (
// callback(chunk);
await readStreamData(reader, decoder, callback);
};
export const readTextEventStreamData = async (
reader: any,
decoder: TextDecoder,
callback: (data: any) => void
) => {
const { done, value } = await reader.read();
if (done) {
return;
}
let chunk = decoder.decode(value, { stream: true });
callback(chunk);
await readTextEventStreamData(reader, decoder, callback);
};