diff --git a/shared-operations/src/functions/index.ts b/shared-operations/src/functions/index.ts index f883f2b04..758a251a7 100644 --- a/shared-operations/src/functions/index.ts +++ b/shared-operations/src/functions/index.ts @@ -53,6 +53,4 @@ export class Operator { return output; } -} - -// TODO: Export Operators? \ No newline at end of file +} \ No newline at end of file diff --git a/shared-operations/src/workflow/getOperatorByName.ts b/shared-operations/src/workflow/getOperatorByName.ts new file mode 100644 index 000000000..289736b44 --- /dev/null +++ b/shared-operations/src/workflow/getOperatorByName.ts @@ -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; +} \ No newline at end of file diff --git a/shared-operations/src/workflow/traverseOperations.ts b/shared-operations/src/workflow/traverseOperations.ts index eaa562c9e..4c459d2e5 100644 --- a/shared-operations/src/workflow/traverseOperations.ts +++ b/shared-operations/src/workflow/traverseOperations.ts @@ -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 { - // TODO: Validate using inbuilt validators: - /* - validationResult = impose.validate() - if(validationResult.valid) { - // Check Next - } - else { - return validationResult.reason - } - */ + + const validationResult = validateOperations(operations) + if(!validationResult.valid) { + throw Error(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,7 +115,14 @@ export async function traverseOperations(operations: Action[], input: PdfFile[], }); break;*/ 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.`); } } } \ No newline at end of file diff --git a/shared-operations/src/workflow/validateOperations.ts b/shared-operations/src/workflow/validateOperations.ts new file mode 100644 index 000000000..2d3bd2f6f --- /dev/null +++ b/shared-operations/src/workflow/validateOperations.ts @@ -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 }; +} \ No newline at end of file