From 4e9effd23ad33c8d171ffbedbe626689baf8d07e Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Thu, 10 Oct 2024 17:56:49 +0530 Subject: [PATCH] wip: delay socket event emissions --- src/components/organisms/RightSidePanel.tsx | 77 ++++++++++++++------- 1 file changed, 52 insertions(+), 25 deletions(-) diff --git a/src/components/organisms/RightSidePanel.tsx b/src/components/organisms/RightSidePanel.tsx index 415d990d..6f961695 100644 --- a/src/components/organisms/RightSidePanel.tsx +++ b/src/components/organisms/RightSidePanel.tsx @@ -55,6 +55,8 @@ export const RightSidePanel: React.FC = ({ onFinishCapture const [showCaptureText, setShowCaptureText] = useState(true); const [captureStage, setCaptureStage] = useState<'initial' | 'pagination' | 'limit' | 'complete'>('initial'); const [hoverStates, setHoverStates] = useState<{ [id: string]: boolean }>({}); + const [pendingEvents, setPendingEvents] = useState>([]); + const [isEmittingEvents, setIsEmittingEvents] = useState(false); const { lastAction, notify } = useGlobalInfoStore(); const { getText, startGetText, stopGetText, getScreenshot, startGetScreenshot, stopGetScreenshot, getList, startGetList, stopGetList, startPaginationMode, stopPaginationMode, paginationType, updatePaginationType, limitType, customLimit, updateLimitType, updateCustomLimit, stopLimitMode, startLimitMode } = useActionContext(); @@ -122,6 +124,35 @@ export const RightSidePanel: React.FC = ({ onFinishCapture const handlePairDelete = () => {} + const addPendingEvent = (action: string, settings: any) => { + setPendingEvents(prev => [...prev, { action, settings }]); + }; + + const emitPendingEvents = useCallback(() => { + if (pendingEvents.length === 0) { + notify('info', 'No pending events to emit'); + return; + } + + setIsEmittingEvents(true); + + const emitEvents = async () => { + for (const event of pendingEvents) { + await new Promise((resolve) => { + socket?.emit('action', event, () => { + resolve(); + }); + }); + } + setPendingEvents([]); + setIsEmittingEvents(false); + onFinishCapture(); + }; + + emitEvents(); + }, [pendingEvents, socket, onFinishCapture]); + + const handleTextLabelChange = (id: number, label: string, listId?: number, fieldKey?: string) => { if (listId !== undefined && fieldKey !== undefined) { // Prevent editing if the field is confirmed @@ -208,10 +239,11 @@ export const RightSidePanel: React.FC = ({ onFinishCapture const settings = getTextSettingsObject(); const hasTextSteps = browserSteps.some(step => step.type === 'text'); if (hasTextSteps) { - socket?.emit('action', { action: 'scrapeSchema', settings }); - } - onFinishCapture(); - }, [stopGetText, getTextSettingsObject, socket, browserSteps, confirmedTextSteps]); + // socket?.emit('action', { action: 'scrapeSchema', settings }); + addPendingEvent('scrapeSchema', settings); + } + //onFinishCapture(); + }, [stopGetText, getTextSettingsObject, browserSteps, confirmedTextSteps, addPendingEvent]); const getListSettingsObject = useCallback(() => { let settings: { @@ -263,13 +295,14 @@ export const RightSidePanel: React.FC = ({ onFinishCapture const stopCaptureAndEmitGetListSettings = useCallback(() => { const settings = getListSettingsObject(); if (settings) { - socket?.emit('action', { action: 'scrapeList', settings }); + // socket?.emit('action', { action: 'scrapeList', settings }); + addPendingEvent('scrapeList', settings) } else { notify('error', 'Unable to create list settings. Make sure you have defined a field for the list.'); } handleStopGetList(); - onFinishCapture(); - }, [stopGetList, getListSettingsObject, socket, notify, handleStopGetList]); + //onFinishCapture(); + }, [stopGetList, getListSettingsObject, addPendingEvent, notify, handleStopGetList]); const handleConfirmListCapture = useCallback(() => { switch (captureStage) { @@ -362,7 +395,8 @@ export const RightSidePanel: React.FC = ({ onFinishCapture caret: 'hide', scale: 'device', }; - socket?.emit('action', { action: 'screenshot', settings: screenshotSettings }); + // socket?.emit('action', { action: 'screenshot', settings: screenshotSettings }); + addPendingEvent('screenshot', screenshotSettings) addScreenshotStep(fullPage); stopGetScreenshot(); }; @@ -452,23 +486,6 @@ export const RightSidePanel: React.FC = ({ onFinishCapture )} - { - workflow.workflow.map((pair, i) => { - const activeId = workflow.workflow.length - i; - return ( - {}} - isActive={activeId === i + 1} - key={workflow.workflow.length - i} - index={workflow.workflow.length - i} - pair={pair} - updateWorkflow={setWorkflow} - numberOfPairs={workflow.workflow.length} - handleSelectPairForEdit={() => {}} - /> - ); - }) - } {browserSteps.map(step => ( handleMouseEnter(step.id)} onMouseLeave={() => handleMouseLeave(step.id)}> { @@ -596,6 +613,16 @@ export const RightSidePanel: React.FC = ({ onFinishCapture )} ))} + {pendingEvents.length > 0 && ( + + )} );