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()
-
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
nilto 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
handlerError handler closure, or
nilto disable.
-
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 aswitchover the event itself.Declaration
Swift
@discardableResult public func addListener(_ listener: @escaping OZSmartAccountEventListener) -> OZSmartAccountEventUnsubscribeParameters
listenerThe 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.
-
Subscribes to events of a specific type.
The listener is invoked only when an event matching
eventTypeis emitted. The returned closure unsubscribes the listener when called.Declaration
Swift
@discardableResult public func on( _ eventType: OZSmartAccountEventType, listener: @escaping OZSmartAccountEventListener ) -> OZSmartAccountEventUnsubscribeParameters
eventTypeThe
OZSmartAccountEventTypetag identifying which event arm to subscribe to.listenerThe closure invoked for each matching event.
Return Value
A closure that unsubscribes the listener when called.
-
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 ) -> OZSmartAccountEventUnsubscribeParameters
eventTypeThe
OZSmartAccountEventTypetag identifying which event arm to subscribe to.listenerThe closure invoked for the first matching event.
Return Value
A closure that unsubscribes the listener if called before the first matching event.
-
Removes registered listeners.
When
eventTypeis non-nil, only type-specific listeners registered viaon(_:listener:)for that event type are removed; global listeners registered viaaddListener(_:)are left intact. Passingnilremoves every type-specific listener and every global listener.Declaration
Swift
public func removeAllListeners(eventType: String? = nil)Parameters
eventTypeThe event tag to clear, or
nilto clear all listeners (both typed and global). -
Convenience overload that removes both type-specific and global listeners.
Declaration
Swift
public func removeAllListeners()
-
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:)foreventTypeplus every global listener registered viaaddListener(_:).Declaration
Swift
public func listenerCount(eventType: String) -> IntParameters
eventTypeThe event tag to query (for example
"WalletConnected").Return Value
The number of registered listeners (type-specific plus global).
View on GitHub
Install in Dash