Skip to content

Improve interface for defining Flows #3376

@jomhyre

Description

@jomhyre

I have an app with a multistep workflow, for which I find the FlowProducer feature to be perfect for sequencing each step, and keeping everything robust when dealing with flaky APIs.

However, I find the interface for creating these flows to be awkward to both write and read, since they are essentially declared backwards:

await flowProducer.add({
    name: 'eat-veggies',
    children: [
        {
            name: 'roast-veggies',
            children: [
                {
                    name: 'chop-veggies',
                }
            ]
        }
    ]
})

I would prefer if I was able to define these flows as a simple sequence of steps, i.e.:

await flowProducer.add([
        {
            name: 'chop-veggies',
        },
        {
            name: 'roast-veggies',
        },
        {
            name: 'eat-veggies',
        },
    ]
)

or possibly even with a fluent interface/builder pattern, e.g.

await flowProducer.add(
    makeFlowOrSomething()
        .addStep("chop-veggies")
        .addStep("roast-veggies")
        .addStep("eat-veggies")
)

I've currently using my own hand-rolled helper function to do this

interface FlowNode {
  name: string;
  queueName: string;
  data: any;
  children?: FlowNode[];
}

function buildFlow(
    firstStep: FlowNode,
    ...remainingSteps: FlowNode[]
): FlowJob {
    let root: FlowNode = firstStep;

    for (const step of remainingSteps) {
        root = { ...step, children: [...(step.children ?? []), root] };
    }

    return root;
}

But I think it would be really nice if this were part of the library. Is there any reason not to take this approach?

It doesn't have to only apply to purely sequential flows as we could still have sibling nodes which run in parallel, e.g.:

await flows.add([
    {
        name: 'chop-veggies'
    },
    [
        {
            name: 'roast-veggies'
        },
        {
            name: 'set-table'
        }
    ],
    {
        name: 'eat-veggies'
    }
])

Excuse me if we already have something like this, I looked around and couldn't find it anywhere.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions