Bus

class Bus

A Bus for events to go from publishers to subscribers

  • A name for the bus.

    Declaration

    Swift

    public private(set) var label: String { get }
  • Queue on which to execute publish/subscribe actions to ensure thread safety

    Declaration

    Swift

    public private(set) var queue: DispatchQueue { get }
  • Initialize a Causality Event Bus

    Declaration

    Swift

    public init(label: String, queue: DispatchQueue = globalQueue)

    Parameters

    label

    name to give the bus

    queue

    Queue for bookkeeping (e.g. to ensure publish/subscribe is thread safe)

Publish Event With Message

Publish Event With No Message

Subscribe Event w/ Subscription

Subscribe Event w/o Subscription

  • Add a subscriber to an event

    Declaration

    Swift

    @discardableResult
    public func subscribe<Event, Message>(_ event: Event, queue: DispatchQueue? = nil, handler: @escaping (Message) -> Void) -> Causality.EventSubscription<Event, Message> where Event : Causality.AnyEvent<Message>

    Parameters

    event

    The event to subscribe to.

    queue

    DispatchQueue to receive messages on. This will take precedence over any queue specified by the publisher.

    handler

    A handler that is called for each event of this type that occurs (on the specified queue)

    Return Value

    Subscription handle that is needed to unsubscribe to this event

  • Add a subscriber to an event

    Declaration

    Swift

    @discardableResult
    public func subscribe<Event, Message>(_ event: Event, queue: OperationQueue, handler: @escaping (Message) -> Void) -> Causality.EventSubscription<Event, Message> where Event : Causality.AnyEvent<Message>

    Parameters

    event

    The event to subscribe to.

    queue

    OperationQueue to receive messages on. This will take precedence over any queue specified by the publisher.

    handler

    A handler that is called for each event of this type that occurs (on the specified queue)

    Return Value

    Subscription handle that is needed to unsubscribe to this event

Unsubscribe Event

Bus Extension

  • Determine if a state has an existing value

    Declaration

    Swift

    public func hasState<State, Value>(_ state: State) -> Bool where State : Causality.AnyState<Value>, Value : Equatable

    Parameters

    state

    State to check

    Return Value

    True if state has an existing value; False otherwise.

  • Get the last known value for a state

    Declaration

    Swift

    public func getState<State, Value>(_ state: State) -> Value? where State : Causality.AnyState<Value>, Value : Equatable

    Parameters

    state

    State to check

    Return Value

    Value of state

Publish Event With State

  • Publish an event to the bus.

    All subscribers to this event will have their handler called along with the associated message.

    Declaration

    Swift

    public func set<State, Value>(state: State, value: Value) where State : Causality.AnyState<Value>, Value : Equatable

    Parameters

    state

    The state to set the value for

    value

    The value to set for the given state

Subscribe Event w/ Subscription

  • Add a subscriber to a specific state type

    Declaration

    Swift

    @discardableResult
    public func subscribe<State, Value>(_ state: State, queue: DispatchQueue? = nil, handler: @escaping (Causality.StateSubscription<State, Value>, Value) -> Void) -> Causality.StateSubscription<State, Value> where State : Causality.AnyState<Value>, Value : Equatable

    Parameters

    state

    The state to subscribe to.

    queue

    DispatchQueue to receive messages on. This will take precedence over any queue specified by the publisher.

    handler

    A handler that is called for each event of this type that occurs (on the specified queue)

  • Add a subscriber to a specific state type

    Declaration

    Swift

    @discardableResult
    public func subscribe<State, Value>(_ state: State, queue: OperationQueue, handler: @escaping (Causality.StateSubscription<State, Value>, Value) -> Void) -> Causality.StateSubscription<State, Value> where State : Causality.AnyState<Value>, Value : Equatable

    Parameters

    state

    The state to subscribe to.

    queue

    OperationQueue to receive messages on. This will take precedence over any queue specified by the publisher.

    handler

    A handler that is called for each event of this type that occurs (on the specified queue)

Subscribe State w/o Subscription

  • Add a subscriber to a specific state type

    Declaration

    Swift

    public func subscribe<State, Value>(_ state: State, queue: DispatchQueue? = nil, handler: @escaping (Value) -> Void) -> Causality.StateSubscription<State, Value> where State : Causality.AnyState<Value>, Value : Equatable

    Parameters

    state

    The state to subscribe to.

    queue

    DispatchQueue to receive messages on. This will take precedence over any queue specified by the publisher.

    handler

    A handler that is called for each event of this type that occurs (on the specified queue)

  • Add a subscriber to a specific state type

    Declaration

    Swift

    public func subscribe<State, Value>(_ state: State, queue: OperationQueue, handler: @escaping (Value) -> Void) -> Causality.StateSubscription<State, Value> where State : Causality.AnyState<Value>, Value : Equatable

    Parameters

    state

    The event type to subscribe to.

    queue

    OperationQueue to receive messages on. This will take precedence over any queue specified by the publisher.

    handler

    A handler that is called for each event of this type that occurs (on the specified queue)

Unsubscribe State