Subscription

public class Subscription<State>

Represents a subscription of a subscriber to the store. The subscription determines which new values from the store are forwarded to the subscriber, and how they are transformed. The subscription acts as a very-light weight signal/observable that you might know from reactive programming libraries.

  • Initializes a subscription with a sink closure. The closure provides a way to send new values over this subscription.

    Declaration

    Swift

    public init(sink: @escaping (@escaping (State?, State) -> Void) -> Void)
  • Provides a subscription that selects a substate of the state of the original subscription.

    Declaration

    Swift

    public func select<Substate>(
        _ selector: @escaping (State) -> Substate
        ) -> Subscription<Substate>
    Parameters
    selector

    A closure that maps a state to a selected substate

  • Provides a subscription that selects a substate of the state of the original subscription.

    Declaration

    Swift

    public func select<Substate>(
        _ keyPath: KeyPath<State, Substate>
        ) -> Subscription<Substate>
    Parameters
    keyPath

    A key path from a state to a substate

  • Provides a subscription that skips certain state updates of the original subscription.

    Declaration

    Swift

    public func skipRepeats(_ isRepeat: @escaping (_ oldState: State, _ newState: State) -> Bool)
        -> Subscription<State>
    Parameters
    isRepeat

    A closure that determines whether a given state update is a repeat and thus should be skipped and not forwarded to subscribers.

    oldState

    The store’s old state, before the action is reduced.

    newState

    The store’s new state, after the action has been reduced.

  • The closure called with changes from the store. This closure can be written to for use in extensions to Subscription similar to skipRepeats

    Declaration

    Swift

    public var observer: ((State?, State) -> Void)?
  • Provides a subscription that skips certain state updates of the original subscription.

    This is identical to skipRepeats and is provided simply for convenience.

    Declaration

    Swift

    public func skip(when: @escaping (_ oldState: State, _ newState: State) -> Bool) -> Subscription<State>
    Parameters
    when

    A closure that determines whether a given state update is a repeat and thus should be skipped and not forwarded to subscribers.

    oldState

    The store’s old state, before the action is reduced.

    newState

    The store’s new state, after the action has been reduced.

  • Provides a subscription that only updates for certain state changes.

    This is effectively the inverse of skip(when:) / skipRepeats(:)

    Declaration

    Swift

    public func only(when: @escaping (_ oldState: State, _ newState: State) -> Bool) -> Subscription<State>
    Parameters
    when

    A closure that determines whether a given state update should notify

    oldState

    The store’s old state, before the action is reduced.

    newState

    The store’s new state, after the action has been reduced. the subscriber.