ActionBranch

@MainActor
public final class ActionBranch<ContentType> : ActionPerformable where ContentType : ContentTypeable

An ActionPerformable conforming type which selects and runs one of several actions based on provided conditions, evaluated against the current content and previous results in a sequence and runs the first path whose conditions return true. Branches are mainly only used as a step in an ActionSequence.

ActionBranch evaluates its BranchConditionable conditions in declaration order and runs the first path whose condition (or conditions, if a group condition like AllSatisfyCondition or AnySatisfyCondition is used) returns true. If no condition matches and there’s no fallback action, the branch fails with cancelled(partialResults:). To guarantee a fallback path, you can add on an .otherwise path at the end of the branch chain. After the selected path action completes, ActionBranch applies the path’s optional transformer to the result and forwards the content through its own output conduit to the next sequence step, recording one ActionResult with origin branch.

In the example below, if there are images passed-in from the previous step, it will process the images with a filter. If instead the content is text, it will render the text into an image. If neither was provided, it will choose the fallback action. This branch object could then be used as a sequence step or in a group of actions.

let branch = ActionBranchConfiguration<AppInteractorType, AppContentType>()
    .branch(when: HasImagesCondition(), action: processImageStep)
    .branch(when: HasTextCondition(),   action: renderTextStep)
    .otherwise(fallbackAction)

Note

Do not instantiate this type directly. Use ActionBranchConfiguration to define and build a branch step.