mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-06-23 07:55:07 +00:00
Updated Workflows (callbacks, new Operator)
This commit is contained in:
parent
a5060f0fd3
commit
a02169048d
@ -32,19 +32,9 @@ router.post("/:workflowUuid?", [
|
|||||||
if(req.body.async === "false") {
|
if(req.body.async === "false") {
|
||||||
console.log("Don't do async");
|
console.log("Don't do async");
|
||||||
|
|
||||||
const traverse = traverseOperations(workflow.operations, inputs);
|
let pdfResults = await traverseOperations(workflow.operations, inputs, (state) => {
|
||||||
|
console.log("State: ", state);
|
||||||
let pdfResults;
|
})
|
||||||
let iteration;
|
|
||||||
while (true) {
|
|
||||||
iteration = await traverse.next();
|
|
||||||
if (iteration.done) {
|
|
||||||
pdfResults = iteration.value;
|
|
||||||
console.log("Done");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
console.log(iteration.value);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("Download");
|
console.log("Download");
|
||||||
await respondWithPdfFiles(res, pdfResults, "workflow-results");
|
await respondWithPdfFiles(res, pdfResults, "workflow-results");
|
||||||
@ -73,22 +63,15 @@ router.post("/:workflowUuid?", [
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const traverse = traverseOperations(workflow.operations, inputs);
|
let pdfResults = await traverseOperations(workflow.operations, inputs, (state) => {
|
||||||
|
console.log("State: ", state);
|
||||||
let pdfResults;
|
|
||||||
let iteration;
|
|
||||||
while (true) {
|
|
||||||
iteration = await traverse.next();
|
|
||||||
if (iteration.done) {
|
|
||||||
pdfResults = iteration.value;
|
|
||||||
if(activeWorkflow.eventStream) {
|
|
||||||
activeWorkflow.eventStream.write(`data: processing done\n\n`);
|
|
||||||
activeWorkflow.eventStream.end();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if(activeWorkflow.eventStream)
|
if(activeWorkflow.eventStream)
|
||||||
activeWorkflow.eventStream.write(`data: ${iteration.value}\n\n`);
|
activeWorkflow.eventStream.write(`data: ${state}\n\n`);
|
||||||
|
})
|
||||||
|
|
||||||
|
if(activeWorkflow.eventStream) {
|
||||||
|
activeWorkflow.eventStream.write(`data: processing done\n\n`);
|
||||||
|
activeWorkflow.eventStream.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
activeWorkflow.result = pdfResults;
|
activeWorkflow.result = pdfResults;
|
||||||
|
@ -4,14 +4,24 @@ import { PdfFile } from "../wrappers/PdfFile";
|
|||||||
import { Progress } from "../functions";
|
import { Progress } from "../functions";
|
||||||
import { Impose } from "../functions/impose";
|
import { Impose } from "../functions/impose";
|
||||||
|
|
||||||
// TODO: Fix Operators Type
|
export async function traverseOperations(operations: Action[], input: PdfFile[], progressCallback: (state: Progress) => void): Promise<PdfFile[]> {
|
||||||
export async function * traverseOperations(operations: Action[], input: PdfFile[] | PdfFile): AsyncGenerator<string, PdfFile[], void> {
|
// TODO: Validate using inbuilt validators:
|
||||||
|
/*
|
||||||
|
validationResult = impose.validate()
|
||||||
|
if(validationResult.valid) {
|
||||||
|
// Check Next
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return validationResult.reason
|
||||||
|
}
|
||||||
|
*/
|
||||||
const waitOperations = organizeWaitOperations(operations);
|
const waitOperations = organizeWaitOperations(operations);
|
||||||
|
|
||||||
let results: PdfFile[] = [];
|
let results: PdfFile[] = [];
|
||||||
yield* nextOperation(operations, input);
|
await nextOperation(operations, input, progressCallback);
|
||||||
return results;
|
return results;
|
||||||
|
|
||||||
async function * nextOperation(actions: Action[] | undefined, input: PdfFile[] | PdfFile): AsyncGenerator<string, void, void> {
|
async function nextOperation(actions: Action[] | undefined, input: PdfFile[], progressCallback: (state: Progress) => void): Promise<void> {
|
||||||
console.log("Next Operation");
|
console.log("Next Operation");
|
||||||
if(actions === undefined || (Array.isArray(actions) && actions.length == 0)) { // isEmpty
|
if(actions === undefined || (Array.isArray(actions) && actions.length == 0)) { // isEmpty
|
||||||
console.log("Last Operation");
|
console.log("Last Operation");
|
||||||
@ -21,21 +31,15 @@ export async function * traverseOperations(operations: Action[], input: PdfFile[
|
|||||||
results = results.concat(input);
|
results = results.concat(input);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
console.log("operation done: " + input.filename);
|
|
||||||
results.push(input);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = 0; i < actions.length; i++) {
|
for (let i = 0; i < actions.length; i++) {
|
||||||
yield* computeOperation(actions[i], Object.assign([], input)); // structuredClone-like for ts TODO: test if this really works
|
await computeOperation(actions[i], Object.assign([], input), progressCallback); // structuredClone-like for ts TODO: test if this really works
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function * computeOperation(action: Action, input: PdfFile[]): AsyncGenerator<string, void, void> {
|
async function computeOperation(action: Action, input: PdfFile[], progressCallback: (state: Progress) => void): Promise<void> {
|
||||||
console.log("Input: ", input);
|
console.log("Input: ", input);
|
||||||
yield "Starting: " + action.type;
|
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case "done": // Skip this, because it is a valid node.
|
case "done": // Skip this, because it is a valid node.
|
||||||
break;
|
break;
|
||||||
@ -51,7 +55,7 @@ export async function * traverseOperations(operations: Action[], input: PdfFile[
|
|||||||
|
|
||||||
waitOperation.waitCount--;
|
waitOperation.waitCount--;
|
||||||
if(waitOperation.waitCount == 0 && waitOperation.doneOperation.actions) {
|
if(waitOperation.waitCount == 0 && waitOperation.doneOperation.actions) {
|
||||||
yield* nextOperation(waitOperation.doneOperation.actions, waitOperation.input);
|
await nextOperation(waitOperation.doneOperation.actions, waitOperation.input, progressCallback);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
/*case "extract":
|
/*case "extract":
|
||||||
@ -62,11 +66,10 @@ export async function * traverseOperations(operations: Action[], input: PdfFile[
|
|||||||
break;*/
|
break;*/
|
||||||
case "impose":
|
case "impose":
|
||||||
let impose = new Impose(action);
|
let impose = new Impose(action);
|
||||||
if(impose.validate().valid) {
|
input = await impose.run(input, (state: Progress) => {
|
||||||
impose.run(input, (state: Progress) => {
|
progressCallback(state);
|
||||||
console.log(state);
|
});
|
||||||
});
|
await nextOperation(action.actions, input, progressCallback);
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
/*case "merge":
|
/*case "merge":
|
||||||
yield* nToOne(input, action, async (inputs) => {
|
yield* nToOne(input, action, async (inputs) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user