Spaces:
Running
Running
Download frontends/react/src/useApi.ts from elvis-hf/optimization: direct link, hf CLI and curl.
- Browser
- Download file 1.01 kB
-
https://huggingface.co/spaces/elvis-hf/optimization/resolve/main/frontends/react/src/useApi.ts
- Command line
-
hf download hf://spaces/elvis-hf/optimization/frontends/react/src/useApi.ts
-
curl -L -o useApi.ts https://huggingface.co/spaces/elvis-hf/optimization/resolve/main/frontends/react/src/useApi.ts
1.01 kB
| import { useState, useRef, useEffect } from "react"; | |
| import WebSocketApi from "./websocket.ts"; | |
| import type { PlotData, Settings } from "./types.ts"; | |
| export default function useApi(url: string) { | |
| const api = useRef<WebSocketApi>(new WebSocketApi()); | |
| const [plotData, setPlotData] = useState<PlotData>({}); | |
| api.current.onMessage( | |
| (data: unknown) => { | |
| // todo data validation / type | |
| setPlotData((prevData) => ({ ...prevData, ...(data as PlotData) })); | |
| } | |
| ) | |
| useEffect(() => { | |
| api.current.connect(url); | |
| return () => api.current.disconnect(); | |
| }, [url]); | |
| return { | |
| plotData, | |
| sendInit: (settings: Settings) => api.current.sendInit(settings), | |
| sendReset: () => api.current.sendReset(), | |
| sendNextStep: () => api.current.sendNextStep(), | |
| sendPrevStep: () => api.current.sendPrevStep(), | |
| sendPlay: () => api.current.sendPlay(), | |
| sendPause: () => api.current.sendPause(), | |
| onReconnect: (handler: () => void) => api.current.onReconnect(handler), | |
| }; | |
| } |