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.
-
Declaration
Swift
@MainActor public let id: UUID -
Always
nil; a branch does not represent a single interactor action type.Declaration
Swift
@MainActor public var actionType: (any InteractorRequestActionTypeable)? { get } -
Declaration
Swift
@MainActor public let identifier: (any ActionIdentifying)? -
The conduit that receives the selected branch path’s (optionally transformed) output and forwards it to the next sequence step.
Declaration
Swift
@MainActor public var outputConduit: (any ActionSequenceConduiting<ContentType>)? -
Declaration
Swift
@MainActor public let shouldEndParentTaskOnFailure: Bool -
Declaration
Swift
@MainActor public let shouldSaveResult: Bool -
The ordered list of branch paths. Conditions are evaluated in declaration order; the first match is selected.
Declaration
Swift
@MainActor public var cases: [ActionBranchPath<ContentType>] -
Undocumented
Declaration
Swift
@MainActor public init(cases: [ActionBranchPath<ContentType>], identifier: (any ActionIdentifying)? = nil, outputConduit: (any ActionSequenceConduiting<ContentType>)? = nil, shouldEndParentTaskOnFailure: Bool = false, shouldSaveResult: Bool = true) -
perform(with:AsynchronoussequenceOutputs: ) Evaluates each branch path condition in order, runs the first matching path, and forwards its (optionally transformed) output to the next sequence step.
Declaration
Swift
@MainActor public func perform(with content: ContentType?, sequenceOutputs: ActionCollectionResults<ContentType>) async -> Result<ActionCollectionResults<ContentType>, any Error>Parameters
contentThe content value forwarded from the previous sequence step.
sequenceOutputsThe accumulated results from all prior steps in the sequence.
Return Value
.successwith the updatedActionCollectionResultson success, or.failurewrapping anActionErrorif no condition matched, the selected branch path failed, or the task was cancelled.
View on GitHub