ActionSequence
@MainActor
public final class ActionSequence<ContentType> : ActionPerformableCollection where ContentType : ContentTypeable
This object controls a sequence of actions that are run in serial order, passing output from one sequence step to the next.
ActionSequence manages a chain of ActionPerformable steps connected by output conduits. Each step receives the content produced by the previous step via its output conduit. The sequence is run starting from the first action, which signals each action to run successively in order. A sequence step can also be an ActionGroup, which runs a collection of actions in parallel.
Note
Generally you should not instantiate an ActionSequence directly. Instead you should use an ActionSequenceConfiguration object to build it. You can either assign this configuration to DestinationProviding/interactorsData and run it via its associated event type, or you can build these configurations within a state model and call Destinationable/performActions(configuration:content:)-c2a7 on its Destination.
Usage
Build a sequence by composing ActionConfiguration steps inside an ActionSequenceConfiguration using the .step(_:inputTransformer:) builder API. Each call to .step(_:) automatically links the previous step’s output to the new step; the optional inputTransformer converts that output before the new step receives it.
// Step 1: retrieve data from an interactor.
let retrievalStep = ActionConfiguration<InteractorType, ContentType, FetchInteractor>(
interactorType: .api,
action: .retrieve,
assistant: .basicAsync,
identifier: ActionIdentifier.retrieve)
// Step 2: process the retrieved data.
let processStep = ActionConfiguration<InteractorType, ContentType, ProcessInteractor>(
interactorType: .process,
action: .transform,
assistant: .basicAsync,
identifier: ActionIdentifier.process)
// Link the steps: retrievalStep's output is transformed and passed into processStep.
let filesSequence = ActionSequenceConfiguration<InteractorType, ContentType>(identifier: ActionIdentifier.fileSequence)
.step(retrievalStep)
.step(processStep, inputTransformer: FileTransformer())
// Run the actions and inspect the accumulated results.
let result = await destination.performActions(configuration: filesSequence, content: nil)
if case .success(let outputs) = result,
let processResult = outputs.last(identifier: ActionIdentifier.fileSequence),
case .savedFiles(let urls) = processResult.content {
// processResult.content holds the final transformed value.
}
-
Declaration
Swift
@MainActor public let id: UUID -
The ordered list of actions that make up this sequence. The sequence runs each action in order of this array.
Declaration
Swift
@MainActor public var actions: [any ActionPerformable<ContentType>] -
Declaration
Swift
@MainActor public var outputConduit: (any ActionSequenceConduiting<ContentType>)? -
Declaration
Swift
@MainActor public let identifier: (any ActionIdentifying)? -
Declaration
Swift
@MainActor public var actionType: (any InteractorRequestActionTypeable)? { get } -
Declaration
Swift
@MainActor public let shouldEndParentTaskOnFailure: Bool -
Declaration
Swift
@MainActor public let shouldSaveResult: Bool -
Creates an
ActionSequencewith an optional initial set of actions.Declaration
Swift
@MainActor public init(actions: [any ActionPerformable<ContentType>]? = nil, identifier: (any ActionIdentifying)? = nil, outputConduit: (any ActionSequenceConduiting<ContentType>)? = nil, shouldEndParentTaskOnFailure: Bool = false, shouldSaveResult: Bool = true)Parameters
actionsAn optional array of actions to seed the sequence. If
nil, the sequence starts empty. -
Appends a new step to the end of the sequence, linking it to the previous step’s output conduit.
If the sequence already contains at least one action, the last action must have a non-nil
outputConduit; the new step is assigned as that conduit’soutputActionbefore being appended. If the sequence is empty, the step is appended directly.Throws
missingConduitif the current last action has no output conduit.Declaration
Swift
@MainActor public func add(action: any ActionPerformable<ContentType>) throwsParameters
actionThe action to append as the next step in the sequence.
-
perform(with:AsynchronoussequenceOutputs: ) Starts the action sequence, passing in optional content to be used when running the first action in the sequence.
Each action is responsible for forwarding results to subsequent steps via its output conduit. The accumulated outputs from all steps are returned upon completion.
Declaration
Swift
@MainActor public func perform(with content: ContentType?, sequenceOutputs: ActionCollectionResults<ContentType>) async -> Result<ActionCollectionResults<ContentType>, any Error>Parameters
contentOptional initial content passed to the first action.
Return Value
A
ResultcontainingActionSequenceResultswith all step outputs on success, or an error if the sequence is empty or any step fails.
View on GitHub