import React, { useEffect, useRef } from 'react'; import { socket } from '../socket'; import { InputGroup, Form, Button } from 'react-bootstrap'; import { DocStringComponent } from './DocStringComponent'; interface AsyncMethodProps { name: string; parent_path: string; parameters: Record; value: Record; docString?: string; hideOutput?: boolean; } export const AsyncMethodComponent = React.memo((props: AsyncMethodProps) => { const renderCount = useRef(0); const formRef = useRef(null); const { name, parent_path, docString, value: runningTask } = props; const execute = async (event: React.FormEvent) => { event.preventDefault(); let method_name: string; const args = {}; if (runningTask !== undefined && runningTask !== null) { method_name = `stop_${name}`; } else { Object.keys(props.parameters).forEach( (name) => (args[name] = event.target[name].value) ); method_name = `start_${name}`; } socket.emit('frontend_update', { name: method_name, parent_path: parent_path, value: { args: args } }); }; useEffect(() => { renderCount.current++; // updates the value of each form control that has a matching name in the // runningTask object if (runningTask) { const formElement = formRef.current; if (formElement) { Object.entries(runningTask).forEach(([name, value]) => { const inputElement = formElement.elements.namedItem(name); if (inputElement) { inputElement.value = value; } }); } } }, [runningTask]); const args = Object.entries(props.parameters).map(([name, type], index) => { const form_name = `${name} (${type})`; const value = runningTask && runningTask[name]; const isRunning = value !== undefined && value !== null; return ( {form_name} ); }); return (

Render count: {renderCount.current}

Function: {name}
{args}
); });