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
View File
@@ -1 +1,2 @@
PORT=9000 PORT=9000
UMI_DEV_SERVER_COMPRESS=none
+12
View File
@@ -11,6 +11,18 @@ export default function createProxyTable(target?: string) {
secure: false, secure: false,
ws: true, ws: true,
pathRewrite: (pth: string) => pth.replace(`/^/${api}/`, `/${api}`), pathRewrite: (pth: string) => pth.replace(`/^/${api}/`, `/${api}`),
// onProxyRes: (proxyRes: any, req: any, res: any) => {
// if (req.headers.accept === 'text/event-stream') {
// res.writeHead(res.statusCode, {
// 'Content-Type': 'text/event-stream',
// 'Cache-Control': 'no-transform',
// Connection: 'keep-alive',
// 'X-Accel-Buffering': 'no',
// 'Access-Control-Allow-Origin': '*'
// });
// proxyRes.pipe(res);
// }
// },
headers: { headers: {
origin: newTarget, origin: newTarget,
Connection: 'keep-alive' Connection: 'keep-alive'
+1 -1
View File
@@ -99,7 +99,7 @@ const SealTable: React.FC<SealTableProps> = (props) => {
columnProps as SealColumnProps; columnProps as SealColumnProps;
if (React.isValidElement(child)) { if (React.isValidElement(child)) {
return ( return (
<Col span={span}> <Col span={span} key={i}>
<TableHeader <TableHeader
title={title} title={title}
style={headerStyle} style={headerStyle}
+12 -2
View File
@@ -125,11 +125,21 @@ const useSetChunkRequest = () => {
}; };
const resetResultSchema = (result: any[]) => { 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) => { return _.map(result, (data: any) => {
if (data.type === WatchEventType.DELETE) { 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; return data;
}); });
}; };
+1 -1
View File
@@ -37,8 +37,8 @@ export default function useEventSource() {
eventSourceRef.current.onmessage = (res: any) => { eventSourceRef.current.onmessage = (res: any) => {
try { try {
console.log('event source message: ', { res, resData: res });
const data = JSON.parse(res.data); const data = JSON.parse(res.data);
console.log('event source message: ', { data, resData: res });
onmessage(data); onmessage(data);
} catch (error) { } catch (error) {
// error // error
+2 -1
View File
@@ -3,8 +3,9 @@ import _ from 'lodash';
interface ChunkedCollection { interface ChunkedCollection {
ids: string[]; ids: string[];
data: any;
collection: any[]; collection: any[];
type: string; type: string | number;
} }
// Only used to update lists without nested state // Only used to update lists without nested state
export function useUpdateChunkedList( export function useUpdateChunkedList(
+3 -7
View File
@@ -372,19 +372,15 @@ const Models: React.FC = () => {
useEffect(() => { useEffect(() => {
fetchData(); fetchData();
// createModelsDataByFetch(); setTimeout(() => {
createModelEvent(); createModelsChunkRequest();
createModelsChunkRequest(); }, 1000);
return () => { return () => {
chunkRequedtRef.current?.current?.cancel?.(); chunkRequedtRef.current?.current?.cancel?.();
}; };
}, [queryParams]); }, [queryParams]);
useEffect(() => {
// watch models list
}, [queryParams]);
const renderChildren = (list: any) => { const renderChildren = (list: any) => {
return ( return (
<Space size={16} direction="vertical" style={{ width: '100%' }}> <Space size={16} direction="vertical" style={{ width: '100%' }}>
+16
View File
@@ -75,3 +75,19 @@ export const readStreamData = async (
// callback(chunk); // callback(chunk);
await readStreamData(reader, decoder, callback); 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);
};