feat(workflows): Workflow DX (#5607)
This commit is contained in:
committed by
GitHub
parent
2850e9a772
commit
9f9db39698
58
packages/workflows/src/utils/composer/parallelize.ts
Normal file
58
packages/workflows/src/utils/composer/parallelize.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { CreateWorkflowComposerContext, WorkflowData } from "./type"
|
||||
import { SymbolMedusaWorkflowComposerContext } from "./helpers"
|
||||
|
||||
/**
|
||||
* Parallelize multiple steps.
|
||||
* The steps will be run in parallel. The result of each step will be returned as part of the result array.
|
||||
* Each StepResult can be accessed from the resulted array in the order they were passed to the parallelize function.
|
||||
*
|
||||
* @param steps
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { createWorkflow, WorkflowData, parallelize } from "@medusajs/workflows"
|
||||
* import { createProductStep, getProductStep, createPricesStep, attachProductToSalesChannelStep } from "./steps"
|
||||
*
|
||||
* interface MyWorkflowData {
|
||||
* title: string
|
||||
* }
|
||||
*
|
||||
* const myWorkflow = createWorkflow("my-workflow", (input: WorkflowData<MyWorkflowData>) => {
|
||||
* const product = createProductStep(input)
|
||||
*
|
||||
* const [prices, productSalesChannel] = parallelize(
|
||||
* createPricesStep(product),
|
||||
* attachProductToSalesChannelStep(product)
|
||||
* )
|
||||
*
|
||||
* const id = product.id
|
||||
* return getProductStep(product.id)
|
||||
* })
|
||||
*/
|
||||
export function parallelize<TResult extends WorkflowData[]>(
|
||||
...steps: TResult
|
||||
): TResult {
|
||||
if (!global[SymbolMedusaWorkflowComposerContext]) {
|
||||
throw new Error(
|
||||
"parallelize must be used inside a createWorkflow definition"
|
||||
)
|
||||
}
|
||||
|
||||
const parallelizeBinder = (
|
||||
global[SymbolMedusaWorkflowComposerContext] as CreateWorkflowComposerContext
|
||||
).parallelizeBinder
|
||||
|
||||
const resultSteps = steps.map((step) => step)
|
||||
|
||||
return parallelizeBinder<TResult>(function (
|
||||
this: CreateWorkflowComposerContext
|
||||
) {
|
||||
const stepOntoMerge = steps.shift()!
|
||||
this.flow.mergeActions(
|
||||
stepOntoMerge.__step__,
|
||||
...steps.map((step) => step.__step__)
|
||||
)
|
||||
|
||||
return resultSteps as unknown as TResult
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user