2023-11-14 03:26:42 +03:00
|
|
|
import { organizeWaitOperations } from "./organizeWaitOperations";
|
2023-11-14 17:34:50 +01:00
|
|
|
import { Action, WaitAction } from "../../declarations/Action";
|
2023-11-14 03:26:42 +03:00
|
|
|
import { PdfFile } from "../wrappers/PdfFile";
|
2023-11-20 21:04:49 +01:00
|
|
|
import { Progress } from "../functions";
|
2023-11-20 22:12:03 +01:00
|
|
|
import { validateOperations } from "./validateOperations";
|
2024-02-23 23:48:03 +01:00
|
|
|
import { getOperatorByName } from "./operatorAccessor";
|
2023-10-17 01:38:51 +02:00
|
|
|
|
2024-05-10 22:38:47 +02:00
|
|
|
export async function traverseOperations(actions: Action[], input: PdfFile[], progressCallback: (state: Progress) => void): Promise<PdfFile[]> {
|
|
|
|
|
const validationResult = await validateOperations(actions);
|
2023-11-20 22:12:03 +01:00
|
|
|
if(!validationResult.valid) {
|
2023-11-20 22:43:09 +01:00
|
|
|
return Promise.reject({validationError: validationResult.reason});
|
2023-11-20 22:12:03 +01:00
|
|
|
}
|
2023-11-20 22:43:09 +01:00
|
|
|
|
2024-05-10 22:38:47 +02:00
|
|
|
const waitOperations = organizeWaitOperations(actions);
|
2023-11-20 21:45:49 +01:00
|
|
|
|
2023-11-14 03:26:42 +03:00
|
|
|
let results: PdfFile[] = [];
|
2024-05-10 22:38:47 +02:00
|
|
|
await nextOperation(actions, input, progressCallback);
|
2023-10-17 02:14:06 +02:00
|
|
|
return results;
|
2023-10-17 01:38:51 +02:00
|
|
|
|
2023-11-20 21:45:49 +01:00
|
|
|
async function nextOperation(actions: Action[] | undefined, input: PdfFile[], progressCallback: (state: Progress) => void): Promise<void> {
|
2023-11-21 14:40:29 +01:00
|
|
|
if(!actions || (Array.isArray(actions) && actions.length == 0)) { // isEmpty
|
|
|
|
|
if(input && Array.isArray(input)) {
|
2024-02-27 21:51:03 +01:00
|
|
|
console.log("operation done: " + input.map(file => file.filename) + (input.length > 1 ? "+" : ""));
|
2023-10-22 01:11:17 +02:00
|
|
|
results = results.concat(input);
|
|
|
|
|
}
|
2023-11-21 14:40:29 +01:00
|
|
|
return;
|
2023-10-17 01:38:51 +02:00
|
|
|
}
|
2023-11-14 03:26:42 +03:00
|
|
|
for (let i = 0; i < actions.length; i++) {
|
2023-11-20 21:45:49 +01:00
|
|
|
await computeOperation(actions[i], Object.assign([], input), progressCallback); // structuredClone-like for ts TODO: test if this really works
|
2023-10-17 01:38:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-20 21:45:49 +01:00
|
|
|
async function computeOperation(action: Action, input: PdfFile[], progressCallback: (state: Progress) => void): Promise<void> {
|
2023-11-14 23:32:03 +01:00
|
|
|
console.log("Input: ", input);
|
2023-11-14 03:26:42 +03:00
|
|
|
switch (action.type) {
|
2024-01-04 20:17:54 -05:00
|
|
|
case "done": // Skip this, because it is a valid node.
|
|
|
|
|
break;
|
|
|
|
|
case "wait":
|
|
|
|
|
const waitOperation = waitOperations[(action as WaitAction).values.id];
|
2023-10-17 03:40:54 +02:00
|
|
|
|
2024-01-04 20:17:54 -05:00
|
|
|
waitOperation.input.concat(input); // TODO: May have unexpected concequences. Needs further testing!
|
2023-10-17 03:40:54 +02:00
|
|
|
|
2024-01-04 20:17:54 -05:00
|
|
|
waitOperation.waitCount--;
|
|
|
|
|
if(waitOperation.waitCount == 0 && waitOperation.doneOperation.actions) {
|
|
|
|
|
await nextOperation(waitOperation.doneOperation.actions, waitOperation.input, progressCallback);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2024-02-23 23:48:03 +01:00
|
|
|
const operator = await getOperatorByName(action.type);
|
2024-01-04 20:17:54 -05:00
|
|
|
if(operator) {
|
|
|
|
|
const operation = new operator(action);
|
|
|
|
|
input = await operation.run(input, progressCallback);
|
|
|
|
|
await nextOperation(action.actions, input, progressCallback);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw new Error(`${action.type} not implemented yet.`);
|
2023-10-17 01:38:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|