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)
  }
}