HAConnection

public protocol HAConnection : AnyObject

The interface for the API itself

See also

HAKit for how to create an instance
  • Handler invoked when a request completes

    Declaration

    Swift

    typealias RequestCompletion = (Result<HAData, HAError>) -> Void
  • Handler invoked when the initial request to start a subscription completes

    Declaration

    Swift

    typealias SubscriptionInitiatedHandler = (Result<HAData, HAError>) -> Void
  • Handler invoked when a subscription receives a new event

    Declaration

    Swift

    typealias SubscriptionHandler = (HACancellable, HAData) -> Void
  • The delegate of the connection

    Declaration

    Swift

    var delegate: HAConnectionDelegate? { get set }
  • The current configuration for the connection

    Declaration

    Swift

    var configuration: HAConnectionConfiguration { get set }
  • The current state of the connection

    Declaration

    Swift

    var state: HAConnectionState { get }
  • Container for caches, either from this library or created externally

    Declaration

    Swift

    var caches: HACachesContainer { get }
  • The queue to invoke all handlers on This defaults to DispatchQueue.main

    Declaration

    Swift

    var callbackQueue: DispatchQueue { get set }
  • Attempt to connect to the server This will attempt immediately and then make retry attempts based on timing and/or reachability and/or application state. Calling connect again without disconnecting will send a ‘ping’ request to verify connection.

    Declaration

    Swift

    func connect()
  • Disconnect from the server or end reconnection attempts

    Declaration

    Swift

    func disconnect()
  • Send a request

    If the connection is currently disconnected, or this request fails to be responded to, this will be reissued in the future until it individually fails or is cancelled.

    Declaration

    Swift

    @discardableResult
    func send(
        _ request: HARequest,
        completion: @escaping RequestCompletion
    ) -> HACancellable

    Parameters

    request

    The request to send; invoked at most once

    completion

    The handler to invoke on completion

    Return Value

    A token which can be used to cancel the request

  • Send a request with a concrete response type

    If the connection is currently disconnected, or this request fails to be responded to, this will be reissued in the future until it individually fails or is cancelled.

    See also

    HATypedRequest extensions which create instances of it

    Declaration

    Swift

    @discardableResult
    func send<T>(
        _ request: HATypedRequest<T>,
        completion: @escaping (Result<T, HAError>) -> Void
    ) -> HACancellable

    Parameters

    request

    The request to send; invoked at most once

    completion

    The handler to invoke on completion

    Return Value

    A token which can be used to cancel the request

  • Start a subscription to a request

    Subscriptions will automatically be restarted if the current connection to the server disconnects and then reconnects.

    Declaration

    Swift

    @discardableResult
    func subscribe(
        to request: HARequest,
        handler: @escaping SubscriptionHandler
    ) -> HACancellable

    Parameters

    request

    The request to send to start the subscription

    handler

    The handler to invoke when new events are received for the subscription; invoked many times

    Return Value

    A token which can be used to cancel the subscription

  • Start a subscription and be notified about its start state

    Subscriptions will automatically be restarted if the current connection to the server disconnects and then reconnects. When each restart event occurs, the initiated handler will be invoked again.

    Declaration

    Swift

    @discardableResult
    func subscribe(
        to request: HARequest,
        initiated: @escaping SubscriptionInitiatedHandler,
        handler: @escaping SubscriptionHandler
    ) -> HACancellable

    Parameters

    request

    The request to send to start the subscription

    initiated

    The handler to invoke when the subscription’s initial request succeeds or fails; invoked once per underlying WebSocket connection

    handler

    The handler to invoke when new events are received for the subscription; invoked many times

  • Start a subscription to a request with a concrete event type

    Subscriptions will automatically be restarted if the current connection to the server disconnects and then reconnects.

    See also

    HATypedSubscription extensions which create instances of it

    Declaration

    Swift

    @discardableResult
    func subscribe<T>(
        to request: HATypedSubscription<T>,
        handler: @escaping (HACancellable, T) -> Void
    ) -> HACancellable

    Parameters

    request

    The request to send to start the subscription

    handler

    The handler to invoke when new events are received for the subscription; invoked many times

    Return Value

    A token which can be used to cancel the subscription

  • Start a subscription to a request with a concrete event type

    Subscriptions will automatically be restarted if the current connection to the server disconnects and then reconnects. When each restart event occurs, the initiated handler will be invoked again.

    See also

    HATypedSubscription extensions which create instances of it

    Declaration

    Swift

    @discardableResult
    func subscribe<T>(
        to request: HATypedSubscription<T>,
        initiated: @escaping SubscriptionInitiatedHandler,
        handler: @escaping (HACancellable, T) -> Void
    ) -> HACancellable

    Parameters

    request

    The request to send to start the subscription

    initiated

    The handler to invoke when the subscription’s initial request succeeds or fails; invoked once per underlying WebSocket connection

    handler

    The handler to invoke when new events are received for the subscription; invoked many times

    Return Value

    A token which can be used to cancel the subscription