Files
parcer/server/src/browser-management/inputHandlers.ts

58 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-06-08 22:40:37 +05:30
/**
* A set of functions handling reproduction of user input
* on the remote browser instance as well as the generation of workflow pairs.
* These functions are called by the client through socket communication.
*/
import { Socket } from 'socket.io';
import logger from "../logger";
import { Coordinates, ScrollDeltas, KeyboardInput } from '../types';
import { browserPool } from "../server";
import { WorkflowGenerator } from "../workflow-management/classes/Generator";
import { Page } from "playwright";
import { throttle } from "../../../src/helpers/inputHelpers";
import { CustomActions } from "../../../src/shared/types";
2024-06-08 22:50:26 +05:30
/**
* A wrapper function for handling user input.
* This function gets the active browser instance from the browser pool
* and passes necessary arguments to the appropriate handlers.
* e.g. {@link Generator}, {@link RemoteBrowser.currentPage}
*
* Also ignores any user input while interpretation is in progress.
*
* @param handleCallback The callback handler to be called
* @param args - arguments to be passed to the handler
* @category HelperFunctions
*/
2024-06-08 22:50:12 +05:30
const handleWrapper = async (
handleCallback: (
generator: WorkflowGenerator,
page: Page,
args?: any
) => Promise<void>,
args?: any
) => {
const id = browserPool.getActiveBrowserId();
if (id) {
const activeBrowser = browserPool.getRemoteBrowser(id);
if (activeBrowser?.interpreter.interpretationInProgress() && !activeBrowser.interpreter.interpretationIsPaused) {
logger.log('debug', `Ignoring input, while interpretation is in progress`);
return;
}
const currentPage = activeBrowser?.getCurrentPage();
if (currentPage && activeBrowser) {
if (args) {
await handleCallback(activeBrowser.generator, currentPage, args);
} else {
await handleCallback(activeBrowser.generator, currentPage);
}
} else {
logger.log('warn', `No active page for browser ${id}`);
}
} else {
logger.log('warn', `No active browser for id ${id}`);
}
}