Effect
Effect.swift:5Effect.mdstruct Effect<Action>
Creating an effect
static var none: Self
An effect that does nothing and completes immediately. Useful for situations where you must return an effect, but you don’t need to do anything.
static func run(priority: TaskPriority?, operation: @escaping (_ send: Send<Action>) async throws -> Void, catch: ((_ error: any Error, _ send: Send<Action>) async -> Void)?, fileID: StaticString, filePath: StaticString, line: UInt, column: UInt
) -> Effect<Action> Wraps an asynchronous unit of work that can emit actions any number of times in an effect.
static func send(Action
) -> Effect<Action> Initializes an effect that immediately emits the action passed in.
typealias EffectOf<R>
A convenience type alias for referring to an effect of a given reducer’s domain.
enum TaskResult<Success>
A value that represents either a success or a failure. This type differs from Swift’s
Result
type in that it uses only one generic for the success case, leaving the failure case as an untypedError
.
Cancellation
func cancellable(id: some Hashable & Sendable, cancelInFlight: Bool
) -> Effect<Action> Turns an effect into one that is capable of being canceled.
static func cancel(id: some Hashable & Sendable
) -> Effect<Action> An effect that will cancel any currently in-flight effect with the given identifier.
func withTaskCancellation<T>(id: some Hashable & Sendable, cancelInFlight: Bool, isolation: isolated (any Actor)?, operation: @escaping () async throws -> T
) async rethrows -> T Execute an operation with a cancellation identifier.
static func cancel(id: some Hashable & Sendable
) Cancel any currently in-flight operation with the given identifier.
Composition
func map<T>(@escaping (Action) -> T
) -> Effect<T> Transforms all elements from the upstream effect with a provided closure.
static func merge(Self...
) -> Effect<Action> Merges a variadic list of effects together into a single effect, which runs the effects at the same time.
static func merge(some Sequence<Self>
) -> Effect<Action> Merges a sequence of effects together into a single effect, which runs the effects at the same time.
func merge(with: Self
) -> Effect<Action> Merges this effect and another into a single effect that runs both at the same time.
static func concatenate(Self...
) -> Effect<Action> Concatenates a variadic list of effects together into a single effect, which runs the effects one after the other.
static func concatenate(some Collection<Self>
) -> Effect<Action> Concatenates a collection of effects together into a single effect, which runs the effects one after the other.
func concatenate(with: Self
) -> Effect<Action> Concatenates this effect and another into a single effect that first runs this effect, and after it completes or is cancelled, runs the other.
SwiftUI integration
func animation(Animation?
) -> Effect<Action> Wraps the emission of each element with SwiftUI’s
withAnimation
.func transaction(Transaction
) -> Effect<Action> Wraps the emission of each element with SwiftUI’s
withTransaction
.
Combine integration
static func publisher(() -> some Publisher<Action, Never>
) -> Effect<Action> Creates an effect from a Combine publisher.
func debounce<S>(id: some Hashable & Sendable, for: S.SchedulerTimeType.Stride, scheduler: S, options: S.SchedulerOptions?
) -> Effect<Action> Turns an effect into one that can be debounced.
func throttle<S>(id: some Hashable & Sendable, for: S.SchedulerTimeType.Stride, scheduler: S, latest: Bool
) -> Effect<Action> Throttles an effect so that it only publishes one output per given interval.