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

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