So I have an external API that I have no control over. The API used to create some workflow of other part of the program, to create that workflow the user- in this case me, have to define a closure that
state
Object.- define an
entrypoint
function, which calls the method thatinitiate
the workflow, provided by the API - the
initiate
method take a function, sayfunc1
as an argument, here the actual workflow starts func1
awaits some event then it updates thestate
and returns a function that returnsfunc2
which is the second step in the workflow, and you read it right it with two returns(input: stepInput) => func2(input, state)
and I have around 20 of these workflows, with average 5 steps for the single workflow and even the options used to tweak the event are similar with minor changes, there are only two variations of events.
async function Workflow() {
interface State {
title: string;
step: number;
totalSteps: number;
result1: string;
result2: string;
result3: string;
}
async function entrypoint() {
const state = {} as Partial<State>;
await initiate(input => step1(input, state));
return state as State;
}
const title = 'workflow title';
async function step1(input: StepInput, state: Partial<State>) {
const result1 = await event1({ // options that tweaks the event });
state.result1 = result1;
return (input: StepInput) => step2(input, state);
}
async function step2(input: StepInput, state: Partial<State>) {
const result2 = await event1({ // options that tweaks the event });
state.result2 = result2;
return (input: StepInput) => step3(input, state);
}
async function step3(input: StepInput, state: Partial<State>) {
const result3 = await event2({ // options that tweaks the event });
state.result3 = result3;
}
state = await entrypoint();
}
I tried to Implement a workflow factory, I ran out of ideas what to pass this factory but I think it will look something like that
async function WorkflowFactory(
steps: any[], // still have no clue what this should be
) {
// the workflow
return async function() {
function someFuncion() {
// probably here the factory magic will happen
}
async function entrypoint() {
const state = { } as Partial<State>;
await initiate(input => steps[0](input, state) );
}
};
}
I don't know for sure if this is the right architectural approach, even a suggestion for different pattern is appreciated
Aucun commentaire:
Enregistrer un commentaire