ReturningSideEffect
public protocol ReturningSideEffect : AnySideEffect
A AnySideEffect
that is capable of returning a typed value.
In order to promote reusability of the logic written using this type, both the state and the dependencies are erased. This helps tremendously when writing libraries and generic logic, and it can also be extended to be used in the apps.
For example, if the app needs to use a typed returning side effect it can define something like:
protocol AppReturningSideEffect: ReturningSideEffect {
typealias StateType = AppState
typealias Dependencies = AppDependencies
func sideEffect(_ context: SideEffectContext<StateType, Dependencies>) throws -> ReturnType
}
extension AppReturningSideEffect {
func sideEffect(_ context: AnySideEffectContext) throws -> ReturnValue {
guard let typedContext = context as? SideEffectContext<StateType, Dependencies> else {
fatalError("Invalid context passed to side effect")
}
return try self.sideEffect(typedContext)
}
}
-
The type of the return value
Declaration
Swift
associatedtype ReturnValue
-
Implements the logic of the side effect.
Throws
if the logic has an error. The related promise will be rejectedSee also
AnySideEffect
Declaration
Swift
func sideEffect(_ context: AnySideEffectContext) throws -> ReturnValue
Parameters
context
the context of the side effect
Return Value
the side effect return value, if applicable
-
anySideEffect(_:
Extension method) Implementation of the
anySideEffect
requirement forReturningSideEffectContext
Declaration
Swift
public func anySideEffect(_ context: AnySideEffectContext) throws -> Any