From 2afef05143878699d025880730f4d39cae3829d4 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Wed, 5 Jun 2024 04:32:28 +0530 Subject: [PATCH] feat: interpret recording inside editor --- .../classes/Interpreter.ts | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/server/src/workflow-management/classes/Interpreter.ts b/server/src/workflow-management/classes/Interpreter.ts index 41872d29..69fa9a6c 100644 --- a/server/src/workflow-management/classes/Interpreter.ts +++ b/server/src/workflow-management/classes/Interpreter.ts @@ -105,4 +105,77 @@ export class WorkflowInterpreter { this.breakpoints = data }); } + + /** + * Sets up the instance of {@link Interpreter} and interprets + * the workflow inside the recording editor. + * Cleans up this interpreter instance after the interpretation is finished. + * @param workflow The workflow to interpret. + * @param page The page instance used to interact with the browser. + * @param updatePageOnPause A callback to update the page after a pause. + * @returns {Promise} + */ + public interpretRecordingInEditor = async ( + workflow: WorkflowFile, + page: Page, + updatePageOnPause: (page: Page) => void, + settings: InterpreterSettings, + ) => { + const params = settings.params ? settings.params : null; + delete settings.params; + const options = { + ...settings, + debugChannel: { + activeId: (id: any) => { + this.activeId = id; + this.socket.emit('activePairId', id); + }, + debugMessage: (msg: any) => { + this.debugMessages.push(`[${new Date().toLocaleString()}] ` + msg); + this.socket.emit('log', msg) + }, + }, + serializableCallback: (data: any) => { + this.socket.emit('serializableCallback', data); + }, + binaryCallback: (data: string, mimetype: string) => { + this.socket.emit('binaryCallback', {data, mimetype}); + } + } + + const interpreter = new Interpreter(workflow, options); + this.interpreter = interpreter; + + interpreter.on('flag', async (page, resume) => { + if (this.activeId !== null && this.breakpoints[this.activeId]) { + logger.log('debug',`breakpoint hit id: ${this.activeId}`); + this.socket.emit('breakpointHit'); + this.interpretationIsPaused = true; + } + + if (this.interpretationIsPaused) { + this.interpretationResume = resume; + logger.log('debug',`Paused inside of flag: ${page.url()}`); + updatePageOnPause(page); + this.socket.emit('log', '----- The interpretation has been paused -----', false); + } else { + resume(); + } + }); + + this.socket.emit('log', '----- Starting the interpretation -----', false); + + const status = await interpreter.run(page, params); + + this.socket.emit('log', `----- The interpretation finished with status: ${status} -----`, false); + + logger.log('debug',`Interpretation finished`); + this.interpreter = null; + this.socket.emit('activePairId', -1); + this.interpretationIsPaused = false; + this.interpretationResume = null; + this.socket.emit('finished'); + }; + + }