SideEffect

public protocol SideEffect : AnySideEffect

A side effect is a single atom of the logic of your application. While you can actually use them as you desire, the idea is to implement in each side effect a meaningful, self contained, piece of logic that can be used from other pieces of you application (e.g., dispatched by a View Controller or by another side effect).

The SideEffect is strongly tied to the state that it handles and the dependencies it has. This greatly simplifies the code written in normal situations. However, if you need to create updaters that are not strictly tied to a concrete types (e.g., in a library) you can use AnySideEffect.

App Tips & Tricks

To further simplify the usage of a SideEffect you can add to your application a helper protocol

/// assuming `AppState` is the type of your application's state and `DependenciesContainer` is the
/// container of your dependencies
protocol AppSideEffect: SideEffect where StateType == AppState, Dependencies == DependenciesContainer {}

By conforming to AppSideEffect, you will get better autocompletion

  • The type of the state of the store

    Declaration

    Swift

    associatedtype StateType : State
  • The type of the dependencies container that is used to pass dependencies to the side effect

    Declaration

    Swift

    associatedtype Dependencies : SideEffectDependencyContainer
  • Block that implements the logic of the side effect. You can implement the logic, leveraging the technology you desire for threading and flow management.

    However, there are two patterns that Katana suggests to use: synchronous side effects and asynchronous side effects

    Synchronous

    A synchronous side effect is a side effect that finishes its execution when the sideEffect(:) method is completed. Since the related promise (that is, the promise that is returned when the side effect is dispatched) is resolved when this method ends, it means that the caller can safely assume that the operations are completed.

    The easier way to achieve this behaviour is by using Hydra.await offered by the Hydra library. You can use Hydra.await with any API that returns a promise (e.g., the dispatch) and it blocks the execution of the method until the promise is resolved (you can find more documentation here https://github.com/malcommac/Hydra/#awaitasync).

    This should be the default approach you take for your side effects

    Asynchronous

    An asynchronous side effect is a side effect that continues to propagate its effect even after the sideEffect(:) method has been completed. This can be very helpful for very long running operations where you don’t want to block other side effects in the queue.

    In order to do use this approach, you can simply apply any asynchronous technique you know that does not block the method (e.g.: promise, callback).

    This approach is not suggested and should be used only in rare cases

    Throws

    if the logic has an error. The related promise will be rejected

    Declaration

    Swift

    func sideEffect(_ context: SideEffectContext<StateType, Dependencies>) throws

    Parameters

    context

    the context of the side effect

    Return Value

    the side effect return value that will be used to parameterize the related promise

  • anySideEffect(_:) Extension method

    Conformance of SideEffect to ReturningSideEffect

    Declaration

    Swift

    public func anySideEffect(_ context: AnySideEffectContext) throws -> Any