2023-11-20 21:04:49 +01:00
|
|
|
import { Action } from "../../declarations/Action";
|
2023-11-27 23:35:18 +01:00
|
|
|
import Joi from "joi";
|
2023-11-20 21:04:49 +01:00
|
|
|
|
2023-11-21 00:12:35 +01:00
|
|
|
export interface ValidationResult {
|
|
|
|
valid: boolean,
|
|
|
|
reason?: string
|
|
|
|
}
|
|
|
|
|
2023-11-20 21:04:49 +01:00
|
|
|
export interface Progress {
|
2023-11-21 00:12:35 +01:00
|
|
|
/** A percentage between 0-1 describing the progress on the currently processed file */
|
2023-11-20 21:04:49 +01:00
|
|
|
curFileProgress: number,
|
2023-11-21 00:12:35 +01:00
|
|
|
/** A percentage between 0-1 describing the progress on all input files / operations */
|
2023-11-20 21:04:49 +01:00
|
|
|
operationProgress: number,
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Operator {
|
2023-11-27 23:35:18 +01:00
|
|
|
/** The internal name of the operator in camelCase (impose, merge, etc.) */
|
2023-11-20 21:04:49 +01:00
|
|
|
static type: string;
|
|
|
|
|
2023-11-27 23:35:18 +01:00
|
|
|
/** The Joi validators & decorators */
|
2023-12-21 15:57:51 +01:00
|
|
|
protected static inputSchema: Joi.Schema;
|
|
|
|
protected static valueSchema: Joi.Schema;
|
|
|
|
protected static outputSchema: Joi.Schema;
|
|
|
|
static schema: Joi.ObjectSchema<{
|
|
|
|
input: Joi.Schema;
|
|
|
|
values: Joi.Schema;
|
|
|
|
output: Joi.Schema;
|
|
|
|
}>;
|
2023-11-20 21:04:49 +01:00
|
|
|
|
|
|
|
actionValues: any;
|
|
|
|
|
|
|
|
constructor (action: Action) {
|
|
|
|
this.actionValues = action.values;
|
|
|
|
}
|
|
|
|
|
|
|
|
async run(input: any[], progressCallback: (progress: Progress) => void): Promise<any[]> {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2023-11-21 00:12:35 +01:00
|
|
|
validate(): ValidationResult {
|
2023-11-20 21:04:49 +01:00
|
|
|
if(!this.actionValues) {
|
|
|
|
return { valid: false, reason: "The Operators action values were empty."}
|
|
|
|
}
|
|
|
|
return { valid: true };
|
|
|
|
}
|
2023-11-27 23:35:18 +01:00
|
|
|
}
|
2023-11-20 21:04:49 +01:00
|
|
|
|
2023-11-27 23:35:18 +01:00
|
|
|
/** This function should be used if the Operation may take multiple files as inputs and only outputs one file */
|
|
|
|
export async function nToOne <I, O>(inputs: I[], callback: (input: I[]) => Promise<O>): Promise<O[]> {
|
|
|
|
return [await callback(inputs)];
|
|
|
|
}
|
2023-11-20 21:04:49 +01:00
|
|
|
|
2023-11-27 23:35:18 +01:00
|
|
|
/** This function should be used if the Operation takes one file as input and may output multiple files */
|
|
|
|
export async function oneToN <I, O>(inputs: I[], callback: (input: I, index: number, max: number) => Promise<O[]>): Promise<O[]> {
|
|
|
|
let output: O[] = []
|
|
|
|
for (let i = 0; i < inputs.length; i++) {
|
|
|
|
output = output.concat(await callback(inputs[i], i, inputs.length));
|
2023-11-20 21:04:49 +01:00
|
|
|
}
|
2023-11-27 23:35:18 +01:00
|
|
|
return output;
|
|
|
|
}
|
2023-11-21 00:12:35 +01:00
|
|
|
|
2023-11-27 23:35:18 +01:00
|
|
|
/** This function should be used if the Operation takes one file as input and outputs only one file */
|
|
|
|
export async function oneToOne <I, O>(inputs: I[], callback: (input: I, index: number, max: number) => Promise<O>): Promise<O[]> {
|
|
|
|
return oneToN(inputs, async (input, index, max) => {
|
|
|
|
return [await callback(input, index, max)]
|
|
|
|
});
|
2023-11-20 22:12:03 +01:00
|
|
|
}
|