OZSmartAccountEventEmitter

public final class OZSmartAccountEventEmitter : @unchecked Sendable

Event emitter for Smart Account lifecycle events.

Manages event subscriptions and dispatches events to all registered listeners. Subscription management and event emission are thread-safe; listener callbacks are invoked outside the internal lock so that a listener may freely call back into the emitter (for example to unsubscribe itself) without deadlocking.

Features:

  • Thread-safe listener management via an internal NSLock.
  • Multiple listeners per event type, plus global listeners that receive every event.
  • Listener error isolation: an error thrown by one listener never prevents sibling listeners from running and never propagates out of emit.
  • Optional error handler for diagnosing listener failures.
  • Synchronous public API so listeners may safely re-enter the emitter during emission.

Example:

let emitter = OZSmartAccountEventEmitter()

// Typed subscription via an OZSmartAccountEventType tag
let unsubscribe = emitter.on(.walletConnected) { event in
    if case let .walletConnected(contractId, _) = event {
        print("Connected to \(contractId)")
    }
}

// Global subscription
let unsub = emitter.addListener { event in
    // dispatch as needed
}

// One-time listener
_ = emitter.once(.transactionSubmitted) { event in
    if case let .transactionSubmitted(hash, _) = event {
        print("First transaction: \(hash)")
    }
}

unsubscribe()
  • Initializes a new emitter with no listeners and no error handler.

    Declaration

    Swift

    public init()

setErrorHandler

  • Sets the error handler invoked when a listener throws while handling an event.

    The error handler receives both the event being dispatched and the error thrown by the failing listener. Pass nil to disable error reporting; in that case listener errors are silently caught so a single failing listener cannot abort emission to its siblings.

    Declaration

    Swift

    public func setErrorHandler(_ handler: OZSmartAccountEventErrorHandler?)

    Parameters

    handler

    Error handler closure, or nil to disable.

addListener

  • Subscribes a listener to receive every emitted event.

    Unlike on(_:listener:), this method registers a global listener that receives all event arms regardless of type. Use it from call sites that dispatch with a switch over the event itself.

    Declaration

    Swift

    @discardableResult
    public func addListener(_ listener: @escaping OZSmartAccountEventListener) -> OZSmartAccountEventUnsubscribe

    Parameters

    listener

    The closure invoked for every event.

    Return Value

    A closure that unsubscribes the listener when called. Calling the returned closure more than once is a no-op.

on

once

  • Subscribes to an event of a specific type, but only triggers once.

    The listener is automatically unsubscribed before its body runs, so even if the listener throws, the once-subscription is still removed. The returned closure unsubscribes the listener before it ever fires; calling it after the event has already fired is a no-op.

    Declaration

    Swift

    @discardableResult
    public func once(
        _ eventType: OZSmartAccountEventType,
        listener: @escaping OZSmartAccountEventListener
    ) -> OZSmartAccountEventUnsubscribe

    Parameters

    eventType

    The OZSmartAccountEventType tag identifying which event arm to subscribe to.

    listener

    The closure invoked for the first matching event.

    Return Value

    A closure that unsubscribes the listener if called before the first matching event.

removeAllListeners

  • Removes registered listeners.

    When eventType is non-nil, only type-specific listeners registered via on(_:listener:) for that event type are removed; global listeners registered via addListener(_:) are left intact. Passing nil removes every type-specific listener and every global listener.

    Declaration

    Swift

    public func removeAllListeners(eventType: String? = nil)

    Parameters

    eventType

    The event tag to clear, or nil to clear all listeners (both typed and global).

  • Convenience overload that removes both type-specific and global listeners.

    Declaration

    Swift

    public func removeAllListeners()

listenerCount

  • Returns the number of listeners currently registered for the supplied event tag.

    The count is the sum of type-specific listeners registered via on(_:listener:) for eventType plus every global listener registered via addListener(_:).

    Declaration

    Swift

    public func listenerCount(eventType: String) -> Int

    Parameters

    eventType

    The event tag to query (for example "WalletConnected").

    Return Value

    The number of registered listeners (type-specific plus global).