StateSubject

@propertyWrapper
public class StateSubject<Element> : Subject<Element>

A Subject that must always have a value.

You can use it as a property wrapper to make the publishing private in the class where it’s contained, but still expose an ObservableState to the other classes.

  • The current state. Set a new value to publish an event to all Observers

    Declaration

    Swift

    public var value: Element { get set }
  • Creates a state subject with an initial value.

    Declaration

    Swift

    public init(_ initialValue: Element)

    Parameters

    initialValue

    The initial state value.

  • Publishes a new state value.

    Declaration

    Swift

    public override func publish(_ element: Element)

    Parameters

    element

    The new state value.

  • Converts this StateSubject to an ObservableState that is readonly and can only receive new values.

    Declaration

    Swift

    public func asObservableState() -> ObservableState<Element>
  • Returns an observable that emits only new updates of this State.

    Declaration

    Swift

    public func updates() -> Observable<Element>
  • Returns this state subject as an observable.

    Declaration

    Swift

    public override func asObservable() -> Observable<Element>
  • The wrapped observable state value for property wrapper usage.

    Declaration

    Swift

    public override var wrappedValue: ObservableState<Element> { get }

Available where Element: Equatable

  • Publishes the new event only if the new one is different from the last one

    Declaration

    Swift

    func publishIfChanged(_ element: Element)