From 2bc618d66b4e087afa97010532d3b3b9408bd9ab Mon Sep 17 00:00:00 2001 From: Felix Kaspar Date: Fri, 20 Oct 2023 02:51:58 +0200 Subject: [PATCH] Fixed yields for functions --- public/traverseOperations.js | 2 +- routes/api/workflow.js | 2 +- traverseOperations.js | 68 ++++++++++++++++++++++++++---------- 3 files changed, 52 insertions(+), 20 deletions(-) diff --git a/public/traverseOperations.js b/public/traverseOperations.js index 8b8e12716..47ed301e5 100644 --- a/public/traverseOperations.js +++ b/public/traverseOperations.js @@ -5,7 +5,7 @@ import { rotatePages } from "./functions/rotatePDF.js"; import { splitPDF } from "./functions/splitPDF.js"; import { organizeWaitOperations } from "./organizeWaitOperations.js"; -// TODO: Make this run with feedback like the server side func. +// TODO: Make this run with feedback like the server side func & The serverside func also got now functionality & fixes for split, so copy it and make it work with the new readsteam system on frontend export async function traverseOperations(operations, input) { const waitOperations = organizeWaitOperations(operations); const results = []; diff --git a/routes/api/workflow.js b/routes/api/workflow.js index 91fdb83cd..a6a1465b9 100644 --- a/routes/api/workflow.js +++ b/routes/api/workflow.js @@ -45,11 +45,11 @@ router.post("/:workflowUuid?", [ while (true) { iteration = await traverse.next(); if (iteration.done) { - console.log(iteration.value); pdfResults = iteration.value; console.log("Done"); break; } + console.log(iteration.value); } console.log("Download"); diff --git a/traverseOperations.js b/traverseOperations.js index 2aa3177f3..42215e153 100644 --- a/traverseOperations.js +++ b/traverseOperations.js @@ -15,6 +15,7 @@ export async function * traverseOperations(operations, input) { // TODO: Pult all nextOperation() in the for await, like for "extract" async function * nextOperation(operations, input) { + console.log(Array.isArray(operations) && operations.length == 0); if(Array.isArray(operations) && operations.length == 0) { // isEmpty if(Array.isArray(input)) { console.log("operation done: " + input[0].fileName + "+"); @@ -36,6 +37,7 @@ export async function * traverseOperations(operations, input) { } async function * computeOperation(operation, input) { + yield "Starting: " + operation.type; switch (operation.type) { case "done": console.log("Done operation will get called if all waits are done. Skipping for now.") @@ -59,7 +61,9 @@ export async function * traverseOperations(operations, input) { waitOperation.waitCount--; if(waitOperation.waitCount == 0) { - await nextOperation(waitOperation.doneOperation.operations, waitOperation.input); + for await (const value of nextOperation(waitOperation.doneOperation.operations, waitOperation.input)) { + yield value; + } } break; case "removeObjects": @@ -69,13 +73,17 @@ export async function * traverseOperations(operations, input) { for (let i = 0; i < input.length; i++) { // TODO: modfiy input input[i].fileName += "_removedObjects"; - await nextOperation(operation.operations, input[i]); + for await (const value of nextOperation(operation.operations, input[i])) { + yield value; + } } } else { // TODO: modfiy input input.fileName += "_removedObjects"; - await nextOperation(operation.operations, input); + for await (const value of nextOperation(operation.operations, input)) { + yield value; + } } break; case "extract": @@ -111,7 +119,6 @@ export async function * traverseOperations(operations, input) { splitCount: splitResult.length }) } - console.log(splits); for await (const value of nextOperation(operation.operations, splits)) { yield value; @@ -143,13 +150,17 @@ export async function * traverseOperations(operations, input) { for (let i = 0; i < input.length; i++) { // TODO: modfiy input input[i].fileName += "_filledField"; - await nextOperation(operation.operations, input[i]); + for await (const value of nextOperation(operation.operations, input[i])) { + yield value; + } } } else { // TODO: modfiy input input.fileName += "_filledField"; - await nextOperation(operation.operations, input); + for await (const value of nextOperation(operation.operations, input)) { + yield value; + } } break; case "extractImages": @@ -159,13 +170,17 @@ export async function * traverseOperations(operations, input) { for (let i = 0; i < input.length; i++) { // TODO: modfiy input input[i].fileName += "_extractedImages"; - await nextOperation(operation.operations, input[i]); + for await (const value of nextOperation(operation.operations, input[i])) { + yield value; + } } } else { // TODO: modfiy input input.fileName += "_extractedImages"; - await nextOperation(operation.operations, input); + for await (const value of nextOperation(operation.operations, input)) { + yield value; + } } break; case "merge": @@ -181,7 +196,9 @@ export async function * traverseOperations(operations, input) { // Only one input, no need to merge input.fileName += "_merged"; } - await nextOperation(operation.operations, input); + for await (const value of nextOperation(operation.operations, input)) { + yield value; + } break; case "transform": { console.warn("Transform not implemented yet.") @@ -189,13 +206,17 @@ export async function * traverseOperations(operations, input) { for (let i = 0; i < input.length; i++) { // TODO: modfiy input input[i].fileName += "_transformed"; - await nextOperation(operation.operations, input[i]); + for await (const value of nextOperation(operation.operations, input[i])) { + yield value; + } } } else { // TODO: modfiy input input.fileName += "_transformed"; - await nextOperation(operation.operations, input); + for await (const value of nextOperation(operation.operations, input)) { + yield value; + } } break; } @@ -204,13 +225,17 @@ export async function * traverseOperations(operations, input) { for (let i = 0; i < input.length; i++) { input[i].fileName += "_extractedPages"; input[i].buffer = await extractPages(input[i].buffer, operation.values["pagesToExtractArray"]); - await nextOperation(operation.operations, input[i]); + for await (const value of nextOperation(operation.operations, input[i])) { + yield value; + } } } else { input.fileName += "_extractedPages"; input.buffer = await extractPages(input.buffer, operation.values["pagesToExtractArray"]); - await nextOperation(operation.operations, input); + for await (const value of nextOperation(operation.operations, input)) { + yield value; + } } break; case "rotate": @@ -218,13 +243,17 @@ export async function * traverseOperations(operations, input) { for (let i = 0; i < input.length; i++) { input[i].fileName += "_turned"; input[i].buffer = await rotatePages(input[i].buffer, operation.values["rotation"]); - await nextOperation(operation.operations, input[i]); + for await (const value of nextOperation(operation.operations, input[i])) { + yield value; + } } } else { input.fileName += "_turned"; input.buffer = await rotatePages(input.buffer, operation.values["rotation"]); - await nextOperation(operation.operations, input); + for await (const value of nextOperation(operation.operations, input)) { + yield value; + } } break; case "impose": @@ -232,19 +261,22 @@ export async function * traverseOperations(operations, input) { for (let i = 0; i < input.length; i++) { input[i].fileName += "_imposed"; input[i].buffer = await impose(input[i].buffer, operation.values["nup"], operation.values["format"]); - await nextOperation(operation.operations, input[i]); + for await (const value of nextOperation(operation.operations, input[i])) { + yield value; + } } } else { input.fileName += "_imposed"; input.buffer = await impose(input.buffer, operation.values["nup"], operation.values["format"]); - await nextOperation(operation.operations, input); + for await (const value of nextOperation(operation.operations, input)) { + yield value; + } } break; default: console.log("operation type unknown: ", operation.type); break; } - yield operation.type; } } \ No newline at end of file