Dynamic Operators

This commit is contained in:
Felix Kaspar 2023-11-20 22:12:03 +01:00
parent a02169048d
commit 6d81fa1a9e
4 changed files with 61 additions and 20 deletions

View File

@ -54,5 +54,3 @@ export class Operator {
return output;
}
}
// TODO: Export Operators?

View File

@ -0,0 +1,26 @@
import { Operator } from "../functions";
// TODO: Import other Operators
import { Impose } from "../functions/impose";
export const Operators = {
Impose: Impose
}
// TODO: Convert this to a map or similar
export function getOperatorByName(name: string): typeof Operator {
let foundClass: typeof Operator = null;
// Loop over each default export
Object.entries(Operators).some(([className, exportedClass]) => {
// Check if the exported item is a class
if (typeof exportedClass === 'function' && exportedClass.prototype) {
if (exportedClass.type === name) {
foundClass = exportedClass;
return true; // Stop the iteration
}
}
return false;
});
return foundClass;
}

View File

@ -2,19 +2,15 @@ import { organizeWaitOperations } from "./organizeWaitOperations";
import { Action, WaitAction } from "../../declarations/Action";
import { PdfFile } from "../wrappers/PdfFile";
import { Progress } from "../functions";
import { Impose } from "../functions/impose";
import { validateOperations } from "./validateOperations";
import { getOperatorByName } from "./getOperatorByName";
export async function traverseOperations(operations: Action[], input: PdfFile[], progressCallback: (state: Progress) => void): Promise<PdfFile[]> {
// TODO: Validate using inbuilt validators:
/*
validationResult = impose.validate()
if(validationResult.valid) {
// Check Next
const validationResult = validateOperations(operations)
if(!validationResult.valid) {
throw Error(validationResult.reason);
}
else {
return validationResult.reason
}
*/
const waitOperations = organizeWaitOperations(operations);
let results: PdfFile[] = [];
@ -63,15 +59,13 @@ export async function traverseOperations(operations: Action[], input: PdfFile[],
const newPdf = await Operations.extractPages({file: input, pageIndexes: action.values["pageIndexes"]});
return newPdf;
});
break;*/
break;
case "impose":
let impose = new Impose(action);
input = await impose.run(input, (state: Progress) => {
progressCallback(state);
});
input = await impose.run(input, progressCallback);
await nextOperation(action.actions, input, progressCallback);
break;
/*case "merge":
case "merge":
yield* nToOne(input, action, async (inputs) => {
const newPdf = await Operations.mergePDFs({files: inputs});
return newPdf;
@ -121,6 +115,13 @@ export async function traverseOperations(operations: Action[], input: PdfFile[],
});
break;*/
default:
const operator = getOperatorByName(action.type);
if(operator) {
let opteration = new operator(action);
input = await opteration.run(input, progressCallback);
await nextOperation(action.actions, input, progressCallback);
}
else
throw new Error(`${action.type} not implemented yet.`);
}
}

View File

@ -0,0 +1,16 @@
import { Action } from "../../declarations/Action";
export function validateOperations(actions: Action[]): { valid: boolean, reason?: string} {
// TODO: Validate using inbuilt validators:
/*
validationResult = impose.validate()
if(validationResult.valid) {
// Check Next
}
else {
return validationResult.reason
}
*/
return { valid: true };
}