Bus

class Bus

A Bus for events to go from publishers to subscribers

  • A name for the bus.

    Declaration

    Swift

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

    Declaration

    Swift

    public init(name: String)

    Parameters

    name

    name to give the bus

  • Subscription identifier used by subscribers to be able to unsubscribe.

    Declaration

    Swift

    public typealias Subscription = UUID

Publish With Message

  • 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 publish<Message>(event: Causality.Event<Message>, message: Message)

    Parameters

    event

    Event to publish

    message

    Message to send in event

Publish With No Message

Subscribe

  • Add a subscriber to a specific event type

    Declaration

    Swift

    @discardableResult
    public func subscribe<Message>(_ event: Causality.Event<Message>, handler: @escaping (Message) -> Void) -> Subscription

    Parameters

    event

    The event type to subscribe to.

    handler

    A handler that is called for each event of this type that occurs. This handler will be called on the queue specified by the publisher (if given). Otherwise there is no guarantee on what queue/thread the handler will be called on.

  • Add a subscriber to a specific event type

    Declaration

    Swift

    @discardableResult
    public func subscribe<Message>(_ event: Causality.Event<Message>, queue: DispatchQueue, handler: @escaping (Message) -> Void) -> Subscription

    Parameters

    event

    The event type 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 event type

    Declaration

    Swift

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

    Parameters

    event

    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

  • Stop a particular subscription handler from listening to events anymore.

    Declaration

    Swift

    public func unsubscribe(_ subscription: Subscription)

    Parameters

    subsription

    The Subscription that was returned from subscribe()

  • Unsubscribe an array of subscriptions

    Declaration

    Swift

    public func unsubscribe(_ subscriptions: [Subscription])

    Parameters

    subscriptions

    Subscriptions to unsubscribe from